-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
perf: improve Buffer.from(buf) by 29x #24341
Conversation
|
Nice catch, updated the description |
const buf = new Uint8Array(u8.buffer, u8.byteOffset, u8.byteLength); | ||
Object.setPrototypeOf(buf, Buffer.prototype); | ||
return buf.slice(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const buf = new Uint8Array(u8.buffer, u8.byteOffset, u8.byteLength); | |
Object.setPrototypeOf(buf, Buffer.prototype); | |
return buf.slice(); | |
const buf = new Uint8Array(u8); | |
Object.setPrototypeOf(buf, Buffer.prototype); | |
return buf; |
buf.slice
refers to Buffer.prototype.slice
which has undesired behavior.
$ cat a.mjs
import { Buffer } from "node:buffer";
const buf = new Uint8Array(1);
Buffer.from(buf)[0] = 1;
console.log(buf[0]);
$ node a.mjs
0
$ deno run a.mjs
0
$ ./deno-canary run a.mjs
1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@0f-0b would you be willing to open a PR with that fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you be willing to open a PR with that fix?
Opened #24352.
Benchmark code from #24341. ```shellsession $ deno run --allow-hrtime bench.mjs 6479.111583 $ target/release/deno run --allow-hrtime bench.mjs 962.753875 $ node bench.mjs 855.174875 ```
Benchmark code from denoland#24341. ```shellsession $ deno run --allow-hrtime bench.mjs 6479.111583 $ target/release/deno run --allow-hrtime bench.mjs 962.753875 $ node bench.mjs 855.174875 ```
Benchmark code from denoland#24341. ```shellsession $ deno run --allow-hrtime bench.mjs 6479.111583 $ target/release/deno run --allow-hrtime bench.mjs 962.753875 $ node bench.mjs 855.174875 ```
Improve
Buffer.from(buffer)
performance by ~29xRef #24323