Skip to content

perf(sidebar): keep sidebar context values stable across renders - #3829

Draft
kalvinnchau wants to merge 1 commit into
mainfrom
peon/perf-sidebar-environment
Draft

kalvinnchau wants to merge 1 commit into
mainfrom
peon/perf-sidebar-environment

Conversation

@kalvinnchau

Copy link
Copy Markdown
Contributor

Problem

MessagesSidebar passed a new MessagesSidebarEnvironment value object on every render, and ChannelInventory passed a new TeamSidebarSections array 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

  • SidebarEnvironment memoizes the environment value on its fields. active is a module constant.
  • useSidebarView memoizes its returned view.
  • TeamSidebarSections hands sections to teamSidebarSource through a store whose identity stays fixed. The source reads it with useSyncExternalStore. 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 with ChannelInventory's focus layout effects.

Evidence

Perf harness background-traffic group at 4× CPU throttle, interleaved base/fix/base/fix ×3, 2 samples per run. Base is 46a09dbfd.

scenario base task ms/msg fix task ms/msg base script ms/msg fix script ms/msg
background-traffic-large-sidebar 19.3–20.1 16.8–19.3 11.1–11.7 8.6–9.9
background-traffic 13.7–15.0 13.1–14.5 7.9–8.7 7.4–8.1

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) and bin/pnpm check pass.

Deferred

  • Human test in the desktop app: sidebar ordering, collapse, drag reorder and keyboard focus after moving a channel.

🤖 Generated with Claude Code

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant