Skip to content

perf(client): cache thread pull request chains and precompute sort timestamps - #46827

Merged
juliusmarminge merged 3 commits into
pingdotgg:mainfrom
marcusbuffett:perf/thread-pull-requests-cache
Dec 1, 2026
Merged

juliusmarminge merged 3 commits into
pingdotgg:mainfrom
marcusbuffett:perf/thread-pull-requests-cache

Conversation

@marcusbuffett

Copy link
Copy Markdown
Contributor

Problem

On threads with many linked pull requests (e.g. a coordinator thread with thousands of linked PRs from subagents), rendering a sidebar row or opening its hover preview repeatedly rebuilds PR chains and sorts every chain on the main thread:

  • SidebarThreadRow calls resolveThreadCurrentPullRequestLink twice and resolveThreadPullRequestBadge once per render, running resolveThreadPullRequestChains three times over the entire pullRequests array.
  • ThreadPullRequestsMiniList (hover preview) re-runs resolveThreadPullRequestChains and pullRequestListLines on every hover even after fix(web): sidebar linked-item hover cards show 8 items and open beside the row #43222 capped the rendered DOM to 8 rows.
  • pullRequestListLines and threadSearchPullRequests call Date.parse() inside their Array.prototype.sort comparators ($O(N \log N)$ parses and temporary array allocations per sort), and layersFromTop allocates and sorts even for single-layer chains.

Change

  • packages/source-control-core/src/shared/threadPullRequests.ts:
    • Cache resolveThreadPullRequestChains, resolveThreadCurrentPullRequest, and resolveThreadPullRequestBadge per immutable links array via WeakMap (alongside the existing per-link normalizedKeys WeakMap), and cache pullRequestListLines per chains array.
    • Precompute per-chain timestamps once before sorting in pullRequestListLines and resolveThreadCurrentPullRequest instead of calling Date.parse() inside sort comparators, and fast-path single-layer chains in layersFromTop and open-chain filtering.
    • Compute badge state in a single pass and skip chain resolution for single-PR threads.
  • packages/shared/src/threadPullRequests.ts:
    • Preserve input array identity in visibleThreadPullRequests when no "stack-dismissed" tombstone is present, and precompute timestamps before sorting in threadSearchPullRequests.
  • apps/web/src/components/ThreadStatusIndicators.tsx, ThreadDetailsPrRows.tsx, ThreadPullRequestsPanel.tsx:
    • Pass the thread's pullRequests array directly to resolveThreadPullRequestChains (which already filters dismissed tombstones) so hover cards, details rows, and the linked-items panel share the WeakMap cache with SidebarThreadRow even when tombstones are present.
    • Use pullRequests.some(...) in PullRequestBadge instead of allocating a filtered copy just to check non-emptiness.
  • packages/client-runtime/src/threadPullRequests.test.ts:
    • Add unit tests for pullRequestListLines ordering/ties/invalid timestamps, WeakMap caching and invalidation on new arrays, and visibleThreadPullRequests identity preservation.

Scope

Focused performance bug fix on hot client derivation paths (web, desktop, and mobile share packages/source-control-core and packages/shared, keeping Hermes compatibility with no ES2023 array methods). Small self-contained fix for a measurable performance bug; no separate issue filed.

Verification

Benchmarked against a real local thread with 6,433 linked pull requests on 89ba000739 (before) vs 3b8d1d30cc (after):

Operation Before After
Cold SidebarThreadRow (2× current + 1× badge) 73.10 ms 52.01 ms
Cold hover (chains + listLines) 21.74 ms 2.43 ms
Warm SidebarThreadRow (avg of 10 / 100) 20.10 ms 0.0004 ms
Warm hover (avg of 10 / 100) 18.16 ms 0.0001 ms
threadSearchPullRequests (avg of 5) 8.88 ms 1.76 ms

Ran vp run check:changed — all checks passed:

  • vp format: pass (6 files)
  • vp lint: pass (0 warnings, 0 errors)
  • vp knip: pass
  • vp typecheck (37 packages): pass
  • vp test (4 files, 43 tests including threadPullRequests.test.ts [32 tests] and ThreadStatusIndicators.linkedPr.browser.test.tsx [Chromium]): pass
  • vp react compiler (3 files): pass

Not checked: did not run the mobile test suite or record a UI screenshot/video because there are no visual or layout changes.

Authored by Gemini 4 Neon in T3 Code (Antigravity harness).

@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Dec 1, 2026
@macroscopeapp

macroscopeapp Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 3b8d1d3

Macroscope's review found this PR approvable — Caches derived pull-request chains, badges and rows while reducing repeated timestamp parsing and filtering. The changes remain confined to in-memory derivations, with no product-default, persistent-data or external-write changes.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Thread pull-request handling now resolves chains from raw links, applies visibility rules where needed, and caches derived results by input identity. List ordering uses update timestamps with stable tie handling. Web components and tests use the updated behavior.

Changes: Thread pull-request handling

Layer / File(s) Summary
Resolve chains and current pull requests
packages/shared/src/threadPullRequests.ts, packages/source-control-core/src/shared/threadPullRequests.ts
Visibility filtering reuses the original array when no links are dismissed. Chain and current-pull-request resolution use identity-based caches. Timestamp parsing maps invalid values to zero. Badge calculation uses visible links and resolved chains.
Order and cache pull-request list rows
packages/source-control-core/src/shared/threadPullRequests.ts, packages/client-runtime/src/threadPullRequests.test.ts
List rows are cached and ordered by the newest chain update, preserving input order for ties. Tests cover ordering, chain metadata, cache reuse, and visibility filtering.
Use raw links in web pull-request lists
apps/web/src/components/ThreadStatusIndicators.tsx, apps/web/src/components/chat/ThreadDetailsPrRows.tsx, apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
Web components resolve chains from raw links. Visibility checks remain for displayed links and mini-list rendering.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Suggested reviewers: juliusmarminge, saphid, stienswout

Merge Risk: Minimal · up to 8457c

This change caches thread pull-request derivations and precomputes sort timestamps, with no visible change in behavior. No concrete defect remains after review. It looks safe to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "perf(client): cache thread pull request chains and precompute sort timestamps" matches the main change. The diff adds WeakMap caching for chains, current PR, badge, and list lines, and prec…
Description check Passed The description follows the template. It has Problem, Change, Scope, and Verification sections, and each has substantive content. The Problem section gives the bug and reproduction context (a thread w…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "perf(client): cache thread pull request chains and precompute sort timestamps" matches the main change. The diff adds WeakMap caching for chains, current PR, badge, and list lines, and precomputes timestamps before sorting. It follows the conventional commit format from the template.

Full details: Description check

Explanation

The description follows the template. It has Problem, Change, Scope, and Verification sections, and each has substantive content. The Problem section gives the bug and reproduction context (a thread with thousands of linked PRs). The Change section lists files and the approach. The Scope section explains why no issue is linked. The Verification section gives before/after benchmarks, the checks that passed, and what was not checked. It does not include the model and harness line the template asks for when an agent was used, but the author did add an 'Authored by' line. The Scope section also says there is no linked issue for a bug fix, which the template allows for small obvious fixes. Under the template's rules, the description is mostly complete.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 57305a3 and 8457c7d.

Files selected for processing: 6

  • apps/web/src/components/ThreadStatusIndicators.tsx
  • apps/web/src/components/chat/ThreadDetailsPrRows.tsx
  • apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
  • packages/client-runtime/src/threadPullRequests.test.ts
  • packages/shared/src/threadPullRequests.ts
  • packages/source-control-core/src/shared/threadPullRequests.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@juliusmarminge juliusmarminge added the takeover:in-progress A takeover lead is working on this PR label Dec 1, 2026
juliusmarminge and others added 2 commits December 1, 2026 15:43
The array-identity caches already make these paths run once per links
array, so the hand-rolled loops and single-layer fast paths only added
code. Fold the visibleThreadPullRequests identity test into the caching
test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Main added a live status override to resolveThreadPullRequestBadge; only
the snapshot-based badge is cached now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@juliusmarminge

Copy link
Copy Markdown
Member

Evidence

Seed 6,433 synthetic independent open PRs through real link/sync RPC commands, open the replay thread in Chromium, then measure sidebar, hover and search derivations on its real immutable links array. Same Linux VM, Chromium 158, Vite dev builds, light theme and 1280×800 viewport. Three runs per commit, 30 samples per operation per run; warm samples batch 20 calls.

Before After
4374266d29 8457c7d3b0
Before: base PR hover preview After: head PR hover preview
Before hover recording After hover recording

Median of the three run medians, with their range, in ms:

Derivation Before After
Cold sidebar 60.65 (58.45–63.20) 42.75 (41.35–44.65)
First hover after sidebar 8.35 (8.30–8.55) 1.20 (1.20–1.20)
Warm sidebar 19.09 (18.85–22.07) <0.005 in every run
Warm hover 7.97 (7.72–8.84) <0.005 in every run
Search 1.70 (1.70–1.80) 1.05 (1.00–1.10)

Each improvement exceeds the spread between run medians. Individual samples include GC outliers; these are median results, not worst-case guarantees. The timer resolves 0.1 ms, so zero-valued 20-call batches are reported as <0.005 ms per call. Cold hover is the first hover following sidebar on the same newly cloned array; cloning is outside timing.

All three runs on both commits produced identical hashes for the complete ordered sidebar, hover and search outputs, excluding only per-seed identifiers/timestamps. Both show open PR #6433, 6,432 others, and 6,433 hover/search entries; rendered previews show #6433 through #6426 and +6425 more. The images and recordings verify content; the measured action is derivation, not hover-to-paint.

All raw timing samples and hashes · Measurement script · RPC seed script · Reproduction and environment notes

Not shown:

  • End-to-end hover-to-paint and complete sidebar render latency were not measured; the recordings verify matching content and the browser script measures the derivations named in the reproduction.
  • Exact sub-microsecond warm timings claimed by the PR are below this browser timer's resolution; measured batches establish <0.005 ms per invocation.
  • Native/derived multi-PR stack fixtures, cache invalidation after later edits, mobile and desktop-shell behavior were not exercised; this evidence uses 6,433 independent open PRs in the web client.

@juliusmarminge juliusmarminge added takeover:ready Takeover done; auto-lands when gates pass and removed takeover:in-progress A takeover lead is working on this PR labels Dec 1, 2026
@juliusmarminge
juliusmarminge merged commit 0491050 into pingdotgg:main Dec 1, 2026
19 checks passed
@juliusmarminge juliusmarminge removed the takeover:ready Takeover done; auto-lands when gates pass label Dec 1, 2026
imetn added a commit to imetn/t4code that referenced this pull request Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants