perf(sidebar): stop re-rendering on every streamed token#2710
Open
KarloAldrete wants to merge 1 commit into
Open
perf(sidebar): stop re-rendering on every streamed token#2710KarloAldrete wants to merge 1 commit into
KarloAldrete wants to merge 1 commit into
Conversation
The sidebar consumed the whole `sessions` record via `useSessions()`, which immer replaces on every appended event (one per streamed token). Since the sidebar is mounted at the root, that re-rendered the whole tree on every token. `deriveTaskData` only reads four session fields (isPromptPending, pendingPermissions size, cloudStatus, cloudOutput.pr_url) -- never `events`: - Add `computeSidebarSessionSignature` (core, pure): a primitive signature of just those fields. - Add `useSidebarSessionMap` (ui): subscribes to that signature and rebuilds the taskId -> session map only when a sidebar-relevant field changes. - `useSidebarData` uses it instead of `useSessions()`. Render-count test: 20 streamed tokens caused 20 sidebar re-renders before, 0 after (and 1 when a relevant field actually changes). Part of PostHog#2162 Generated-By: PostHog Code Task-Id: 8e9f327d-84b1-4608-9f48-c3038dbf87ca
Contributor
|
Reviews (1): Last reviewed commit: "perf(sidebar): stop re-rendering on ever..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The sidebar re-renders on every streamed agent token. It consumes the whole
sessionsrecord viauseSessions(), which immer replaces on each appended event (one per token). Because the sidebar is mounted at the root, this re-renders the whole tree on every token during streaming.Part of #2162.
Changes
deriveTaskDataonly reads four session fields (isPromptPending,pendingPermissions.size,cloudStatus,cloudOutput.pr_url) — neverevents.computeSidebarSessionSignature(core, pure): a primitive signature of just those fields.useSidebarSessionMap(ui): subscribes to that signature and rebuilds thetaskId → sessionmap only when a sidebar-relevant field changes — not on every token.useSidebarDatauses it instead ofuseSessions().No behavior change —
deriveTaskDatareceives the same data; only the subscription granularity changed.How did you test this?
New render-count test (
useSidebarSessionMap.test.tsx):useSessions())useSidebarSessionMap())computeSidebarSessionSignature.test.ts): the signature ignoresevents, changes on the 4 fields.pnpm --filter @posthog/core --filter @posthog/ui typecheck— clean.biome lint packages/core— clean, nonoRestrictedImports; host-boundaries unchanged.Automatic notifications
Created with PostHog Code