Repository navigation
perf: speed up hot paths and cut allocations - #686
Conversation
`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>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a Co-authored-by: jxom <jake@tempo.xyz>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a Co-authored-by: jxom <jake@tempo.xyz>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a Co-authored-by: jxom <jake@tempo.xyz>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a Co-authored-by: jxom <jake@tempo.xyz>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a Co-authored-by: jxom <jake@tempo.xyz>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e11f-67e9-7006-9b0c-2f21fcb49e9a Co-authored-by: jxom <jake@tempo.xyz>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Published 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>
There was a problem hiding this comment.
💡 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".
| */ | ||
| export function validate(value: unknown): value is Bytes { | ||
| // The checks `assert` makes, without building and catching an error. | ||
| if (value instanceof Uint8Array) return true |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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>
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%.
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 every0x80byte, which is quadratic.0x80: 67 → 5,758 (86x)Blobs.from: byte loops instead of a cursor plus a slice per field elementAbiParameters.decode: skip the array regex for non-array types,uint256shortcut, address view instead of copy, cursor read tracking via bit set instead of Mapuint256[1000]: 2,198 → 7,523 (3.4x);address[100]: 20.0k → 32.4k (1.6x);AbiFunction.decodeData: 1.33–1.36xBytes.toBigInt: values with ≤6 significant bytes built as a numberTypedData: type hashes cached (bounded, 1,024 entries, cleared withCaches.clearand on engine changes)getSignPayload: 7.6k → 9.9k (1.3–1.5x);hashStruct: 1.37xBase58: nine digits per BigInt step (58^9 < 2^53)fromBytes256 B: 11.8k → 64.5k (5.5x);toBytes256 B: 20.4k → 58.3k (2.9x)Hex.validate/Bytes.validate: same checks asassert, without building and catching an errorVirtualMaster.mineSaltAsyncpool: cache the in-flight promise, import in parallelBundle size (esbuild, min+gzip):
Blobs−931 B,AbiParameters.decode−26 B,Hex.validate1,147 → 175 B,Bytes.validate928 → 169 B.Bytes.toBigInt, Cbor, Address, VirtualMaster.Worth a reviewer's attention
internal/cursor.ts):recursiveReadLimitonly ever used "count > 0", so a bit set for 32-byte-aligned positions (len / 256bytes) plus aSetfor 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 internalgetReadCountis removed.Blobs.toon malformed input: blobs whose length is one more than a multiple of 32 used to throwCursor.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
ox/wasmexists.Uint8Array.fromHex: the documented Chromium masking bug.Testing
tsc -b, format and lint are clean.coreandtempo-unitsuites have the same 33 failures asmain, all anvil fork tests that couldn't reach a fork RPC; everything else passes.FC_NUM_RUNS=1000) andpnpm vectors(18,736) pass.