Repository navigation
Conversation
…ignature and formatUnits Amp-Thread-ID: https://ampcode.com/threads/T-01a1e1b3-c54c-70a6-8130-edc1ec203821 Co-authored-by: jxom <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: cb08f02 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-5538
pnpm add [email protected] |
|
Bundled into #5543 as its own commit, so this PR is superseded. Closing in favour of the bundle. |
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/hexToStringreuse one module-levelTextDecoderinstead of constructing one per call.decodewithout{ stream: true }keeps no state between calls, and a new test checks that an incomplete UTF-8 sequence doesn't affect the next call.normalizeSignaturecompares characters directly instead of allocating an array literal and calling.includesfor every character.formatUnits(Value.format) trims the fraction's trailing zeros with a char-code loop instead ofreplace(/(0+)$/, '').Not changed: the uncached
checksumAddresspath. Replacingsplit/joinwith a char loop measured within noise, because keccak takes most of that path. SkippingTextEncoderfor its input helps a little, and that is part of thebytesToHexPR (stringToBytes).Behavior
Unchanged. A harness compared
mainand this branch on 103,377 calls on Node 26, Node 24 and Bun 1.4, includingformatUnits,formatEtherandformatGweiover 117 values (negative, zero,2n ** 256n - 1n) and decimals0to80plus invalid ones (-1,1.5,NaN), 24 signatures fornormalizeSignature(nested tuples, extra spaces, tabs, invalid signatures), andbytesToString/hexToStringwith and withoutsize. Return values and errors were identical.Benchmarks
SKIP_GLOBAL_SETUP=true vitest bench --project core --run <file>, Node 26.13, same bench file onmainand this branch:normalizeSignature(5-parameter function)formatUnits(1.23e18, 18)formatUnits(40e18, 18)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:formatUnits(40e18, 18)formatUnits(1.23e18, 18)formatUnits(123456789n, 6)bytesToString12 B (preallocated input)The existing
bytesToStringbench infromBytes.bench.tsallocates its input array on every iteration, which hides the change (937K → 975K).New bench:
src/utils/hash/normalizeSignature.bench.ts.formatUnits.bench.tsgained a fractional case.Size
import * from 'viem' (esm)const viem = require('viem') (cjs)import * from 'viem/account-abstraction'import { privateKeyToAccount } from 'viem/accounts'import * from 'viem/actions'This PR fits the current limits on its own (
pnpm sizewith 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 leaveviem/actions6 B under its limit. So every PR in the series raises those four limits by 0.5 kB in a separatechore:commit (precedent: 9677703). The edit is identical in each PR, so git merges them cleanly in any order, and no intermediate merge state breakspnpm 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
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 onmainhere (no Anvil in this environment).main: full, apart from two branches that were already uncovered (slice.tsL67,Value.tsL29).tscclean forsrcand the changed test and bench files.pnpm knip --productionclean on the five PRs merged together.