Skip to content

perf(ext/crypto): batch randomUUID generation#35953

Merged
nathanwhit merged 1 commit into
denoland:mainfrom
nathanwhit:agent/optimize-random-uuid
Jul 13, 2026
Merged

perf(ext/crypto): batch randomUUID generation#35953
nathanwhit merged 1 commit into
denoland:mainfrom
nathanwhit:agent/optimize-random-uuid

Conversation

@nathanwhit

Copy link
Copy Markdown
Member

Summary

  • generate 128 formatted UUIDs per native refill and return them as one packed one-byte V8 string
  • serve normal crypto.randomUUID() calls from JavaScript slices, reducing native transitions to one per batch
  • retain the native per-call implementation for --seed so mixed randomUUID() and getRandomValues() calls preserve deterministic RNG ordering
  • honor node:crypto.randomUUID({ disableEntropyCache: true }) with an uncached generation path

Performance

Focused release-lite benchmark using the website benchmark helper:

Runtime ops/sec
Deno website baseline 9.50M
Deno patched 16.60M-17.12M
Node 26.3.0, same local harness 16.97M

The implementation uses the same 128-UUID batch size as Node, but formats the whole batch in Rust into one packed string. The JavaScript hot path only slices the next 36-byte UUID, avoiding both a native call and per-byte formatting for 127 of every 128 calls.

Validation

  • cargo check -p deno_crypto
  • cargo test -p deno_crypto test_fast_uuid_v4_correctness
  • cargo fmt --check
  • ./tools/lint.js ext/crypto/00_crypto.js ext/crypto/crypto.rs ext/crypto/lib.rs ext/node/polyfills/internal/crypto/random.ts tests/unit_node/crypto/crypto_misc_test.ts
  • ./target/release-lite/deno test --config tests/config/deno.json tests/unit_node/crypto/crypto_misc_test.ts
  • PYENV_VERSION=3.11.8 ./tests/wpt/wpt.ts run --binary=./target/release-lite/deno -- WebCryptoAPI/randomUUID
  • generated 1,000 UUIDs across multiple refills and checked UUID v4 shape and uniqueness
  • repeated mixed randomUUID() / getRandomValues() calls under --seed=100 in separate processes and confirmed identical output

@nathanwhit
nathanwhit marked this pull request as ready for review July 10, 2026 20:47
@bartlomieju bartlomieju changed the title perf(crypto): batch randomUUID generation perf(ext/crypto): batch randomUUID generation Jul 13, 2026
@nathanwhit
nathanwhit merged commit 2c7aa39 into denoland:main Jul 13, 2026
173 checks passed
bartlomieju pushed a commit that referenced this pull request Jul 15, 2026
## Summary

- generate 128 formatted UUIDs per native refill and return them as one
packed one-byte V8 string
- serve normal `crypto.randomUUID()` calls from JavaScript slices,
reducing native transitions to one per batch
- retain the native per-call implementation for `--seed` so mixed
`randomUUID()` and `getRandomValues()` calls preserve deterministic RNG
ordering
- honor `node:crypto.randomUUID({ disableEntropyCache: true })` with an
uncached generation path

## Performance

Focused `release-lite` benchmark using the website benchmark helper:

| Runtime | ops/sec |
| --- | ---: |
| Deno website baseline | 9.50M |
| Deno patched | 16.60M-17.12M |
| Node 26.3.0, same local harness | 16.97M |

The implementation uses the same 128-UUID batch size as Node, but
formats the whole batch in Rust into one packed string. The JavaScript
hot path only slices the next 36-byte UUID, avoiding both a native call
and per-byte formatting for 127 of every 128 calls.

## Validation

- `cargo check -p deno_crypto`
- `cargo test -p deno_crypto test_fast_uuid_v4_correctness`
- `cargo fmt --check`
- `./tools/lint.js ext/crypto/00_crypto.js ext/crypto/crypto.rs
ext/crypto/lib.rs ext/node/polyfills/internal/crypto/random.ts
tests/unit_node/crypto/crypto_misc_test.ts`
- `./target/release-lite/deno test --config tests/config/deno.json
tests/unit_node/crypto/crypto_misc_test.ts`
- `PYENV_VERSION=3.11.8 ./tests/wpt/wpt.ts run
--binary=./target/release-lite/deno -- WebCryptoAPI/randomUUID`
- generated 1,000 UUIDs across multiple refills and checked UUID v4
shape and uniqueness
- repeated mixed `randomUUID()` / `getRandomValues()` calls under
`--seed=100` in separate processes and confirmed identical output
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.

1 participant