Commit 7ff598c
authored
🤖 perf: reduce memory pressure from streaming message aggregation (#1151)
## Summary
Performance optimizations to reduce memory pressure and GC pauses caused
by inefficient string handling during streaming.
## Problem
Heap analysis revealed:
- **582 MB in 30.5M concatenated strings** - each streaming delta was
stored as a separate part, and merging used repeated `+` concatenation
creating O(n²) intermediate strings
- **1.39s Major GC pauses** during animation events due to memory
pressure
- **35M total heap objects** accumulated during typical usage
## Changes
### 1. Stabilize `foregroundToolCallIds` Set reference
Avoid invalidating React Compiler memoization by checking content
equality before creating new Set instances.
### 2. Use `array.join()` for message part merging
Replace O(n²) string concatenation with array accumulation. V8 optimizes
`join()` much better than repeated `+`.
### 3. Compact message parts on stream end
When streaming completes, merge thousands of delta parts into single
strings immediately. This converts memory from O(deltas) small objects
to O(content_types) merged objects, preventing accumulation.
### 4. Extract `mergeAdjacentParts()` helper
Deduplicate the merge logic between `compactMessageParts()` and
`getDisplayedMessages()`.
## Validation
- `make static-check` passes
- `make typecheck` passes
- Heap timeline analysis confirms these patterns as the primary memory
sources
---
_Generated with `mux` • Model: `claude-sonnet-4-20250514` • Thinking:
`low`_1 parent 10e73fe commit 7ff598c
File tree
3 files changed
+90
-27
lines changed- docs
- src/browser
- hooks
- utils/messages
3 files changed
+90
-27
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
112 | 119 | | |
113 | 120 | | |
114 | 121 | | |
| |||
Lines changed: 81 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 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 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
77 | 137 | | |
78 | 138 | | |
79 | 139 | | |
| |||
288 | 348 | | |
289 | 349 | | |
290 | 350 | | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
291 | 360 | | |
292 | 361 | | |
293 | 362 | | |
| |||
545 | 614 | | |
546 | 615 | | |
547 | 616 | | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
548 | 621 | | |
549 | 622 | | |
550 | 623 | | |
| |||
587 | 660 | | |
588 | 661 | | |
589 | 662 | | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
590 | 666 | | |
591 | 667 | | |
592 | 668 | | |
| |||
606 | 682 | | |
607 | 683 | | |
608 | 684 | | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
609 | 688 | | |
610 | 689 | | |
611 | 690 | | |
| |||
957 | 1036 | | |
958 | 1037 | | |
959 | 1038 | | |
960 | | - | |
961 | | - | |
962 | | - | |
963 | | - | |
964 | | - | |
965 | | - | |
966 | | - | |
967 | | - | |
968 | | - | |
969 | | - | |
970 | | - | |
971 | | - | |
972 | | - | |
973 | | - | |
974 | | - | |
975 | | - | |
976 | | - | |
977 | | - | |
978 | | - | |
979 | | - | |
980 | | - | |
981 | | - | |
982 | | - | |
983 | | - | |
984 | | - | |
985 | | - | |
| 1039 | + | |
| 1040 | + | |
986 | 1041 | | |
987 | 1042 | | |
988 | 1043 | | |
| |||
0 commit comments