Commit d1bc384
committed
🤖 Optimize startup performance and fix 3s freeze
Reduces startup time from 8.6s to <500ms and eliminates the 3s "evil spinner"
that blocked the UI after the window appeared.
Root Cause Analysis:
- AIService was importing the massive "ai" package (~3s) during startup
- Git status IPC handler triggered AIService lazy-load immediately after mount
- Combined effect: window appeared but then froze for 3s before becoming interactive
Key Performance Fixes:
1. Lazy-load AIService in IpcMain - defer AI SDK import until first actual use
2. Fix git status IPC to use Config.findWorkspace() instead of AIService
3. Defer git status polling to next tick (non-blocking on mount)
4. Defer auto-resume checks to next tick (non-blocking on mount)
5. Make React DevTools installation non-blocking in main process
Token Stats Refactoring:
- Remove ~425 LoC of approximation infrastructure (tokenStats.worker, etc.)
- Move token calculation to backend via IPC (STATS_CALCULATE handler)
- Add tokenizerWorkerPool for non-blocking tokenization in main process
- Eliminate ChatProvider context - move stats to TokenConsumerBreakdown component
- Add per-model cache keys to prevent cross-model contamination
Bundle Optimizations:
- Lazy load Mermaid component to defer 631KB chunk
- Disable production source maps (saves ~50MB in .app)
- Add manual chunks for better caching (react-vendor, syntax-highlighter)
- Remove worker bundle checks (worker removed in refactoring)
Import Linting Enhancements:
- Enhanced check_eager_imports.sh to detect AI SDK in critical startup files
- Added renderer/worker checks to prevent heavy packages (ai-tokenizer, models.json)
- CI guards for bundle sizes (400KB main budget)
Performance Results:
- Startup: 8.6s → <500ms (94% improvement)
- Window: appears instantly, no freeze
- AI SDK: loads on-demand when first message sent
- Git status: non-blocking background operation
Testing:
- All 528 tests pass
- Integration tests for stats:calculate IPC handler
- Tokenizer cache isolation tests
- StreamingTokenTracker model-change safety tests
_Generated with `cmux`_1 parent 8de6d12 commit d1bc384
File tree
35 files changed
+1665
-805
lines changed- .github/workflows
- scripts
- src
- components
- ChatMetaSidebar
- Messages
- constants
- contexts
- hooks
- services
- stores
- types
- utils
- main
- tokens
- workers
- tests/ipcMain
35 files changed
+1665
-805
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
| 113 | + | |
113 | 114 | | |
114 | 115 | | |
115 | | - | |
116 | | - | |
| 116 | + | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
| 2 | + | |
5 | 3 | | |
6 | 4 | | |
7 | | - | |
| 5 | + | |
8 | 6 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
| 7 | + | |
| 8 | + | |
13 | 9 | | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
| 10 | + | |
22 | 11 | | |
23 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
24 | 18 | | |
25 | | - | |
26 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
27 | 23 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
35 | 27 | | |
36 | 28 | | |
37 | 29 | | |
38 | | - | |
| 30 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | | - | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
14 | | - | |
15 | | - | |
| 16 | + | |
| 17 | + | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
21 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
22 | 34 | | |
23 | 35 | | |
24 | | - | |
| 36 | + | |
25 | 37 | | |
26 | 38 | | |
27 | 39 | | |
28 | 40 | | |
29 | 41 | | |
30 | 42 | | |
31 | | - | |
| 43 | + | |
32 | 44 | | |
33 | 45 | | |
34 | 46 | | |
| |||
40 | 52 | | |
41 | 53 | | |
42 | 54 | | |
43 | | - | |
44 | | - | |
| 55 | + | |
| 56 | + | |
45 | 57 | | |
46 | 58 | | |
47 | 59 | | |
| |||
50 | 62 | | |
51 | 63 | | |
52 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
53 | 126 | | |
54 | 127 | | |
55 | | - | |
56 | | - | |
57 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
58 | 137 | | |
59 | 138 | | |
60 | 139 | | |
61 | | - | |
| 140 | + | |
0 commit comments