Skip to content

perf(0.x): backport hot-path regexes and signature hash cache - #571

Merged
gakonst merged 1 commit into
0.xfrom
perf/0.x-backport-569-570
Nov 3, 2026
Merged

gakonst merged 1 commit into
0.xfrom
perf/0.x-backport-569-570

Conversation

@gakonst

@gakonst gakonst commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Backports #569 and #570 from main to 0.x, so the 0.14.x line (legacy-v0) gets them.

#569: hoist per-call regular expressions in hot validation paths

Regex literals inside a function body allocate a fresh RegExp on every evaluation. This moves the ones on hot validation paths to module scope: Hex.assert (strict), Value.format/Value.from, TypedData type dependency and field validation, Siwe.isUri, frame nonce RPC decoding, and the integer and fixed-bytes checks in ABI overload resolution (isArgOfType). None is global or sticky, so sharing them holds no lastIndex state. isArgOfType reads the integer bit size from the integer type regex's capture instead of running a second replace.

Differences from main:

  • 0.x has no ox/zod entrypoint, so those schema changes don't apply.
  • 0.x's Siwe has no isAuthority helper, so only the isUri and splitUri regexes move.
  • Value.from keeps 0.x's own decimal pattern (/^(-?)([0-9]*)\.?([0-9]*)$/), now hoisted, so its accepted inputs don't change.
  • On main, isArgOfType already had module-level integerTypeRegex/fixedBytesTypeRegex. 0.x had inline literals, so this PR hoists both.

#570: cache signature hashes by normalized signature

AbiItem.getSignatureHash keccak-hashed the signature on every call for ABI items without a precomputed hash. Selector lookups call it for every item they compare: AbiItem.fromAbi with a selector, AbiFunction.decodeData with an ABI, and AbiEvent.decodeLog/AbiEvent.extractLogs for each log. The hash is now memoized in a bounded (8192-entry) map keyed by the normalized signature string. The map sits next to the checksum cache in a new internal/caches.ts, so Caches.clear() resets it. Only Caches.checksum stays public.

Differences from main:

  • 0.x uses its existing LruMap (the same class as the checksum cache) rather than main's BoundedMap.
  • 0.x has no Engine, so keccak256 can't be swapped and the engine-change test isn't ported. The normalized-signature and Caches.clear tests are ported.

Public API is unchanged. Each change has its own patch changeset.

Local checks

  • pnpm check:types: passed
  • pnpm check:tsdoc: passed
  • biome check on the changed files, plus the pnpm check pre-commit hook: passed
  • vitest --project core on AbiItem, Caches, Hex, Siwe, TypedData and Value: 378 tests passed
  • Full core, tmp_frames and tempo-unit projects: everything passed except the Anvil fork network tests (RPC, Provider, transaction envelopes, AbiEvent/AbiError network cases). Without VITE_ANVIL_FORK_URL those get HTTP 400s or time out against the public fallback RPC, so CI is the check for them.

Backports #569 and #570 from main.

#569: Regex literals inside a function body allocate a fresh RegExp on
every evaluation. Move the ones on hot validation paths to module scope:
`Hex.assert` (strict), `Value.format`/`Value.from`, `TypedData` type
dependency and field validation, `Siwe.isUri`, frame nonce RPC
decoding, and the integer and fixed-bytes checks in ABI overload
resolution. All of them are non-global and non-sticky, so sharing them
holds no `lastIndex` state. `isArgOfType` also reads the integer bit
size from the integer type regex capture instead of running a second
`replace` regex. 0.x has no `ox/zod` entrypoint, and keeps its own
`Value.from` decimal pattern, so behavior is unchanged.

#570: `AbiItem.getSignatureHash` keccak-hashed the signature on every
call for ABI items without a precomputed `hash`. Memoize the hash in a
bounded (8192-entry) LRU map keyed by the normalized signature string,
next to the checksum cache in a new `internal/caches.ts`, so
`Caches.clear()` resets it. Only `Caches.checksum` stays public. 0.x
has no `Engine`, so keccak256 cannot be swapped and the engine test is
not ported.
@vercel

vercel Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
ox Ready Ready Preview Nov 3, 2026 4:08am UTC

Request Review

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Published [email protected] under the pr-571 dist-tag, which follows this pull request until it closes.

pnpm add ox@pr-571
pnpm add [email protected]

@gakonst
gakonst merged commit 4a029cf into 0.x Nov 3, 2026
14 checks passed
@gakonst
gakonst deleted the perf/0.x-backport-569-570 branch November 3, 2026 04:08
@github-actions github-actions Bot mentioned this pull request Nov 3, 2026
gakonst added a commit to tempoxyz/accounts that referenced this pull request Nov 3, 2026
Updates `ox` from 0.14.59 to 0.14.62 (`legacy-v0`, published
2026-11-03T04:23:31Z). The catalog, the override and the package's own
dependency range all change. The range goes from `~0.14.59` to
`~0.14.62`, so this adds a patch changeset. Follows #943, which updated
Viem.

## What 0.14.62 brings

- wevm/ox#571 backports #569 and #570:
- `Hex.assert`, `Value`, `TypedData`, `Siwe.isUri` and ABI overload
resolution reuse module-level regular expressions instead of building
new ones on every call.
- `AbiItem.getSignatureHash` and `AbiItem.getSelector` cache signature
hashes in a bounded cache that `Caches.clear` resets. So do the selector
lookups in `AbiItem.fromAbi`, `AbiFunction.decodeData`,
`AbiEvent.decodeLog` and `AbiEvent.extractLogs`.
- wevm/ox#565: `AbiEvent.decode` and `AbiEvent.encode` handle anonymous
events.

The range also picks up 0.14.60 and 0.14.61. The `eth_fillTransaction`
result `tx` is now typed as a `OneOf` union. `AbiItem.fromAbi` throws
`AmbiguityError` for overloads it can't tell apart.
`AbiParameters.decode` checks integer ranges and address padding.
Envelope `assert` rejects a tip above a zero `maxFeePerGas`. `TypedData`
validates types more strictly. `KeyAuthorization` `isAdmin` is
deprecated for TIP-1137. None of these needed changes here, and every
suite passes unchanged.

## Lockfile

As in #943, the lockfile is edited directly, because a full resolution
adds a real `[email protected]` peer. Only the `ox` entries change: the
version, the integrity, and the specifiers set by the override. `ox`
0.14.62 has the same dependencies as 0.14.59. It isn't a peer of any
package, so no hashed peer suffix includes it. `ox` is excluded from
`minimumReleaseAge`.

## Validation

- `pnpm install --frozen-lockfile` from empty `node_modules`: passed.
- `pnpm check:types`: passed.
- `pnpm check`: 0 errors; the 5 warnings are already on `main`.
- `pnpm build`: passed.
- `pnpm test --project lib/pure --reporter=dot`: 50 files, 889 tests
passed.
- `VITE_NODE_ENV=localnet VITE_NODE_TAG=latest pnpm test --project
lib/localnet --reporter=dot --maxWorkers=4` against
`ghcr.io/tempoxyz/tempo:latest` in OrbStack: 10 files, 658 tests passed.

This branch was successfully deployed

1 active deployment
Preview — 77d1ed0a Deployed Nov 3, 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.

1 participant