Commit 04ed053
committed
Cache flattenNodes() in View to eliminate redundant tree walks
During token streaming, every tree update caused multiple O(n) tree
traversals: once inside the View's _onTreeUpdate(), then again for
each React hook subscriber calling view.flattenNodes(). With rapid
streaming tokens this multiplied into a significant performance cost.
The View now caches the flattenNodes() result in a _cachedNodes field.
The public flattenNodes() returns this cache in O(1). A new private
_computeFlatNodes() method performs the actual tree walk and is called
only when the visible output may have changed: tree structural changes,
branch selection changes, fork auto-selection, and history page reveal.
The original design avoided caching ("no cache invalidation complexity,
at the cost of repeated traversals") because the result depends on
branch selection state. Caching is safe here because every mutation
path that can change the visible output refreshes the cache
synchronously before emitting events to consumers - JS single-threading
eliminates async staleness risks.1 parent 85506b4 commit 04ed053
File tree
4 files changed
+53
-12
lines changed- docs/internals
- src/core/transport
- test/core/transport
4 files changed
+53
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | | - | |
| 125 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
| 121 | + | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
124 | 124 | | |
125 | | - | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
126 | 131 | | |
127 | | - | |
| 132 | + | |
128 | 133 | | |
129 | 134 | | |
130 | 135 | | |
| |||
133 | 138 | | |
134 | 139 | | |
135 | 140 | | |
| 141 | + | |
| 142 | + | |
136 | 143 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
148 | 155 | | |
149 | 156 | | |
150 | 157 | | |
| |||
159 | 166 | | |
160 | 167 | | |
161 | 168 | | |
162 | | - | |
163 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
164 | 172 | | |
165 | 173 | | |
166 | 174 | | |
| |||
186 | 194 | | |
187 | 195 | | |
188 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
189 | 208 | | |
190 | 209 | | |
191 | 210 | | |
| |||
255 | 274 | | |
256 | 275 | | |
257 | 276 | | |
258 | | - | |
| 277 | + | |
| 278 | + | |
259 | 279 | | |
260 | 280 | | |
261 | 281 | | |
| |||
311 | 331 | | |
312 | 332 | | |
313 | 333 | | |
314 | | - | |
| 334 | + | |
| 335 | + | |
315 | 336 | | |
316 | 337 | | |
317 | 338 | | |
| |||
584 | 605 | | |
585 | 606 | | |
586 | 607 | | |
587 | | - | |
| 608 | + | |
| 609 | + | |
588 | 610 | | |
589 | 611 | | |
590 | 612 | | |
| |||
621 | 643 | | |
622 | 644 | | |
623 | 645 | | |
624 | | - | |
| 646 | + | |
625 | 647 | | |
626 | 648 | | |
627 | 649 | | |
| 650 | + | |
628 | 651 | | |
629 | 652 | | |
630 | 653 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1176 | 1176 | | |
1177 | 1177 | | |
1178 | 1178 | | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
1179 | 1190 | | |
1180 | 1191 | | |
1181 | 1192 | | |
| |||
0 commit comments