Skip to content

Add shallow equality guards to MessageBlockStore to prevent unnecessary renders - #1222

Open
nordicnode wants to merge 2 commits into
CodebuffAI:mainfrom
nordicnode:perf-message-block-equality-guard
Open

Add shallow equality guards to MessageBlockStore to prevent unnecessary renders#1222
nordicnode wants to merge 2 commits into
CodebuffAI:mainfrom
nordicnode:perf-message-block-equality-guard

Conversation

@nordicnode

@nordicnode nordicnode commented Sep 2, 2026

Copy link
Copy Markdown

Add shallow equality guards to MessageBlockStore to prevent unnecessary renders

Summary

• In cli/src/state/message-block-store.ts, added shallow equality guards to setContext and setCallbacks to prevent state reference churn and eliminate spurious notifications to store subscribers and context consumers.
• Previously, setContext (triggered on every layout effect in <Chat />) drafted and replaced state.context with a new object reference even when all property values were identical. Because Zustand notifies store subscribers whenever a new state reference is created by Immer, this triggered re-render passes for any components and listeners consuming the store or context directly.
• In setContext, added a key-by-key shallow equality guard for merged properties.
• In setCallbacks, respected full replacement semantics by checking key set length before checking individual values, ensuring that omitted callback keys trigger replacement rather than being silently preserved.
• Exported and shared a single EMPTY_RESPONSE_ADS module constant between initialContext and chat.tsx, ensuring stable object reference identity when ads are disabled with zero allocation.
• Added unit test suite in cli/src/state/__tests__/message-block-store.test.ts verifying that subscribers fire only on true mutations, are suppressed on identical updates, and handle omitted keys in setCallbacks.

Test plan

[✓] bun test --config=/dev/null src/state/__tests__/message-block-store.test.ts — 4 pass, 0 fail
[✓] bun run --cwd cli typecheck — 0 errors
[✓] PR hygiene check passed

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