Repository navigation
Conversation
…oNumber and bytesToBigInt 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: eb36113 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: dee4a5aedc
ℹ️ 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".
| import type { BaseError } from '../../errors/base.js' | ||
| import type { Hex } from '../../types/misc.js' |
There was a problem hiding this comment.
Import public test types through the package entrypoint
Root AGENTS.md explicitly requires tests to import public APIs through package entrypoints rather than relative source paths. Both BaseError and Hex are public exports, so these newly added imports violate that repository convention. Import them from 'viem' instead, and apply the same correction to the new BaseError imports in fromBytes.test.ts and toHex.test.ts.
AGENTS.md reference: AGENTS.md:L188-L190
Useful? React with 👍 / 👎.
|
Published pnpm add viem@pr-5536
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
Fast paths for the common cases. Each one falls through to the existing code for anything it does not handle, so errors come from the same code as before.
numberToHex: an unsigned safe integer or non-negative bigint fitssizebytes exactly when its hex has at mostsize * 2digits. The fast path checks that withtoString(16).padStart, without the bigint bound math. Signed values, out-of-range values and unusual sizes take the existing path.hexToNumber: when unsigned, unsized and0x-prefixed,Number(hex)parses it directly, and a safe-integer result is exact. Anything else, including unsafe results, goes throughhexToBigIntand the existing range error.hexToBigInt(signed, negative):BigInt.asIntNinstead of building anf…fstring and parsing it. This applies only to0xinput, which always fitssizebytes. Input without the prefix is parsed as decimal byBigInt, so it keeps the old arithmetic.bytesToNumber/bytesToBigInt: when unsigned and non-empty, skip leading zero bytes and, with up to 6 significant bytes (always a safe integer), accumulate the value directly instead of going through hex. This is the ABI offset and length word case.Behavior
Unchanged. A harness compared
mainand this branch on 103,377 calls on Node 26, Node 24 and Bun 1.4: identical return values and identical thrown errors (class and message). Inputs included the 2^53 boundary from both sides,-0,NaN,Infinity, fractions,2n ** 256natsize: 32, signed min/max for sizes 1, 2 and 32,sizeof0,-1,1.5,'32',NaNandInfinity, hex with trailing whitespace,0X,_, missing prefix, empty bytes, 6 and 7 significant bytes, and non-string or non-array junk.New tests snapshot (recorded on
main)numberToHex,hexToNumber,hexToBigInt,bytesToNumberandbytesToBigIntacross those boundaries, including the error messages.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.numberToHex(52)numberToHex(420, { size: 32 })numberToHex(uint256, { size: 32 })hexToNumber('0x1a4')hexToBigInt(negative 32 B, { signed: true })bytesToNumber(32 B offset word)bytesToBigInt(32 B offset word)bytesToBigInt(32 B uint256)Benches are in
toHex.bench.ts,fromHex.bench.ts(thehexToNumberbench is no longer skipped) andfromBytes.bench.ts.Bytes allocated per call:
numberToHex(420, { size: 32 })596 → 376,numberToHex(uint256, { size: 32 })504 → 224,bytesToNumber(offset word)1,123 → 56,bytesToBigInt(offset word)1,123 → 80.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: 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.