Skip to content

perf(ext/web): use simdutf for base64 encode/decode - #32743

Merged
bartlomieju merged 9 commits into
mainfrom
perf/base64-simdutf
Mar 23, 2026
Merged

bartlomieju merged 9 commits into
mainfrom
perf/base64-simdutf

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Mar 15, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Replace base64-simd crate with V8's bundled simdutf library (exposed via v8::simdutf) for all base64 encode/decode operations
  • Update v8 crate to 146.9.0 and enable the simdutf feature
  • Remove base64-simd dependency from ext/web
  • Use v8::String::new_external_onebyte for large base64 encode output (>8KB) to avoid copying into V8's heap, matching Node.js's approach
  • Use MaybeUninit / Vec::with_capacity to avoid unnecessary zeroing of buffers before simdutf writes
  • Call base64 ops directly from buffer.mjs instead of going through JS wrapper functions
  • Avoids potential C++ symbol clashes when linking separate simdutf alongside V8

Benchmark results (50K iterations, ms, lower is better)

Size Op this PR canary stable node bun
32B Encode 4.7 8.7 9.1 5.5 3.3
32B Decode 5.6 10.1 11.2 3.7 7.0
32B Write 2.4 5.1 7.0 2.8 2.1
128B Encode 3.1 3.0 3.1 2.7 2.4
128B Decode 3.7 7.2 8.9 4.1 5.4
128B Write 1.9 5.2 7.4 3.0 2.4
512B Encode 5.2 6.0 6.2 3.9 6.6
512B Decode 5.5 10.0 13.6 6.0 6.5
512B Write 2.8 7.9 11.6 4.4 3.2
4KB Encode 28.8 36.5 36.8 16.0 16.1
4KB Decode 30.3 53.7 69.6 31.8 21.9
4KB Write 14.2 35.3 50.9 18.3 14.7
64KB Encode 213.4 458.9 583.3 244.2 143.3
64KB Decode 429.0 709.3 843.8 320.1 236.3
64KB Write 341.4 617.3 732.0 265.7 214.8
1MB Encode 2343.1 6039.8 8201.8 2214.4 2259.0
1MB Decode 4908.2 10254.0 11753.1 4411.0 3666.4
1MB Write 4499.9 9428.8 11100.3 4240.6 3434.0

vs canary highlights

  • Encode: 1.0-2.6x faster across all sizes
  • Decode: 1.7-2.1x faster across all sizes
  • Write: 1.8-2.5x faster across all sizes
  • atob: 6.8ms vs 7.4ms (1.1x faster)

Towards #24323

Benchmark code
const { Buffer } = await import("node:buffer");

const ITERATIONS = 50000;
const sizes = [32, 128, 512, 4096, 65536, 1048576];

function bench(label, fn, iterations) {
  // Warmup
  for (let i = 0; i < 100; i++) fn();
  const start = performance.now();
  for (let i = 0; i < iterations; i++) fn();
  const elapsed = performance.now() - start;
  return elapsed;
}

console.log(`\nBase64 Benchmark (${ITERATIONS} iterations)\n`);
console.log("Size     | Encode (ms) | Decode (ms) | Write (ms)");
console.log("---------|-------------|-------------|----------");

for (const size of sizes) {
  const buf = Buffer.alloc(size, 0x42);
  const b64str = buf.toString("base64");
  const targetBuf = Buffer.alloc(size);

  const encodeMs = bench(`encode ${size}`, () => {
    buf.toString("base64");
  }, ITERATIONS);

  const decodeMs = bench(`decode ${size}`, () => {
    Buffer.from(b64str, "base64");
  }, ITERATIONS);

  const writeMs = bench(`write ${size}`, () => {
    targetBuf.write(b64str, "base64");
  }, ITERATIONS);

  const sizeLabel = size >= 1024
    ? `${size / 1024}KB`.padEnd(8)
    : `${size}B`.padEnd(8);
  console.log(
    `${sizeLabel} | ${encodeMs.toFixed(1).padStart(11)} | ${decodeMs.toFixed(1).padStart(11)} | ${writeMs.toFixed(1).padStart(10)}`,
  );
}

// Also test atob/btoa
console.log("\natob/btoa Benchmark (50000 iterations)\n");
const testStr = Buffer.alloc(512, 0x42).toString("base64");
const aMs = bench("btoa", () => btoa("Hello, World!"), 50000);
const bMs = bench("atob", () => atob(testStr), 50000);
console.log(`btoa: ${aMs.toFixed(1)}ms`);
console.log(`atob: ${bMs.toFixed(1)}ms`);

Test plan

  • cargo clippy -p deno_web passes
  • ./tools/format.js passes
  • CI tests

🤖 Generated with Claude Code

bartlomieju and others added 5 commits March 15, 2026 10:04
Replace base64-simd crate with V8's bundled simdutf library for base64
operations. This avoids potential C++ symbol clashes and provides
significant performance improvements, especially for decode and write
operations (2-3x faster than canary at large sizes).

Update v8 crate to 146.7.0 and enable the simdutf feature. Remove
base64-simd dependency from ext/web.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use v8::String::new_external_onebyte for base64 output >8KB to avoid
copying encoded data into V8's heap. V8 takes ownership of the buffer
and frees it on GC, matching Node.js's approach.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use MaybeUninit for stack buffers and Vec::with_capacity for heap
buffers to avoid unnecessary zeroing before simdutf overwrites them.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove indirection through forgivingBase64Encode/Decode/etc wrapper
functions in 00_infra.js and call ops directly from buffer.mjs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR improves base64 encode/decode performance across deno_web and the Node.js Buffer polyfill by switching from the standalone base64-simd crate to V8’s bundled simdutf base64 implementation (via the v8 crate), and by tightening the Rust↔JS call path for hot operations.

Changes:

  • Re-implement deno_web base64 ops using v8::simdutf, including optimized buffer allocation patterns and external V8 string creation for large outputs.
  • Update the Node Buffer polyfill to call the base64 ops directly (instead of going through JS wrapper helpers).
  • Bump the v8 crate and enable the simdutf feature; remove base64-simd from ext/web dependencies.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
ext/web/lib.rs Switch base64 encode/decode ops to v8::simdutf; add external-string path for large base64 encodes.
ext/web/Cargo.toml Drop base64-simd dependency from deno_web.
ext/node/polyfills/internal/buffer.mjs Route base64 Buffer operations directly to core ops for lower overhead.
Cargo.toml Upgrade v8 crate and enable simdutf feature flag.
Cargo.lock Lockfile updates reflecting v8 bump and dependency graph changes.
Comments suppressed due to low confidence (1)

ext/node/polyfills/internal/buffer.mjs:978

  • The comment says op_base64_encode_from_buffer uses v8::String::new_external_onebyte, but the Rust implementation only uses external strings when the base64 output exceeds 8KB; smaller outputs still go through the normal one-byte string path (copying into V8). Consider adjusting the comment to reflect the actual threshold/behavior so it doesn’t mislead future performance work.
    throw new codes.ERR_BUFFER_OUT_OF_BOUNDS("length");
  }

  return blitBuffer(asciiToBytes(string), this, offset, length);

Comment thread ext/web/lib.rs Resolved
Comment thread ext/node/polyfills/internal/buffer.mjs Resolved
Comment thread Cargo.toml Resolved
…mports

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Comment thread ext/web/lib.rs Resolved
bartlomieju and others added 2 commits March 21, 2026 09:53
Address review feedback: creating `&mut [u8]` from uninitialized memory
(via `from_raw_parts_mut` on `Vec::as_mut_ptr()` or `MaybeUninit`) is
undefined behavior even if the consumer only writes.

Fix by re-declaring simdutf FFI functions directly with raw pointer
signatures (`*mut u8` instead of `&mut [u8]`), so we never need to
construct a reference to uninitialized memory. The wrapper
`simdutf_base64_encode` now takes `*mut u8` + length, and
`simdutf_base64_decode_to_vec` calls the FFI function directly.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bartlomieju
bartlomieju merged commit 80121ae into main Mar 23, 2026
219 of 221 checks passed
@bartlomieju
bartlomieju deleted the perf/base64-simdutf branch March 23, 2026 07:53
@ofalvai ofalvai mentioned this pull request Mar 29, 2026
2 of 13 tasks
bartlomieju pushed a commit that referenced this pull request Jun 9, 2026
The nix flake was broken by #32743 which requires a release of
librusty-v8 with simdutf support, and #33294 which requires a newer
version of rust-overlay than the one pinned in flake.lock.
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.

3 participants