Skip to content

perf: format block transactions once and build transactions without delete - #5540

Closed
jxom wants to merge 1 commit into
mainfrom
perf/format-block
Closed

jxom wants to merge 1 commit into
mainfrom
perf/format-block

Conversation

@jxom

@jxom jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member

Speeds up block and transaction formatting (B4 from the perf audit). Outputs and errors are unchanged.

Changes

  • formatTransaction no longer uses delete. Deleting properties pushed every legacy, EIP-2930 and EIP-1559 result into dictionary mode. Fields a type doesn't have are now left out with object rest destructuring, and the result is built as one object literal.
    • Key order is unchanged for every type, including an own __proto__ key.
    • Fee fields are still parsed for every type before they are dropped, so a malformed maxFeePerBlobGas on a legacy transaction still throws.
    • yParity is only assigned for non-legacy types.
    • V8 %HasFastProperties is now true for the results of every type. On main it was false for legacy, 2930 and 1559.
  • OP Stack, Celo and zkSync block formatters formatted every transaction twice: once in formatBlock and again in the chain override. They now follow src/tempo/Formatters.ts.
    • Each transaction is formatted once.
    • formatBlock gets transactions: [].
    • The chain extras are applied after that.
    • Errors are thrown in the same order: transactions first, then the block fields.
    • zkSync's transaction override is now a named formatZksyncTransaction, shared by defineTransaction and the block formatter.
    • The block formatters are module constants with the same { exclude: undefined, format, type: 'block' } shape defineBlock returns, so extractChain / client resolver snapshots are unchanged.
  • Bench: new src/utils/formatters/block.bench.ts with deterministic 300-transaction blocks for formatBlock and each chain, plus formatTransaction per type.

Benchmarks

SKIP_GLOBAL_SETUP=true ./node_modules/.bin/vitest bench -c ./test/vitest.config.ts --project core --run src/utils/formatters/block.bench.ts, origin/main vs this branch (ops/s):

Bench Before After
formatBlock, 300 txs 678 1,213 1.8×
OP Stack block, 300 txs 442 1,203 2.7×
Celo block, 300 txs 260 1,103 4.2×
zkSync block, 300 txs 298 1,128 3.8×
formatTransaction legacy 247k 350k 1.4×
formatTransaction EIP-1559 508k 533k 1.05×

Memory, 300-tx block (node --expose-gc):

Case Allocated before → after Retained before → after
formatBlock 632 → 535 KiB 512 → 114 KiB
OP Stack block 1.29 MiB → 539 KiB 512 → 114 KiB

Retention drops because fast-properties objects are much smaller than dictionary-mode ones.

Correctness

  • Equivalence with origin/main: a scratch harness compared values, key order, error class and message against copies of the origin/main modules. It covered formatTransaction, formatBlock, and the OP Stack, Celo and zkSync block and transaction formatters. Inputs included every type, missing and malformed fields, own __proto__ keys, symbol keys and shuffled key order. 5 seeds, all 8 comparisons identical.
  • New tests in transaction.test.ts, with snapshots written against origin/main:
    • key order and dropped fields per type
    • malformed fee fields still throwing on legacy
    • an own __proto__ key
  • Tests run:
    • src/utils/formatters, getBlock, getTransaction, src/utils/chain, createClientResolver and src/zksync pass (75 files, 261 tests).
    • The OP Stack and Celo formatter tests aren't in the core project. I ran them with a temporary config and they fail identically on origin/main: the OP Stack ones because of fork snapshots with stale blockTimestamp, and Celo transaction > action because the live Celo RPC now returns chainId: null for that transaction.
  • tsc on src passes, and so do the touched tests and bench.

Size

Local size-limit deltas are small, at most +140 B on viem/chains, and stay under the current limits. Other perf PRs (#5525, #5532, #5533, #5539) bump the same limits, so whichever lands later may need a rebase there.

…elete

formatTransaction leaves fields a transaction type does not have out of
the copy instead of deleting them, so results stay fast-properties
objects. Fee fields are still parsed for every type, so errors are
unchanged. The OP Stack, Celo, and zkSync block formatters format each
transaction once and pass formatBlock an empty list, in the same order
of errors as before.

Co-authored-by: jxom <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e1b3-ce83-72d3-b448-f29dd09b6e57
@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
viem Error Error Nov 16, 2026 3:12am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 40b621a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 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-16T03:10:12.001353Z 40b621a PR opened
ℹ️ 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

Copy link
Copy Markdown
Contributor

Published [email protected] under the pr-5540 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5540
pnpm add [email protected]

jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Bundled into #5543 as its own commit, so this PR is superseded. Closing in favour of the bundle.

@jxom jxom closed this Nov 16, 2026

This branch had an error being deployed

1 failed deployment
Preview — 40b621af 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