Repository navigation
Conversation
🦋 Changeset detectedLatest commit: 29b3a77 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 |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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-5534
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
bytesToHexuses the nativeUint8Array.prototype.toHex(Node 25+, Chrome 140+, Firefox 133+, Safari 18.2+), found once at module load. Other runtimes, plain arrays, cross-realm arrays and empty views keep the existing table loop. Empty views matter: a detached or out-of-bounds view has length 0 and nativetoHexthrows on it, while the loop returns0x.stringToBytesskipsTextEncoderfor ASCII strings of up to 64 characters, since their char codes are already the UTF-8 bytes.TextEncoder.encodecosts about 1µs per call in V8, and arrays of up to 64 bytes are allocated on the heap. Longer or non-ASCII strings go throughTextEncoderas before.stringToHexis nowbytesToHex(stringToBytes(value), opts), so it gets both changes.toHex.tsnow imports fromtoBytes.ts, which already imports fromtoHex.ts; the cycle is safe because neither module calls the other at load time, andfromHex.tsandtoBytes.tsalready import each other the same way.Output is also a flat string instead of a rope: a 32-byte hash now retains about 120 bytes instead of about 1 KB.
Behavior
Unchanged. A harness compared
mainand this branch on 103,377 calls acrossbytesToHex,stringToHex,stringToBytes,toHex,toBytes,keccak256,checksumAddressand the rest of the encoding and data utils, on Node 26 (nativetoHex), Node 24 (no nativetoHex) and Bun 1.4. Return values (including theUint8Arrayconstructor) and thrown errors (class and message) were identical in every case. Inputs included every byte value, subarray views,Buffer, detached and out-of-bounds views, length-tracking views, plain arrays, lone surrogates, non-ASCII at the 64-character boundary, and thesizeoption.New tests pin the cases a plausible regression would break: every byte value, views and
Bufferagainst an independent encoding, a detached buffer (fails if the length guard is removed), and ASCII and non-ASCII strings around 64 characters againstBuffer.from(value, 'utf8').CI runs Node 24, which has no native
toHex, so CI covers only the fallback branch. I ran the encoding, data, hash and address tests on Node 26 as well.Benchmarks
SKIP_GLOBAL_SETUP=true vitest bench --project core --run <file>, Node 26.13, same bench file onmainand this branch. The orb is shared, so expect ±10–20% noise.src/utils/encoding/toHex.bench.ts:bytesToHex20 B, random poolbytesToHex32 B, random poolbytesToHex1 KB, random poolbytesToHex100 KB, random poolbytesToHex1 KB (existing bench)stringToHex('Hello world.')stringToHex, 64 charsstringToHex, 200 charsstringToHex, 10K charsstringToHex, 12 chars non-ASCIIsrc/utils/address/getAddress.bench.ts, uncached checksum (20,000 distinct addresses, more than the 8,192-entry cache): 46,040 → 50,739 (1.10×). keccak dominates this path, so thestringToBytessaving barely shows.stringToBytes(standalone loop, not in a bench file, to keep this PR's bench edits away from thehexToBytesPR):'Hello world.''transfer(address,uint256)'Node 24 (no native
toHex, so only the ASCII change applies):bytesToHexstays within noise (0.99–1.03× on the random pools),stringToHex('Hello world.')is 2.45× and 64 chars 1.53×, and the 10K-char and non-ASCII cases are unchanged (1.08×, 0.94×).Bytes allocated per call (
node --expose-gc, large young generation):bytesToHex32 BbytesToHex1 KBbytesToHex100 KBstringToHex('Hello world.')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/data src/utils/hash src/utils/address src/utils/unit: passing on Node 26.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). On Node 24, which CI runs, the nativetoHexbranch can't run, so it shows as uncovered there; the fallback loop is covered on both through aUint8Arrayfrom another realm (node:vm).tscclean forsrcand the changed test and bench files.pnpm knip --productionclean on the five PRs merged together.