Repository navigation
Conversation
toRlp builds hex output straight from hex and byte leaves, falling back to the byte encoder for anything else so errors are unchanged. fromRlp walks hex input as a string with the cursor's bounds errors. Leaves of inputs up to 4096 hex characters, and leaves of at least an eighth of a longer input, are slices of it; other leaves are copied so they do not keep a long input alive. Amp-Thread-ID: https://ampcode.com/threads/T-01a1e1b3-ce83-72d3-b448-f29dd09b6e57 Co-authored-by: jxom <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 61b183c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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 viem@pr-5539
pnpm add [email protected] |
|
Bundled into #5543 as its own commit, so this PR is superseded. Closing in favour of the bundle. |
Speeds up hex RLP and transaction serialize/parse (B5 from the perf audit). Outputs and errors are unchanged.
serializeTransactionandparseTransactiononly calltoRlp/fromRlp, so the transaction files themselves are unchanged.Changes
toRlp(value, 'hex')builds the un-prefixed hex output directly. Hex leaves are lowercased and odd lengths get a leading zero, the same byteshexToByteswould produce. Byte leaves go throughbytesToHex. Anything else, such as invalid hex, a missing0x, numbers or other objects, falls back to the existing byte encoder for the whole value, so it throws the same error. The result is flattened with onejoin.'bytes'output is unchanged.fromRlp(hex, 'hex')walks a valid hex string directly, with the same prefix, length, canonical-form, depth, list-boundary, trailing-byte and cursorPositionOutOfBoundsErrorchecks (sharedreadLength). Invalid hex and'bytes'output use the existing path.join.Bytes.fromHex+Hex.fromBytesfor long inputs, it validates with a regex and copies short leaves, which measured about 3× faster on a 13 KiB list.parseTransaction.bench.tsfailed to load onmainbecause its legacy fixture was signed withyParityand nov; it now usesv: 28n.Benchmarks
SKIP_GLOBAL_SETUP=true ./node_modules/.bin/vitest bench -c ./test/vitest.config.ts --project core --run src/utils/encoding/toRlp.bench.ts src/utils/encoding/fromRlp.bench.ts src/utils/transaction/serializeTransaction.bench.ts src/utils/transaction/parseTransaction.bench.ts,origin/mainvs this branch (ops/s):serializeTransactionlegacyserializeTransactionEIP-1559 ERC-20 transferserializeTransactionEIP-1559 access list + 1 KiB calldataserializeTransactionEIP-7702serializeTransactionEIP-4844, 2 blobsparseTransactionlegacyparseTransactionEIP-1559 ERC-20 transferparseTransactionEIP-1559 access listparseTransactionEIP-7702parseTransactionEIP-4844, 2 blobstoRlphex, 200 × bytes32toRlphex, 128 KiB blobfromRlphex, 40 × bytes32 (short input)fromRlphex, 200 × bytes32 (long input, copies)fromRlphex, 2 × 128 KiB blobsThe existing byte-input and byte-output RLP benches are unchanged, within noise.
Memory (
node --expose-gc):serializeTransactionEIP-4844 wrapperparseTransactionEIP-4844 wrapperserializeTransactionaccess listparseTransactionaccess listtoRlp200 × bytes32mainretained 8 MiB per parsed blob wrapper, and 16× its size for atoRlpoutput, becausebytesToHexoutput stays un-flattened. That primitive is outside this PR.What a kept leaf retains of its input (
rof a parsed blob wrapper, a leaf of a decoded list):parseTransaction(4844 wrapper).rparseTransaction(4844 wrapper).sidecarsfromRlp(200 × bytes32)[0]fromRlp(40 × bytes32)[0]Correctness
origin/main: a scratch harness compared values, error class and message against copies of theorigin/mainmodules. It ran 3 seeds, each with about 6ktoRlptrees (hex and bytes output), 24kfromRlpinputs (hex and bytes output), 510serializeTransactionand 228parseTransactioncases. Everything matched.toRlpleaves: empty,0x0, odd lengths, mixed case,0X, no prefix, trailing space, invalid characters, 55/56/255/256/2047/2048/70000-byte leaves,Uint8Array,Uint16Array,DataView,undefined,null, numbers, booleans and objects.fromRlp: encodings around 4096 characters (each byte length from 2040 to 2050) and the 1/8 ratio, plus truncation, extra bytes, uppercase,0X, no prefix, odd length, invalid characters at the start and end, random byte flips, depth 1023/1024/1100, and non-canonical and out-of-bounds lengths.Rlpfuzz properties were ported to Viem's API, plus differentialtoRlpandfromRlpproperties againstorigin/mainon Ox's arbitraries: larger trees that cross 4096 characters, and changed, dropped, appended, recased or removed-nibble encodings. At 20,000 runs each, all 5 properties passed.fromRlp.test.ts, with snapshots written againstorigin/main: mixed-case and odd-length input, short and long lists on both sides of the threshold (including uppercase), a blob leaf, a truncated long input'sPositionOutOfBoundsError, andtoRlphex leaves in any case or length plus an invalid leaf's error. ThetoRlpcases live here becausetoRlp.test.tsonly runs withTEST_RLP=true.src/utilsandsrc/accountspass against Anvil: 171 files, 1878 tests. The OP Stack, Celo and zkSync serializer and parser tests, which CI'scoreproject excludes, also pass (6 files, 101 tests). So doestoRlp.test.tswithout its 16 MiB cases, which run out of memory onmaintoo.Bundle size (needs a maintainer decision)
Adds about 100–400 B. Measured locally with the same
tscbuild and size-limit:mainimport * from 'viem'(esm)require('viem')(cjs)privateKeyToAccountviem/actionsviem/chainsCI measures about 200–300 B more than this local build. #5525, #5532 and #5533 raise the same limits, so whichever lands later needs another bump.