Repository navigation
perf(client): equal query refetches no longer re-render PR rows - #20850
Conversation
ApprovabilityVerdict: 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. |
commented
Oct 17, 2026
|
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
No improvement: the PR rows still re-render on every checks refetch. A hook-slot diff on The window-to-window spread (main #2 = 1660) comes from Behaviour check: rows and their states are unchanged ("Ready" on #19274/#19260/#19261/#19270, "Resolve" on #19259), and the checks keep refreshing. |
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>
8e3fced to
4f864cf
Compare
commented
Oct 17, 2026
|
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
This fixes the round-2 problem. On main, every poll re-renders each row through 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: |
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
ThreadDetailsPrRowstate#6,value.checks[0..n]identity only).Fix
createEnvironmentQueryAtomFamily(client-runtime) passes every resolved and emitted value through the existingreuseUnchangedhelper, 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).ThreadDetailsPrRowcompares timestamps into a boolean before merging detail and checks. The compiled output now keys the mergeddetailon[checksAreNewer, checksQuery.data, detailQuery.data]instead of the raw timestamps, which move on every refetch. With the data stable,detailstays 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
waitingflag 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.dataidentity (PullRequestDetailPanelsnapshot save,CodexSetupSectionhandoff import) now fire less often. Both effects are idempotent or guarded.Tests
The new
runtime.test.tscase 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:
ThreadDetailsPrRowstill rendered 20 times in 120 s idle. Keepingdatastable was not enough, because every refetch publishes a newAsyncResult(new timestamp,waitingon and off), and the row read the whole result throughuseEnvironmentQuery.The query atoms are unchanged beyond the first commit: SWR still reads the timestamp, and existing
useEnvironmentQuerycallers still seeisPending. Readers that need less now subscribe to less:selectEnvironmentQueryData(client-runtime) selects a result's data alone. With thereuseUnchangedsharing, an equal refetch leaves it identical, so a subscriber to it is not notified.apps/web/src/state/query.tsaddsuseEnvironmentQuerySelector(atom, select),useEnvironmentQueryIsNewer(query, than)(a derived boolean atom comparing the two timestamps), anduseEnvironmentQueryRefresh(atom), which returnsrefreshplus anisPending()read from the registry at call time.ThreadDetailsPrRowreads detail data, checks data, "checks are newer", and "checks settled empty" through those. It checksisPending()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:
detailis 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 toAtom.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 thereuseUnchangedsharing.After: still to be re-measured in the app.
🤖 Generated with Claude Code