Skip to content

perf: Mac Catalyst performance optimization — 50-150x faster session switching#165

Merged
PureWeen merged 8 commits intomainfrom
fix/catalyst-performance-optimization
Feb 21, 2026
Merged

perf: Mac Catalyst performance optimization — 50-150x faster session switching#165
PureWeen merged 8 commits intomainfrom
fix/catalyst-performance-optimization

Conversation

@PureWeen
Copy link
Owner

Problem

Mac Catalyst was very slow when:

  • Switching between sessions (729ms – 4027ms measured via [SwitchPerf] logs)
  • Sending messages
  • Expanding/collapsing grid view

Fast on Android/Windows and for new users — the issue scaled with accumulated data (753 session dirs, 463MB events.jsonl).

Root Causes Found & Fixed

1. JS draft capture running during every session switch (biggest impact)

_sessionSwitching flag was cleared prematurely in RefreshState() before SafeRefreshAsync() could read it. This caused an expensive round-trip JS interop call (querying all card inputs, capturing draft text, focus, cursor position) to execute on every single session switch.

Before: 729–4027ms per switch | After: 16–28ms (50-150x faster)

2. Synchronous disk I/O on every state change

  • SaveActiveSessionsToDisk(): sync read + merge + write of active-sessions.json called 7+ times per operation
  • SaveOrganization(): sync write called from 12+ locations
  • SaveUiState(): sync read-modify-write on UI thread

Fix: Timer-based debounce (2s for sessions/org, 1s for UI state) coalesces rapid saves. Flush on dispose.

3. GetOrganizedSessions() recalculated on every render

Created dictionaries, iterated all sessions per group, sorted — on every StateHasChanged().

Fix: Cache with hash-key invalidation (session count + group count + sort mode + per-session state).

4. LoadPersistedSessions() scanning 753 directories on every state change

RefreshSessions() called GetPersistedSessions() which reads workspace.yaml + events.jsonl from every session directory.

Fix: Only load persisted sessions on init and when the section is explicitly toggled open.

5. Markdown cache hash collisions + no eviction

Used GetHashCode() as key (collision risk). Capped at 500 with no eviction.

Fix: String content as key, LRU eviction at 1000 entries.

6. ReconcileOrganization() running unnecessarily

Full reconciliation (disk reads, directory checks) ran after every session operation even when nothing changed.

Fix: Hash-based skip when session set unchanged.

Test Results

  • 822 tests passing (0 failures)
  • MauiDevFlow verified: session switching 16-28ms, click processing 0-1ms
  • All 3 active sessions with large histories (964, 4705, 5821 msgs) render correctly

PureWeen and others added 7 commits February 21, 2026 07:12
- SaveActiveSessionsToDisk: 2s debounce timer coalesces rapid saves
- SaveOrganization: 2s debounce timer (called from 12+ places)
- SaveUiState: 1s debounce with pending state object
- ReconcileOrganization: skip if session set unchanged (hash check)
- GetOrganizedSessions: cached result invalidated by hash key
- Markdown cache: use string key (no hash collisions), LRU eviction at 1000

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
RefreshState was prematurely clearing _sessionSwitching before
SafeRefreshAsync could read it, causing the expensive JS draft
capture round-trip to execute during every session switch.

Before: 729-4027ms per switch
After: 16-28ms per switch (50-150x improvement)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
LoadPersistedSessions() was called from RefreshSessions() on every
OnStateChanged event, scanning all session directories for resumable
sessions. This scans workspace.yaml + events.jsonl for each dir.

Now only loads persisted sessions on init and when the section is
explicitly toggled open. Reduces steady-state I/O dramatically.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Two new domain knowledge skills for future agents:

- performance-optimization: 5 critical invariants (session switching,
  debounce flush, LoadPersistedSessions danger, cache invalidation,
  ReconcileOrganization skip guard), caching architecture, known
  optimization opportunities

- processing-state-safety: 9-item cleanup checklist, 8 IsProcessing
  paths table, 8 invariants, top 3 regression patterns, reference
  to full regression history across 7 PRs

Also adds brief performance pointers to copilot-instructions.md with
links to the skills for detailed reference.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tests cover:
- GetOrganizedSessions caching (same instance when unchanged)
- Cache invalidation on group add, sort mode change, group delete
- Organization operations (create, delete, move, pin, collapse)
- SaveUiState rapid-fire without corruption
- DisposeAsync safety (flush + idempotent)
- GetOrganizedSessions returns IReadOnlyList

836 total tests passing (822 existing + 14 new)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- LoadUiState returns pending debounced state instead of stale disk data
- DisposeAsync flushes pending UI state (was only disposing timer)
- SaveOrganization invalidates organized sessions cache immediately
- ReconcileOrganization hash is now order-independent (XOR)
- Added 6 edge case tests (842 total passing)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Snapshot JSON/entries on caller's thread before passing to Timer callback,
  preventing concurrent mutation of Organization lists and session History
- Add IsRestoring guard to FlushSaveActiveSessionsToDisk (prevents partial
  writes during app startup if dispose is called mid-restore)
- Replace XOR hash with additive hash in ReconcileOrganization (fewer collisions)
- Remove Task.Run wrappers from SaveUiState calls (no longer needed since
  SaveUiState is now in-memory + debounce timer, eliminates read-modify-write race)

Findings from: Opus 4.6, Codex 5.3, Sonnet 4.6 code reviews

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@PureWeen PureWeen force-pushed the fix/catalyst-performance-optimization branch from 25406e9 to 969ccaf Compare February 21, 2026 13:27
- SaveOrganization snapshot-on-caller doesn't throw under rapid mutations
- SaveOrganization state is captured before timer fires
- FlushSaveActiveSessionsToDisk respects IsRestoring during dispose
- ReconcileOrganization hash handles group create/delete churn
- SaveUiState is non-blocking (<500ms for 100 calls, returns latest state)
- Rapid group mutations don't corrupt organization state
- 883 total tests passing (26 in PerformanceOptimizationTests)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@PureWeen PureWeen merged commit 2cc091a into main Feb 21, 2026
1 check passed
@PureWeen PureWeen deleted the fix/catalyst-performance-optimization branch February 22, 2026 00:15
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