Skip to content
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

Merged
merged 4 commits into from
Jun 26, 2024

Conversation

littledivy
Copy link
Member

@littledivy littledivy commented Jun 26, 2024

Improve Buffer.from(buffer) performance by ~29x

$ deno run -A bench.mjs
7987.390458 ms

$ node bench.mjs
773.420042 ms

$ bun bench.mjs
737.7943750 ms

$ target/release/deno run -A bench.mjs
275.3805 ms
import { Buffer } from "node:buffer";

const buffer = Buffer.alloc(1024 * 10, "latin1");

const start = performance.now();
for (let i = 0; i < 1000000; i++) {
  Buffer.from(buffer);
}
console.log(performance.now() - start);

Ref #24323

@littledivy littledivy changed the title perf: improve Buffer.from(buf, 'base64') performance perf: improve Buffer.from(buf, 'base64') by 29x Jun 26, 2024
@0f-0b
Copy link
Contributor

0f-0b commented Jun 26, 2024

bench.mjs does not involve base64 decoding because buffer is not a string.

@littledivy littledivy changed the title perf: improve Buffer.from(buf, 'base64') by 29x perf: improve Buffer.from(buf) by 29x Jun 26, 2024
@littledivy
Copy link
Member Author

Nice catch, updated the description

@littledivy littledivy merged commit 6da8745 into denoland:main Jun 26, 2024
17 checks passed
Comment on lines +233 to +235
const buf = new Uint8Array(u8.buffer, u8.byteOffset, u8.byteLength);
Object.setPrototypeOf(buf, Buffer.prototype);
return buf.slice();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Member

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?

Copy link
Contributor

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.

dsherret added a commit that referenced this pull request Jun 26, 2024
dsherret pushed a commit that referenced this pull request Jun 26, 2024
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
```
@littledivy littledivy deleted the perf_buffer_from_base64 branch July 1, 2024 06:19
sbmsr pushed a commit to sbmsr/deno-1 that referenced this pull request Jul 2, 2024
sbmsr pushed a commit to sbmsr/deno-1 that referenced this pull request Jul 2, 2024
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
```
zebreus pushed a commit to zebreus/deno that referenced this pull request Jul 8, 2024
zebreus pushed a commit to zebreus/deno that referenced this pull request Jul 8, 2024
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
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants