Skip to content

perf: reuse a TextDecoder and drop per-call allocations in normalizeSignature and formatUnits - #5538

Closed
jxom wants to merge 2 commits into
mainfrom
perf/misc-primitives
Closed

jxom wants to merge 2 commits into
mainfrom
perf/misc-primitives

Conversation

@jxom

@jxom jxom commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

From the sync encoding primitives section of a performance and memory audit. Every change keeps outputs and errors identical; see Behavior.

Summary

Small items from the same audit:

  • bytesToString / hexToString reuse one module-level TextDecoder instead of constructing one per call. decode without { stream: true } keeps no state between calls, and a new test checks that an incomplete UTF-8 sequence doesn't affect the next call.
  • normalizeSignature compares characters directly instead of allocating an array literal and calling .includes for every character.
  • formatUnits (Value.format) trims the fraction's trailing zeros with a char-code loop instead of replace(/(0+)$/, '').

Not changed: the uncached checksumAddress path. Replacing split/join with a char loop measured within noise, because keccak takes most of that path. Skipping TextEncoder for its input helps a little, and that is part of the bytesToHex PR (stringToBytes).

Behavior

Unchanged. A harness compared main and this branch on 103,377 calls on Node 26, Node 24 and Bun 1.4, including formatUnits, formatEther and formatGwei over 117 values (negative, zero, 2n ** 256n - 1n) and decimals 0 to 80 plus invalid ones (-1, 1.5, NaN), 24 signatures for normalizeSignature (nested tuples, extra spaces, tabs, invalid signatures), and bytesToString / hexToString with and without size. Return values and errors were identical.

Benchmarks

SKIP_GLOBAL_SETUP=true vitest bench --project core --run <file>, Node 26.13, same bench file on main and this branch:

Bench main (ops/s) branch (ops/s) ×
normalizeSignature (5-parameter function) 413,479 – 471,650 674,555 – 755,284 1.43 – 1.83
formatUnits(1.23e18, 18) 3,232,127 – 3,688,332 4,762,171 – 4,855,934 1.29 – 1.50
formatUnits(40e18, 18) 3,085,305 – 3,273,922 2,933,352 – 3,395,479 noise (0.6–1.1 across runs)

Two runs each. The orb is shared, and formatUnits(40e18, 18) is the first bench in its file, so it swung widely; the ethers row in the same file moved as much. A standalone loop over three rounds is steadier:

Case main (ops/s) branch (ops/s)
formatUnits(40e18, 18) 4.6M – 4.8M 6.1M – 6.3M
formatUnits(1.23e18, 18) 5.4M – 5.6M 6.9M – 7.7M
formatUnits(123456789n, 6) 6.5M – 7.8M 9.9M – 11.5M
bytesToString 12 B (preallocated input) 4.3M – 5.4M 7.8M – 8.2M

The existing bytesToString bench in fromBytes.bench.ts allocates its input array on every iteration, which hides the change (937K → 975K).

New bench: src/utils/hash/normalizeSignature.bench.ts. formatUnits.bench.ts gained a fractional case.

Size

Entry main this PR all five PRs limit
import * from 'viem' (esm) 91,183 B 91,347 B (+164) 91,881 B (+698) 92 kB
const viem = require('viem') (cjs) 117,921 B 117,992 B (+71) 118,541 B (+620) 118.5 kB → 119 kB
import * from 'viem/account-abstraction' 61,497 B 61,619 B (+122) 62,072 B (+575) 62 kB → 62.5 kB
import { privateKeyToAccount } from 'viem/accounts' 21,696 B 21,716 B (+20) 22,122 B (+426) 22 kB → 22.5 kB
import * from 'viem/actions' 72,912 B 72,948 B (+36) 73,494 B (+582) 73.5 kB → 74 kB

This PR fits the current limits on its own (pnpm size with time measurement off, since there's no Chrome here). All five PRs in this series together go over three limits by 41–122 B and leave viem/actions 6 B under its limit. So every PR in the series raises those four limits by 0.5 kB in a separate chore: commit (precedent: 9677703). The edit is identical in each PR, so git merges them cleanly in any order, and no intermediate merge state breaks pnpm size.

Series: #5534 (bytesToHex, stringToHex, stringToBytes), #5535 (hexToBytes, toBytes), #5536 (number conversions), #5537 (data utils), #5538 (TextDecoder, normalizeSignature, formatUnits). They touch some of the same files in separate hunks; any merge order applies cleanly (checked both orders).

Checks

  • Equivalence harness (main vs branch): 103,377 calls, 0 differences on Node 26, Node 24 and Bun.
  • pnpm test --project core --run src/utils/encoding src/utils/hash src/utils/unit: passing.
  • src/utils, src/accounts, src/siwe: the only failures are the Anvil-dependent tests that fail identically on main here (no Anvil in this environment).
  • Line and branch coverage of the changed modules on Node 26 is the same as on main: full, apart from two branches that were already uncovered (slice.ts L67, Value.ts L29).
  • Biome clean; tsc clean for src and the changed test and bench files.
  • pnpm knip --production clean on the five PRs merged together.

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

Request Review

@changeset-bot

changeset-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cb08f02

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-16T01:59:32.592013Z cb08f02 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-5538 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5538
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 — cb08f029 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