Repository navigation
perf: harden and speed up ABI, RLP, Hex and transport codecs - #689
Conversation
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e175-c7ad-72d8-aae0-552ef56ffbaa Co-authored-by: jxom <jake@tempo.xyz>
`AbiEvent.decodeLog` formatted and normalized the signature of every event in the ABI on every log. The hash is now cached per item in a clearable weak cache, which `Caches.clear` and engine changes reset. Amp-Thread-ID: https://ampcode.com/threads/T-01a1e175-c7ad-72d8-aae0-552ef56ffbaa Co-authored-by: jxom <jake@tempo.xyz>
- Bound decode reads by twice the data size plus the repeated-read allowance. - Require zero padding for `bytesN` words and topics, one-word value topics, and hex characters in `address` (`checksumAddress: false`) and `bytesN` topics. - Throw `Hex.InvalidHexValueError` for invalid hex in `AbiParameters.encode` instead of patching it into the output, and write hex in one pass. - Resolve array element decoders once, track repeated reads with a bitset, create the cursor's `DataView` lazily, and skip leading zero bytes when reading integers. Amp-Thread-ID: https://ampcode.com/threads/T-01a1e175-c7ad-72d8-aae0-552ef56ffbaa Co-authored-by: jxom <jake@tempo.xyz>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e175-c7ad-72d8-aae0-552ef56ffbaa Co-authored-by: jxom <jake@tempo.xyz>
Short hex inputs are read straight from the string and their leaves sliced out of it. Longer inputs slice only leaves large enough that keeping one retains little of the input. Amp-Thread-ID: https://ampcode.com/threads/T-01a1e175-c7ad-72d8-aae0-552ef56ffbaa Co-authored-by: jxom <jake@tempo.xyz>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e175-c7ad-72d8-aae0-552ef56ffbaa Co-authored-by: jxom <jake@tempo.xyz>
Also builds the timeout error only on timeout, and prints a string request body in `HttpError` without JSON-encoding it again. Amp-Thread-ID: https://ampcode.com/threads/T-01a1e175-c7ad-72d8-aae0-552ef56ffbaa Co-authored-by: jxom <jake@tempo.xyz>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e175-c7ad-72d8-aae0-552ef56ffbaa 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b8206a61a2
ℹ️ 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 class WeakCache<key extends object, value> { | ||
| map = new WeakMap<key, value>() |
There was a problem hiding this comment.
Implement the weak cache with functions and plain data
The new WeakCache introduces a non-error class, contrary to the explicit “Classes for errors only” convention in AGENTS.md. Implement this cache as a factory returning plain data and functions instead; its backing WeakMap can still be replaced by clear(). The allowance for global memoization does not exempt cache implementations from the class convention.
AGENTS.md reference: AGENTS.md:L15-L15
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 414ab22: WeakCache is now a weakCache() factory. It returns plain clear/get/set functions, and clear swaps the WeakMap held in the closure.
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e175-c7ad-72d8-aae0-552ef56ffbaa Co-authored-by: jxom <jake@tempo.xyz>
|
Published pnpm add ox@pr-689
pnpm add ox@0.0.0-pr-689-136340a |
… keys in receipts
Ports the performance, memory and security findings from a Viem audit, mapped onto ox
mainat 7421689. Each intended behaviour change has a changeset.Security and correctness
32 * (bytes.length + recursiveReadLimit), so a 66,688-bytebytes[]payload with 33 offsets pointing at one 64 KiB tail decoded to 64.9× its size. It is now2 * bytes.length + 32 * recursiveReadLimit, and that payload throwsCursor.ReadSizeLimitExceededError.bytesNwords and topics need zero padding (AbiParameters.BytesSizeMismatchError).Hex.SizeOverflowError.addresstopics decoded withchecksumAddress: falseare validated, and throw the sameAddress.InvalidAddressErroras checksummed decoding.bytesNtopics with non-hex characters throwHex.InvalidHexValueError.AbiParameters.encode: it throwsHex.InvalidHexValueErrorinstead of patching the raw characters into the output. That patch path was also quadratic.Transaction.fromRpcandTransactionReceipt.fromRpcmap types and statuses by own keys only, soconstructor,toStringand__proto__no longer resolve toObject.prototypemembers.Cbor.decodedepth: it throwsCbor.DepthLimitExceededErrorpast 1024 nesting levels instead of overflowing the stack.RpcTransport.fromHttptimeouts: it rejects on timeout even when a customfetchFnignores the abort signal.HttpErrorno longer JSON-encodes a body that is already a string.Performance
vitest bench,main→ this PR, Node 26.13.1. Microbenchmarks here vary ±20–30% between runs, so the encode/decode rows were rechecked by running both versions interleaved in one process (in brackets).AbiEvent.decodeLog, 500 ERC-20 logsAbiEvent.decodeLog, 500 logs, 32-event ABIAbiItem.fromAbiby selector, 200-item ABIAbiFunction.encodeData, plain ABI itemaddress[1000]uint256[1000]string[100]Bytes.toNumber, 32-byte word with a small valueBytes.toBigInt, signedHex.concat, 8 argsHex.fromNumber, bigint, size 32Hex.toBigInt, signedRlp.toHex, legacy / EIP-1559 / EIP-7702 txRlp.toHex, EIP-4844 with 3 blobsCbor.decode, medium objectRpcTransportrequestHow:
Caches.clearand every engine change reset.DataViewis created lazily;main; naive slicing retained 4.4 MB.TimeoutError(about 3 µs with its stack trace) is built only on timeout.Also removed the unused
LruMapanduid.Caveat
An ABI item mutated after it was first hashed keeps its old hash until
Caches.clear()or an engine change.Testing
main.FC_NUM_RUNS=2000(AbiParameters ×3, AbiEvent, Rlp, Hex, Cbor): pass. A new Rlp property checks that the string and byte decoders agree on mutated encodings, errors included.main; those two guard the new code paths.tsc -bpasses at every commit.mainat 7421689.Deferred: a per-ABI selector index;
uintN/intNtopics with non-hex characters still throwBigInt'sSyntaxError, as before.