Skip to content

perf(client): equal query refetches no longer re-render PR rows - #20850

Merged
juliusmarminge merged 2 commits into
mainfrom
perf/query-reuse-unchanged
Oct 17, 2026
Merged

juliusmarminge merged 2 commits into
mainfrom
perf/query-reuse-unchanged

Conversation

@juliusmarminge

@juliusmarminge juliusmarminge commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Problem

The render audit looked at the thread details panel of a thread with 15 linked PRs. While idle, each row's checks query refetches every 45-60s. Each refetch publishes a value with new identities and the same content, so every row, its controls, and its tooltip re-render even when no check changed. That came to about 30 renders per minute while idle (lane 4: 30 renders, top cause ThreadDetailsPrRow state#6, value.checks[0..n] identity only).

Fix

  • createEnvironmentQueryAtomFamily (client-runtime) passes every resolved and emitted value through the existing reuseUnchanged helper, against the previous success. An equal refetch keeps the previous value reference, and so does each unchanged nested object or array. A genuine change still publishes new references, and only for the parts that changed. This applies to every environment query, including the persisted ones (the reused value is also what gets saved).
  • ThreadDetailsPrRow compares timestamps into a boolean before merging detail and checks. The compiled output now keys the merged detail on [checksAreNewer, checksQuery.data, detailQuery.data] instead of the raw timestamps, which move on every refetch. With the data stable, detail stays stable, and the downstream compiler cache slots ($[n] !== detail, detail?.checks, the trailing actions and the checks tooltip) bail out.

The AsyncResult wrapper still changes on every refetch, on purpose. SWR staleness reads its timestamp, and the waiting flag drives the spinner and the refresh gating, so those transitions still publish.

What still renders

Each row component itself still renders on the waiting-on and waiting-off transitions of a refresh. Its children with unchanged props no longer do.

Callers that key effects on query.data identity (PullRequestDetailPanel snapshot save, CodexSetupSection handoff import) now fire less often. Both effects are idempotent or guarded.

Tests

The new runtime.test.ts case checks that an equal refetch returns the same reference and a changed refetch returns a new one. It fails without the fix.

After:

Update

The in-app re-measure showed no change: ThreadDetailsPrRow still rendered 20 times in 120 s idle. Keeping data stable was not enough, because every refetch publishes a new AsyncResult (new timestamp, waiting on and off), and the row read the whole result through useEnvironmentQuery.

The query atoms are unchanged beyond the first commit: SWR still reads the timestamp, and existing useEnvironmentQuery callers still see isPending. Readers that need less now subscribe to less:

  • selectEnvironmentQueryData (client-runtime) selects a result's data alone. With the reuseUnchanged sharing, an equal refetch leaves it identical, so a subscriber to it is not notified.
  • apps/web/src/state/query.ts adds useEnvironmentQuerySelector(atom, select), useEnvironmentQueryIsNewer(query, than) (a derived boolean atom comparing the two timestamps), and useEnvironmentQueryRefresh(atom), which returns refresh plus an isPending() read from the registry at call time.
  • ThreadDetailsPrRow reads detail data, checks data, "checks are newer", and "checks settled empty" through those. It checks isPending() when its live refresh fires instead of subscribing to it. A poll that returns equal checks no longer re-renders the row. A changed poll, or detail being newer than checks, still does.

Compiled output: detail is keyed on [checksAreNewer, checksData, detailData], all three stable across an equal poll. Both files compile with no React Compiler diagnostics.

Test: runtime.test.ts "does not notify a data-only reader when a refetch returns equal content" subscribes to Atom.map(query, selectEnvironmentQueryData) and checks that an equal refetch notifies the result subscriber but not the data subscriber, and that a changed refetch notifies both. It fails without the reuseUnchanged sharing.

After: still to be re-measured in the app.

🤖 Generated with Claude Code

@juliusmarminge juliusmarminge added the macroscope-review Opt PRs made by unvouched contributors in for Macroscope review. Vouched contributors auto-reviews label Oct 17, 2026
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:S 10-29 changed lines (additions + deletions). labels Oct 17, 2026
@macroscopeapp

ghost commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Needs Human Approval

Macroscope's review found this PR requires human approval — The optimization changes reference-identity and subscription behavior in the shared environment-query runtime, affecting many existing production consumers beyond PR rows. The targeted tests cover the new sharing behavior, but the broader effects and memoization implications should be checked across those consumers.

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

@coderabbitai

ghost commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Path: .coderabbit.config.ts
  • Review profile: CHILL
  • Plan: Team
  • Run ID: c077980b-458c-4af4-b1e9-3b386db40160
📥 Commits

Reviewing files that changed from the base of the PR and between 8e3fced and 4f864cf.

📒 Files selected for processing: 5
  • apps/web/src/components/chat/ThreadDetailsPrRow.browser.test.tsx
  • apps/web/src/components/chat/ThreadDetailsPrRow.tsx
  • apps/web/src/state/query.ts
  • packages/client-runtime/src/state/runtime.test.ts
  • packages/client-runtime/src/state/runtime.ts

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

📝 Walkthrough

The environment query runtime now reuses prior successful values when refetched values are unchanged. New query hooks select data, compare update times, and expose refresh state. The PR row uses these hooks to select details and checks and control refreshes.

Changes: Environment Query and PR Row

Layer / File(s) Summary
Reuse unchanged query values
packages/client-runtime/src/state/runtime.ts, packages/client-runtime/src/state/runtime.test.ts
Environment query emissions and settled results reuse prior successful value references when content is unchanged, with or without persistence. Tests cover equal-content refetches, subscriber notifications, and changed content.
Select, compare, and refresh queries
apps/web/src/state/query.ts
The query hooks select result values, compare update timestamps, and provide refresh and pending-state callbacks. The timestamp helper supplies dataUpdatedAt.
Select PR details and refresh checks
apps/web/src/components/chat/ThreadDetailsPrRow.tsx, apps/web/src/components/chat/ThreadDetailsPrRow.browser.test.tsx
The PR row selects detail and check data, uses freshness to choose newer checks, and skips refreshes while queries are pending. It stops refreshing checks after a successful null result. The browser test mocks the selector and query state.

Priority: ⬇️ Low

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

Change: Refactor

Suggested reviewers: t3dotgg, bil0000

Merge Risk: ⚪ Minimal · up to 4f864

No actionable merge-blocking regression was established; the change is mergeable after normal checks.

Pre-merge checks: Passed 3 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Description check Warning The description clearly explains the problem, implementation, affected behavior, and tests. However, it omits the required Scope and approval section and does not provide completed verification result… Add the required Scope and approval section with issue or maintainer-approval details, rename or expand the testing section as Verification, report observed test and performance results, include before/after UI screenshots where applicable,…
Passed Passed checks: 3 passed
Check name Status Explanation
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.
Title check Passed The title clearly and concisely describes the primary change: preventing equal query refetches from re-rendering PR rows.
Full details: Description check

Explanation

The description clearly explains the problem, implementation, affected behavior, and tests. However, it omits the required Scope and approval section and does not provide completed verification results or the required UI screenshots. The reported measurements remain pending.

Resolution

Add the required Scope and approval section with issue or maintainer-approval details, rename or expand the testing section as Verification, report observed test and performance results, include before/after UI screenshots where applicable, and identify the model and harness used for the agent-generated work.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@juliusmarminge

ghost commented Oct 17, 2026

Copy link
Copy Markdown
Member Author

Re-measured in the app (seeded data, same scenario, median of 3):

Measured against main at 489d08a, with this PR's commit cherry-picked onto that same commit (it applies cleanly, and still merges cleanly with today's main). The server ran without --watch, and no window had a reconnect. Thread "Take Over PR 6315" with 5 linked open PRs, thread details panel open with "Show 4 more" expanded, idle for 2 minutes per window (it spans checks polls at 45/60 s).

scenario main this PR
idle 120 s, total renders 568 (444 / 568 / 1660) 666 (595 / 666 / 781)
idle 120 s, ThreadDetailsPrRow renders 20 (12 / 20 / 35) 20 (20 / 20 / 15)

No improvement: the PR rows still re-render on every checks refetch. A hook-slot diff on ThreadDetailsPrRow with this PR shows slot #6 (the checks query AsyncResult) changing as #6.waiting=false #6.timestamp=…, four rows per poll. reuseUnchanged keeps data stable, but get.setSelf(AsyncResult.success(...)) still publishes a new AsyncResult with a new timestamp and a waiting flip. useEnvironmentQuery subscribes to the whole result (dataUpdatedAt, isPending), so the row re-renders anyway, and so do its tooltips (TooltipTrigger props:render,children ~110, ThreadDetailsControl handlers). The spread for detail on line 123 also builds a new object on every render. To stop this, skip setSelf when the reused value is identical (and leave waiting alone for background refetches), or have the row read only data through a selector.

The window-to-window spread (main #2 = 1660) comes from providerStatuses pushes landing in the window. Those were present on both sides, and are not this PR.

Behaviour check: rows and their states are unchanged ("Ready" on #19274/#19260/#19261/#19270, "Resolve" on #19259), and the checks keep refreshing.

Julius Marminge and others added 2 commits October 17, 2026 03:43
A refetch that returns the same content now keeps the previous value's
references, so PR rows and their tooltips skip re-rendering on idle
checks polls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The query result still publishes a new AsyncResult on every refetch (new
timestamp, waiting on and off), so reading the whole result re-rendered the
row on every poll. The row now subscribes to the data alone, to a derived
"checks are newer" boolean, and to whether checks settled empty, and reads
the pending flag only when its refresh fires.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@juliusmarminge
juliusmarminge force-pushed the perf/query-reuse-unchanged branch from 8e3fced to 4f864cf Compare October 17, 2026 11:10
@github-actions github-actions Bot added size:L 100-499 changed lines (additions + deletions). and removed size:S 10-29 changed lines (additions + deletions). labels Oct 17, 2026
@juliusmarminge

ghost commented Oct 17, 2026

Copy link
Copy Markdown
Member Author

Re-measured after the fix, in the app (seeded data, same scenario as round 2, median of 3):

This PR's two commits are cherry-picked onto main at 2fae0f9841 (they apply cleanly). The server ran without --watch, and no window had a reconnect. Thread "Take Over PR 6315" with 5 linked PRs, thread details open with "Show 4 more" expanded, idle for 120 s per window, which spans several checks polls.

scenario main this PR
idle 120 s, ThreadDetailsPrRow renders 20 (12 / 20 / 30) 0 (0 / 0 / 10)
idle 120 s, total renders 496 (378 / 496 / 1270) 259 (202 / 259 / 976)
idle 120 s, commits 22 4

This fixes the round-2 problem. On main, every poll re-renders each row through ThreadDetailsPrRow state#6 (the checks query result, with a new timestamp and waiting flip). With this PR, the checks polls cause no row renders at all. The 10 in run 3 is a different cause. That window had two providerStatuses pushes, and the rows rendered via state#0, which is useServerConfigs() (the row reads it for the pullRequests/pullRequestChecks capabilities). One render per row per push, not per poll. Reading just those two booleans through a selector would remove it, but that's outside this PR. The high totals in main #2 and PR #3 come from those pushes, on both sides.

Behaviour check: the rows and their states are unchanged: #19274, #19260, #19261 and #19270 show "Ready", and #19259 shows "Resolve". The checks requests still go out on schedule (Exit/Success responses arrive in every window).

Screenshot: /home/julius/.cache/audit/remeasure/20850-after.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

macroscope-review Opt PRs made by unvouched contributors in for Macroscope review. Vouched contributors auto-reviews size:L 100-499 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant