Skip to content

perf: look up the dedupe cache once per request - #5521

Merged
jxom merged 1 commit into
mainfrom
perf/dedupe-single-lookup
Nov 15, 2026
Merged

jxom merged 1 commit into
mainfrom
perf/dedupe-single-lookup

Conversation

@jxom

@jxom jxom commented Nov 15, 2026

Copy link
Copy Markdown
Member

Summary

withDedupe called promiseCache.get(id) twice on a cache hit. promiseCache is an LruMap, and LruMap.get deletes and re-inserts the key on every hit, so each hit refreshed the entry twice. This keeps the first lookup and returns it. Misses are unchanged.

Supersedes #5517 (thanks @backseats). It's the same source change, without the vi.spyOn call-count test, since the repo's test conventions forbid mocks and call tracking, and there is no observable behavior change to test.

Behavior

Identical on main and this branch, checked with real promises: a hit returns the same in-flight promise, a miss stores one, the entry is removed on both resolve and reject, enabled: false and a missing or empty id bypass the cache, a caller after settlement starts a new request, and LRU key order after a hit is the same.

Benchmark

Node 26, ns per withDedupe call (main → this branch):

Cache entries Random-key hit Same-key hit
10 110–190 → 65–140 135–180 → 100–195 (noise)
1,000 165–190 → 115–160 2,700–3,250 → 1,400–1,700
8,192 255–285 → 150–220 ~20,500 → ~10,500

Misses are unchanged (~4µs, dominated by creating the promise). The cache only holds in-flight requests, so it is usually small; the big win is when many callers hit one in-flight id while thousands of requests are pending.

Checks

  • pnpm test --run src/utils/promise/withDedupe.test.ts src/utils/buildRequest.test.ts: 86 passed
  • Biome on withDedupe.ts: clean
  • tsc -b src/tsconfig.json: clean

@changeset-bot

changeset-bot Bot commented Nov 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3f51644

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

@vercel

vercel Bot commented Nov 15, 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 15, 2026 8:21pm UTC

Request Review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T20:19:06.479336Z 3f51644 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-5521 dist-tag, which follows this pull request until it closes.

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

@jxom
jxom merged commit c368d83 into main Nov 15, 2026
39 of 40 checks passed
@jxom
jxom deleted the perf/dedupe-single-lookup branch November 15, 2026 21:20
This was referenced Nov 15, 2026

This branch had an error being deployed

1 failed deployment
Preview — 3f516442 Deployed Nov 15, 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