Skip to content

perf: assign log args and eventName instead of spreading them - #5542

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

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

Conversation

@jxom

@jxom jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member

Speeds up formatLog (B8 from the perf audit). Outputs and errors are unchanged.

Changes

  • formatLog builds the formatted log once. It assigns args and eventName only when eventName is set, instead of spreading { args, eventName } (or {}) into the literal.
    • An input that already has its own args / eventName keys keeps them in the same position, and their values are overwritten, as with the spread.
    • An empty eventName adds nothing.
  • Bench: new src/utils/formatters/log.bench.ts:
    • 1,000 logs without an event name (the getLogs / getFilterLogs path)
    • 1,000 logs with an event name and args (the watchEvent path)
    • formatTransactionReceipt with 500 logs

Benchmarks

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

Bench Before After
1,000 logs 4,182–4,252 4,375–4,555 1.05–1.07×
1,000 logs with event name and args 3,228–3,342 3,757–3,866 1.16×
formatTransactionReceipt, 500 logs 7,234–8,020 7,965–8,619 1.07–1.10×

The audit's 1.25× also replaced the map in formatTransactionReceipt with a loop. This PR keeps to formatLog. The rest of the time goes to the ...log spread itself, which must stay to keep unknown fields and their order.

Correctness

  • Equivalence with origin/main: a scratch harness compared own keys in order (Reflect.ownKeys, so symbol keys too), values, and errors against a copy of the origin/main module.
    • Inputs had random subsets and orders of the log fields plus args, eventName and unknown keys, with malformed values (symbols, bigints, objects, invalid hex), an own __proto__ key, and every args/eventName option combination.
    • 4 seeds × 20,000 inputs, all identical.
  • New test: an input with its own eventName key, formatted with and without an event name. The snapshot (key order included) was written against origin/main.
  • Tests run: src/utils/formatters, getLogs, getFilterLogs, getFilterChanges, watchEvent, watchContractEvent, parseEventLogs and getTransactionReceipt pass (15 files, 218 tests).
  • tsc on src passes.

Size

Within the current limits. Other perf PRs (#5525, #5532, #5533, #5539, #5540, #5541) bump the same limits, so whichever lands later may need a rebase there.

formatLog builds the formatted log once and assigns args and eventName
only when there is an event name, instead of spreading a temporary
object (or an empty one) into the result. Own args and eventName keys
on the input keep their position, as before.

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

changeset-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e942535

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

@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:34am UTC

Request Review

@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:32:52.728366Z e942535 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-5542 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5542
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 — e942535a 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