Skip to content

perf: make multicall chunking, batch scheduling and IPC framing linear - #5526

Closed
jxom wants to merge 3 commits into
mainfrom
perf/linear-batching-and-ipc-framing
Closed

jxom wants to merge 3 commits into
mainfrom
perf/linear-batching-and-ipc-framing

Conversation

@jxom

@jxom jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member

Summary

Three hot paths copied or rescanned a growing buffer on every step, so their cost grew with the square of the input:

  • multicall chunking built each chunk with chunk = [...chunk, call]. It now pushes.
  • createBatchScheduler copied the queue on every schedule ([...queue, item]) and passed a fresh copy of the queued args to shouldSplitBatch, which call and multicall then summed on every check. The queue now grows in place, and shouldSplitBatch receives a running size. A new getSize option says how much each item adds (default 1, so http keeps counting requests); call and multicall pass their calldata length. createBatchScheduler is internal, so this changes no public API.
  • IPC framing copied the pending buffer twice per data event (Uint8Array.from and Buffer.concat) and rescanned it from byte 0. extractMessages is replaced by createMessageExtractor, which keeps the scanner state (brace level, in-string, escaped) and the pending message's chunks across data events, and concatenates each message once. The string, escape and brace handling is unchanged; the existing ipc.framing.test.ts and ipc.test.ts cases now run against the incremental extractor, including every split point and byte-by-byte delivery.

The IPC change only touches the framing code. #5523 changes the onClose wiring in the same getSocket, so whichever lands second needs a small rebase around the onData lines.

Benchmarks

The benches are new colocated files, run on main and on this branch with the same file:
SKIP_GLOBAL_SETUP=true ./node_modules/.bin/vitest bench -c ./test/vitest.config.ts --project core --run <file> (Node 26, mean per iteration, main → this branch).

src/utils/promise/createBatchScheduler.bench.ts: schedule n items in one tick, one createBatchScheduler per item sharing an id (as call and http do). The ~1.2ms floor is the setTimeout(0) that flushes the batch.

Items main This branch
100 1.18ms 1.19ms
1,000 13.8ms 1.23ms
5,000 305ms 1.93ms

src/actions/public/multicall.bench.ts: real multicall and batched call over HTTP against an ephemeral local JSON-RPC server that returns canned aggregate3 results.

Case main This branch
multicall, 1,000 calls, batchSize: 1024 34.5ms 37.0ms (noise, ±12-18%)
multicall, 1,000 calls, batchSize: 0 19.0ms 18.9ms
multicall, 10,000 calls, batchSize: 0 294ms 203ms
1,000 batched calls into one multicall 27.3ms 17.9ms
5,000 batched calls into one multicall 243ms 99ms

End to end, encoding and decoding dominate multicall at 1,000 calls; the chunk copy shows from about 10,000 calls in one chunk. In isolation, chunking 1,000 calls with batchSize: 0 went from 731 to 46,211 ops/s in the audit's scratch bench.

src/utils/rpc/ipc.bench.ts: one request over a real Unix socket whose response is delivered in 64KB chunks.

Response main This branch
1MB 43ms 6.5ms
4MB 403ms 30ms
16MB 5,798ms 125ms

The audit's scratch script measured 4.1GB copied to frame the 16MB response on main.

Checks

  • pnpm test --run --project core src/actions/public/multicall.test.ts src/actions/public/call.test.ts src/clients/transports/http.test.ts src/clients/transports/ipc.test.ts src/utils/rpc/ src/utils/promise/createBatchScheduler.test.ts: all pass except call.test.ts > errors > gas fee is less than block base fee, which fails the same way on main in this environment (fork base fee).
  • pnpm test --run --project core src/clients src/utils/promise src/actions/public/readContract.test.ts: 347 passed; public.test.ts > smoke test > getProof failed with an upstream fork RPC error.
  • pnpm check:types: clean
  • Biome on the changed files: clean

ampagent and others added 3 commits November 16, 2026 00:34
createBatchScheduler copied the whole queue on every schedule and passed
a copy of the queued args to shouldSplitBatch, which call and multicall
summed on every check. The queue now grows in place and shouldSplitBatch
receives a running size built from getSize.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1e1b3-b2f5-71c9-92e4-458176671755
Co-authored-by: jxom <[email protected]>
Each data event copied the pending buffer twice and rescanned it from the
first byte. The scanner state and the pending message's chunks now carry
over between data events, and each message is concatenated once.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1e1b3-b2f5-71c9-92e4-458176671755
Co-authored-by: jxom <[email protected]>
@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
viem Error Error Nov 16, 2026 12:37am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a48339e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T00:37:03.537883Z a48339e PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

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

pnpm add viem@pr-5526
pnpm add [email protected]

jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Bundled into #5543 as its own commit, so this PR is superseded. Closing in favour of the bundle.

@jxom jxom closed this Nov 16, 2026

This branch had an error being deployed

1 failed deployment
Preview — a48339e4 Deployed Nov 16, 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.

2 participants