Repository navigation
Conversation
Prepare each event once per parseEventLogs call and look candidates up by selector, read well-formed address, uint and bytesN topics directly, and skip candidates that strict decoding would reject for missing topics or data. Logs are still formatted before matching, so malformed logs keep throwing. Amp-Thread-ID: https://ampcode.com/threads/T-01a1e1b3-ce83-72d3-b448-f29dd09b6e57 Co-authored-by: jxom <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 59ab168 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-5533
pnpm add [email protected] |
|
Bundled into #5543 as its own commit, so this PR is superseded. Closing in favour of the bundle. |
Speeds up
parseEventLogsanddecodeEventLog(B1 from the perf audit). Outputs and errors are unchanged. Logs are still formatted before they are matched, so a malformedRpcLogthat matches no event still throws.Changes
decodeEventLog.ts):toEventPlanreads an event's indexed and non-indexed inputs and each indexed input's topic kind once.decodeEventLogWithPlandecodes against one event exactly asdecodeEventLog({ abi: [event] })did, including the'name' infilter, the selector fromformatAbiItem, the anonymous candidate rules and every error. The publicdecodeEventLogbuilds a plan for the event it selects.parseEventLogs: builds the plans once per call and maps selectors to plans in ABI order, so overloads keep their precedence. Each log looks up its candidates in the map instead of filtering the whole ABI. AWeakMapindex across calls was avoided because it goes stale when an ABI array is mutated./^0x[0-9a-fA-F]{64}$/is read directly foraddress(checksum of the last 20 bytes),uintwider than 48 bits (BigInt(topic)) andbytesN(lowercased prefix). Other topics and types still go throughdecodeAbiParameters, so short, long or invalid topics throw the same errors.Transfermatched against ERC-20'sTransfer. Non-strict fallback still uses the first candidate..map().filter()became one loop.Benchmarks
src/utils/abi/parseEventLogs.bench.tsused to fetch logs from mainnet at import. It now builds 1000 deterministic logs: 60% ERC-20Transfer, 10%Approval, 10% ERC-721Transferand 20% events outside the ABI.src/utils/abi/decodeEventLog.bench.tsis new.SKIP_GLOBAL_SETUP=true ./node_modules/.bin/vitest bench -c ./test/vitest.config.ts --project core --run src/utils/abi/parseEventLogs.bench.ts src/utils/abi/decodeEventLog.bench.ts,origin/mainvs this branch, mean ms per call (two runs, noisy machine):parseEventLogs, 1000RpcLogs,erc20AbiparseEventLogs, 1000 formattedLogsparseEventLogs,eventName+argsfilterdecodeEventLog× 500 ERC-20 logsMemory per call (
node --expose-gc, 1000 logs,erc20Abi):parseEventLogs,RpcLogsparseEventLogs,LogsdecodeEventLog, oneTransferAfter this change the profile is led by the
checksumAddressLRU (lru.get, about 10%),formatLog,createCursorandhexToBytesin the data decode, all outside this PR's files. The topic regex is about 7%.Correctness
origin/main: scratch harnesses compared output values, key order, error class and error message against copies of theorigin/mainmodules.parseEventLogs: about 3.1k calls per seed over 5 seeds, each with up to 1000 logs. They coverederc20Abi, a 32-event ABI, anonymous events, a nameless event, same-selector overloads (Transfer(bytes20,uint160,uint256)),bytes4/uint48/uint56/int256/bool/uint8[]topics, and every combination ofargs,eventNameandstrict. Mutations included truncated or invalid data, missing, extra or uppercase topics,0Xprefixes, 31- and 33-byte topics, invalid hex, random topics, logs with their owneventName, and malformedRpcLogquantities. Everything matched.decodeEventLog: 2k cases per seed over 3 seeds, adding events withoutinputs,string/tupleindexed inputs and the same topic mutations. Everything matched.AbiEventfuzz properties were ported to Viem's API, with a differential property againstorigin/mainadded for both functions, using Ox's arbitraries for events and logs, recased topics, dropped topics and noise. At 20,000 runs each, all 5 properties passed: round-trip decode, Viem errors only for noise logs, lone-eventparseEventLogs, and differentialdecodeEventLogandparseEventLogs(randomeventName,strict).decodeEventLog: indexed topics in mixed case and dirty address padding decode as before, and a 31-byte address topic still throwsAbiDecodingDataSizeTooSmallError. The snapshots were written againstorigin/main.parseEventLogs: a malformedRpcLogthat matches no event still throws.src/utils/abi/and thegetLogs,getContractEvents,watchContractEvent,watchEvent,getFilterLogsandgetFilterChangestests pass against Anvil: 26 files, 623 tests.tsconsrcand on the touched tests and benches passes.Bundle size (needs a maintainer decision)
Measured locally with the same
tscbuild and size-limit:mainimport * from 'viem'(esm)require('viem')(cjs)viem/actionsCI measures about 200–300 B more than this local build. #5525 and #5532 raise the same limits, so whichever lands later needs another bump.