Skip to content

perf: compute each typed data struct type hash once per call - #5541

Closed
jxom wants to merge 1 commit into
mainfrom
perf/hash-typed-data
Closed

jxom wants to merge 1 commit into
mainfrom
perf/hash-typed-data

Conversation

@jxom

@jxom jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member

Speeds up hashTypedData (B7 from the perf audit). Outputs and errors are unchanged.

Changes

  • hashStruct creates a Map of type hashes and passes it through the internal encodeData / encodeField.
    • Each struct type's encodeType (dependency walk, sort, string build) and its keccak256 now run once per call instead of once per struct value, such as every element of a PermitDetails[].
    • hashTypedData and hashDomain go through hashStruct, so they get it too. No public signature changes.
    • The cache is per call. The audit also measured a cross-call LRU, which only helped the single-struct case and keeps state between calls, so it isn't included.
  • Bench: new src/utils/signature/hashTypedData.bench.ts with EIP-2612 Permit, Permit2 PermitBatch (100 details) and a Seaport order (100 consideration items).

Benchmarks

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

Bench Before After
Permit2 PermitBatch, 100 details 151–174 255–267 1.5–1.7×
Seaport order, 100 consideration items 153–158 202–229 1.3–1.5×
EIP-2612 Permit 5,824–6,216 5,569–6,267 unchanged (noise)

A single Permit has one struct per hash, so nothing repeats. Its time is spent in validateTypedData, encodeAbiParameters and keccak256.

Correctness

  • Equivalence with origin/main: a scratch harness compared hashTypedData, hashStruct and hashDomain values, error class and message against a copy of the origin/main module.
    • Inputs: Permit, Permit2 and Seaport messages; a self-recursive struct (Node[] children); nested and fixed struct arrays (Cell[][], Cell[2]); a struct named uint256[]; fresh random valid instances.
    • Mutations: malformed values, extra or missing fields and types, self-referencing fields, wrong primary types, deleted types and bad domains.
    • 5 seeds × 2,510 inputs, all identical (about 1,300 valid hashes and 1,200 errors per seed).
  • New test: four struct types repeated in arrays, nested fixed arrays and recursively, with the expected hash from Ox's TypedData.getSignPayload (independent implementation).
    • Mutation check: keying the cache by a constant fails it and 8 existing tests.
  • Ox has no TypedData fuzzer to port.
  • Tests run: hashTypedData, verifyTypedData, recoverTypedDataAddress, typedData, signTypedData (account and wallet action), public verifyTypedData and experimental/erc7739 (13 files, 153 tests) pass.
  • tsc on src passes.

Size

At most +137 B locally (viem/account-abstraction), within the current limits. Other perf PRs (#5525, #5532, #5533, #5539, #5540) bump the same limits, so whichever lands later may need a rebase there.

hashStruct passes a map of type hashes through encodeData and
encodeField, so encodeType, the dependency walk, the sort and keccak run
once per struct type instead of once per struct value, such as every
element of a struct array.

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: 017b820

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:25am 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:23:11.137115Z 017b820 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-5541 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5541
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 — 017b8203 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