Comments (9)
Well. we could reduce the for loop overhead by doing 4 masking operations instead of one.
from undici.
As I indicated earlier. The masking is as far as I know totally useless for backend and could just be skipped...
from undici.
Maybe instead of i + 4 < length, you precalculate that too
const lengthFor4 = length - (length & 3)
from undici.
import { randomBytes } from "crypto";
import { group, bench, run } from "mitata";
function maskForOne(mask, buffer) {
const alloc = Buffer.allocUnsafe(buffer.length);
for (let i = 0; i < buffer.length; ++i) {
alloc[i] = buffer[i] ^ mask[i & 3];
}
return alloc;
}
function maskForFour(mask, buffer) {
const length = buffer.length;
const alloc = Buffer.allocUnsafe(buffer.length);
const lengthFor4 = length - (length & 3);
if (length > 3) {
for (let i = 0; i < lengthFor4; i += 4) {
alloc[i] = buffer[i] ^ mask[0];
alloc[i + 1] = buffer[i + 1] ^ mask[1];
alloc[i + 2] = buffer[i + 2] ^ mask[2];
alloc[i + 3] = buffer[i + 3] ^ mask[3];
}
}
for (let i = lengthFor4; i < length; ++i) {
alloc[i] = buffer[i] ^ mask[i & 3];
}
return alloc;
}
group("mask", () => {
const buffer = new Uint8Array(randomBytes(1024 * 4).buffer);
const mask = new Uint8Array(randomBytes(4).buffer);
bench("for 1", () => maskForOne(mask, buffer));
bench("for 4", () => maskForFour(mask, buffer));
});
await run();
• mask
------------------------------------------------- -----------------------------
for 1 13'381 ns/iter (6'200 ns … 536 µs) 7'400 ns 123 µs 188 µs
for 4 9'444 ns/iter (5'800 ns … 4'131 µs) 7'000 ns 122 µs 197 µs
summary for mask
for 4
1.42x faster than for 1
from undici.
maybe for the last potential 3 operations a duffs device?
from undici.
yes?
from undici.
Something like this. Sry, I still suffer from a heat stroke and cant provide better code.
switch (length & 3) {
case 3:
buffer[i + 3] ^ mask[3]
case 2:
buffer[i + 2] ^ mask[2]
case 2:
buffer[i + 1] ^ mask[1]
...
from undici.
Take care and get well soon.
from undici.
I give this up
from undici.
Related Issues (20)
- Only use one network request when identical requests are made HOT 6
- TypeError and Access Issues in jsdom Environment Post Update to undici 6.16.0 HOT 4
- websocket: a number of conditions fail the connection but do not emit an error event
- websocket: handle parser errors more consistently
- running autobahn test suite HOT 4
- Failing autobahn tests HOT 6
- Interceptors: add response decompress interceptor HOT 4
- Implement Request/Response `.bytes()` method
- permessage-deflate support in websocket HOT 1
- bug: spreading `Headers` object has breaking behavior in versions between Node 20.12 and 20.13 HOT 9
- Limit the maximum numbers of open sockets HOT 2
- interceptors: move `throwOnError` to interceptor HOT 2
- interceptors: move signal handling to interceptor
- Support for custom proxy auth schemes HOT 7
- Nightly tests are failing HOT 2
- do not encode with encodeURIComponent if query with method GET passed. HOT 4
- Retry Handler Fails When Request Has a Body HOT 5
- autobahn workflow is always 1 commit behind HOT 1
- Nightly tests are failing HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from undici.