Skip to content

perf: speed up hot paths and cut allocations - #686

Merged
jxom merged 11 commits into
mainfrom
perf/hot-paths
Nov 16, 2026
Merged

jxom merged 11 commits into
mainfrom
perf/hot-paths

Conversation

@jxom

@jxom jxom commented Nov 15, 2026

Copy link
Copy Markdown
Member

Speeds up several sync hot paths and cuts their allocations. The one async waterfall I found is fixed too. Each change was measured before and after with pnpm bench (HEAD vs. this branch), and benches were added where none covered the path.

Speed is ops/sec (hz) before → after. Memory is bytes allocated per call, measured with GC held off. On the runner used, runs of unchanged code vary by ±10–20%.

Change Bench (hz) Memory per call
Blobs.to: find the terminator once, copy with byte loops into an exactly sized array. It used to rescan the rest of the blob for every 0x80 byte, which is quadratic. 6 blobs: 17.5 → 838 (48x); 100 KB of 0x80: 67 → 5,758 (86x) 45.8 MB → 762 KB; 18.1 MB → 101 KB
Blobs.from: byte loops instead of a cursor plus a slice per field element 6 blobs: 758 → 1,649 (2.2–2.9x) 6.9 MB → 788 KB
AbiParameters.decode: skip the array regex for non-array types, uint256 shortcut, address view instead of copy, cursor read tracking via bit set instead of Map uint256[1000]: 2,198 → 7,523 (3.4x); address[100]: 20.0k → 32.4k (1.6x); AbiFunction.decodeData: 1.33–1.36x 626 → 328 KB; 155 → 132 KB
Bytes.toBigInt: values with ≤6 significant bytes built as a number small: 6.5M → 9.7M; zero: 6.8M → 12.9M; full 256-bit unchanged 1,000 small values: 176 → 24 KB
TypedData: type hashes cached (bounded, 1,024 entries, cleared with Caches.clear and on engine changes) getSignPayload: 7.6k → 9.9k (1.3–1.5x); hashStruct: 1.37x 57.3 → 48.7 KB
Base58: nine digits per BigInt step (58^9 < 2^53) fromBytes 256 B: 11.8k → 64.5k (5.5x); toBytes 256 B: 20.4k → 58.3k (2.9x) 2–7x less
Hex.validate / Bytes.validate: same checks as assert, without building and catching an error invalid input: 37–49x; valid input unchanged 2.3 KB → ≤105 B
VirtualMaster.mineSaltAsync pool: cache the in-flight promise, import in parallel 8 concurrent cold calls: 8 pools → 1, 6–11 ms → ~2 ms no leaked Blob URLs in browsers

Bundle size (esbuild, min+gzip):

  • Smaller: Blobs −931 B, AbiParameters.decode −26 B, Hex.validate 1,147 → 175 B, Bytes.validate 928 → 169 B.
  • Larger by 8–93 B: TypedData, Base58, Bytes.toBigInt, Cbor, Address, VirtualMaster.

Worth a reviewer's attention

  • Cursor read tracking (internal/cursor.ts): recursiveReadLimit only ever used "count > 0", so a bit set for 32-byte-aligned positions (len / 256 bytes) plus a Set for unaligned positions gives the same results as the Map. A differential test over 3,000 random read sequences against the old cursor matched exactly. Worst case for memory, a sparse 1 MB decode, costs +3.9 KB; a bit per byte would have cost +250 KB. The unused internal getReadCount is removed.
  • Blobs.to on malformed input: blobs whose length is one more than a multiple of 32 used to throw Cursor.PositionOutOfBoundsError, an accident of the cursor bounds, since every other odd length was accepted. They now decode like any other length. Every other input gives identical output to the old implementation, checked across edge sizes and 300+ malformed blob sets.

Rejected

  • A faster hand-written keccak. Noble keccak is now the main remaining cost, but replacing an audited primitive isn't worth it when ox/wasm exists.
  • Reusing noble hash instances: relies on noble internals and would keep possibly secret state in a shared buffer.
  • Native Uint8Array.fromHex: the documented Chromium masking bug.
  • Returning views from public APIs: would alias caller buffers.

Testing

  • tsc -b, format and lint are clean.
  • The core and tempo-unit suites have the same 33 failures as main, all anvil fork tests that couldn't reach a fork RPC; everything else passes.
  • Fuzz (ABI, Cbor, Hex, Base; FC_NUM_RUNS=1000) and pnpm vectors (18,736) pass.
  • Added a cursor test for repeated reads at an unaligned position.

ampagent and others added 8 commits November 15, 2026 22:52
`Blobs.to` rescanned the rest of the blob for every 0x80 byte and
recorded every read position in a cursor Map. It now finds the terminator
once and copies into an exactly sized array. `Blobs.from` no longer
slices each field element.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a
Co-authored-by: jxom <jake@tempo.xyz>
- `getArrayComponents` skips its regex for types not ending in `]`.
- `decodeNumber` skips type parsing for `uint256`.
- `decodeAddress` hex-encodes a transient view instead of a copy.
- The cursor tracks read positions in a bit set (one bit per 32-byte
  word) plus a set for unaligned positions, instead of a Map.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a
Co-authored-by: jxom <jake@tempo.xyz>
@vercel

vercel Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
ox Error Error Nov 16, 2026 1:39am UTC

Request Review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T01:37:35.695608Z 107035e New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Published ox@0.0.0-pr-686-d0f53fe under the pr-686 dist-tag, which follows this pull request until it closes.

pnpm add ox@pr-686
pnpm add ox@0.0.0-pr-686-d0f53fe

A Proxy or getter that throws while `Bytes.validate` reads
`BYTES_PER_ELEMENT` or the constructor name made it throw instead of
returning `false` as before.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a
Co-authored-by: jxom <jake@tempo.xyz>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e7489403e5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/core/Bytes.ts Outdated
*/
export function validate(value: unknown): value is Bytes {
// The checks `assert` makes, without building and catching an error.
if (value instanceof Uint8Array) return true

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep the instanceof check inside the validation try block

When value is a revoked proxy or a proxy whose getPrototypeOf trap throws, value instanceof Uint8Array throws before reaching the try block. Consequently, Bytes.validate(value) now propagates an exception instead of returning false as it did before this change. Move this check inside the existing try block to preserve the validator's behavior for these inputs.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in d0f53fe: the typeof check now runs first, and instanceof moved inside the try block, so a revoked Proxy or a throwing getPrototypeOf trap returns false as before. Results match the pre-PR validate on 35 edge values, including both of those. Valid input is unchanged (~17M ops/s) and invalid strings stay ~50x faster.

`instanceof` throws on a revoked Proxy or a throwing `getPrototypeOf`
trap, which made `Bytes.validate` throw instead of returning `false`.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a
Co-authored-by: jxom <jake@tempo.xyz>
@jxom
jxom merged commit 93361ec into main Nov 16, 2026
14 of 15 checks passed
@jxom
jxom deleted the perf/hot-paths branch November 16, 2026 01:45
@github-actions github-actions Bot mentioned this pull request Nov 16, 2026

This branch had an error being deployed

1 failed deployment
Preview — 107035e8 Deployed Nov 16, 2026 by vercel[bot]
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.

2 participants