Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 2288b8f 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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8668bfee5d
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| @@ -1,5 +1,6 @@ | |||
| import { expect, test } from 'vitest' | |||
|
|
|||
| import { stringToBytes } from '../encoding/toBytes.js' | |||
There was a problem hiding this comment.
Import the public test helper through the package entrypoint
The new stringToBytes import bypasses the package entrypoint even though this function is publicly exported from viem. The root AGENTS.md explicitly requires package-entrypoint imports for public surface tests; importing the implementation directly leaves that wiring outside the regression test. Import stringToBytes from 'viem' instead.
AGENTS.md reference: AGENTS.md:L188-L190
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 2288b8f: keccak256.test.ts now imports stringToBytes from viem, and the new Hex/BaseError type imports in toBytes.test.ts come from viem too.
|
Published pnpm add viem@pr-5535
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
hexToBytesdecodes through a 128-entryInt8Arraynibble table and writes straight into the output, including thesizeright-padding, with no intermediateslice/padEndstrings. On a non-hex character it reruns the existing loop, so the thrown error is the same. Unusual sizes (NaN, fractions, negatives) and inputs shorter than 2 characters also take the existing loop.toBytesdecodes0xstrings in one pass instead of running theisHexregex and then decoding. Sized calls and strings that fail to decode take the existingisHexpath, so'0xzz'still encodes as UTF-8.keccak256andnumberToBytesgo through these functions and inherit the change.Native
Uint8Array.fromHex/setFromHexwere measured and lose to the table on V8 at every size (32 B: 2.12M vs 3.46M ops/s; 100 KB: 1,125 vs 1,692), so this does not use them.Behavior
Unchanged for every input of the declared types. A harness compared
mainand this branch on 103,377 calls (hexToBytes,toBytes,fromHex,numberToBytes,keccak256and the rest of the encoding and data utils) on Node 26, Node 24 and Bun 1.4: identical return values and identical thrown errors (class and message). Inputs included every byte value in both cases, odd lengths,'','0x', strings without a0xprefix,0X, characters on either side of each hex range (/ : @ G \g), non-ASCII characters whose low byte looks like a hex digit (İ,ı), whitespace, a bad character at a random position, and thesizeoption including0, negatives, fractions andNaN`.One difference outside realistic use: a
sizeabove V8's maximum string length divided by two (about 256 MiB) used to throwRangeError: Invalid string lengthfrompadEnd. It now allocates the array, or throws the engine's allocationRangeErrorwhen that fails.New tests pin every byte value in mixed case against
Buffer.from(hex, 'hex'), the error message for characters next to the hex ranges, for non-ASCII characters, after uppercase digits and withsize(all of which go through the old loop), the behavior of skipping the first two characters without checking them,toBytesencoding non-hex0xstrings as UTF-8, andkeccak256('0xzz')hashing the UTF-8 text. The snapshots were recorded onmainand pass unchanged here.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/toBytes.bench.ts(random pools are flat strings, like parsed JSON-RPC responses):hexToBytes20 B, random poolhexToBytes32 B, random poolhexToBytes1 KB, random poolhexToBytes100 KB, random poolhexToBytes1 KB (existing bench)hexToBytes20 B,size: 32toBytes32 B hexThe
size: 32row is low because of JIT state left by the benches before it in the same file: in a standalone loop the same call goes 1.51M → 7.86M ops/s.src/utils/hash/keccak256.bench.ts: unchanged within noise (32 B hex 78,664 → 77,056; 1 KB hex 10,947 → 11,334). The hash itself takes about 12µs, so saving a few hundred nanoseconds of decoding does not show. It's included as a baseline for the@noble/hashesquestion.Bytes allocated per call:
hexToBytes32 B 284 → 253,toBytes32 B hex 392 → 304. Larger inputs are dominated by the output array.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: 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.