-
Notifications
You must be signed in to change notification settings - Fork 513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
websocket: use dataview #2107
websocket: use dataview #2107
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #2107 +/- ##
==========================================
- Coverage 85.89% 85.86% -0.04%
==========================================
Files 76 76
Lines 6608 6608
==========================================
- Hits 5676 5674 -2
- Misses 932 934 +2
☔ View full report in Codecov by Sentry. |
Interesting: I assumed it would be quicker the way I changed it, but I didn't benchmark, so that's on me! |
@jawj don't worry about it at all, I should have explained why we used DataViews since you asked in the PR, but I didn't. There aren't currently benchmarks so there's no easy way of benchmarking this either 😆. |
@KhafraDev Thanks! Though this got me curious, so I did a bit of benchmarking ... let b, t0;
const trials = 1_000_000;
const bufferSize = 256;
const maxUint16Plus1 = 65_536;
t0 = performance.now();
for (let i = 0; i < trials; i ++) {
const value = i % maxUint16Plus1;
b = Buffer.allocUnsafe(bufferSize);
new DataView(b.buffer).setUint16(2, value);
}
console.log('buggy DataView', performance.now() - t0, 'ms');
t0 = performance.now();
for (let i = 0; i < trials; i ++) {
const value = i % maxUint16Plus1;
b = Buffer.allocUnsafe(bufferSize);
new DataView(b.buffer, b.byteOffset, b.byteLength).setUint16(2, value)
}
console.log('fixed DataView', performance.now() - t0, 'ms');
t0 = performance.now();
for (let i = 0; i < trials; i ++) {
const value = i % maxUint16Plus1;
b = Buffer.allocUnsafe(bufferSize);
b.writeUInt16BE(value, 2);
}
console.log('writeUInt16BE ', performance.now() - t0, 'ms');
t0 = performance.now();
for (let i = 0; i < trials; i ++) {
const value = i % maxUint16Plus1;
b = Buffer.allocUnsafe(bufferSize);
b[2] = value >>> 8;
b[3] = value & 0xff;
}
console.log('bit-twiddling ', performance.now() - t0, 'ms'); Which run on Node 18.10 and a 2020 Intel MacBook Pro gives:
|
I think the reason for That being said, ... it looks like this is very much what the code itself is doing as well so the performance measurement is apt for comparing what the options are for this PR. |
@KhafraDev is this mergable once conflicts are done? |
I don't think this should be merged. It's slower, because it has the overhead of creating a new |
We use a dataview here for performance reasons. I should have addressed this in #2106 but I did not.
Refs: nodejs/performance#2