Repository navigation
perf(sidebar): keep sidebar context values stable across renders - #3829
Draft
kalvinnchau wants to merge 1 commit into
Draft
kalvinnchau wants to merge 1 commit into
kalvinnchau wants to merge 1 commit into
Conversation
the messages sidebar environment and team sidebar sections were new objects on every render. react's lazy context propagation then searched every memoized sidebar subtree that skipped rendering for consumers on each background message. memoize the environment value and the sidebar view result, and pass team sections through a stable store read with useSyncExternalStore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Kalvin Chau <kalvin@block.xyz>
This branch has not been deployed
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
MessagesSidebarpassed a newMessagesSidebarEnvironmentvalue object on every render, andChannelInventorypassed a newTeamSidebarSectionsarray on every render. When an incoming background message re-rendered the sidebar, React 19's lazy context propagation walked the whole subtree of every memoized sidebar child that skipped rendering, looking for consumers of the changed contexts. With a large sidebar, that walk was the main cost of a background message.Change
SidebarEnvironmentmemoizes the environment value on its fields.activeis a module constant.useSidebarViewmemoizes its returned view.TeamSidebarSectionshands sections toteamSidebarSourcethrough a store whose identity stays fixed. The source reads it withuseSyncExternalStore. The value is written during render, so a source rendering in the same pass reads current sections. A layout effect notifies any source that skipped rendering before paint, which keeps section DOM in step withChannelInventory's focus layout effects.Evidence
Perf harness
background-trafficgroup at 4× CPU throttle, interleaved base/fix/base/fix ×3, 2 samples per run. Base is46a09dbfd.Fiber census of the large-sidebar run: context-propagation walks under bailed sidebar memos dropped to 0. The warm channel-switch group (channel, large sidebar, DM) was flat.
Tests:
vitest run src/bundled/channels(66 files, 1353 tests) andbin/pnpm checkpass.Deferred
🤖 Generated with Claude Code