Commit 3d3def2
Turbopack: optimize SelfTimeTree performance, memory usage, and fix range query boundaries (#92931)
### What?
Optimize the `SelfTimeTree` data structure in the Turbopack trace server
for better performance and lower memory usage when loading large trace
files, and fix a correctness bug in range queries.
### Why?
Two issues were identified:
1. **Performance**: The tree was splitting and rebalancing on every
insert during bulk loading, and each `distribute_entries` call
recursively triggered further split checks per entry, creating
significant overhead.
2. **Correctness**: Range query boundary conditions used exclusive
comparisons (`<`/`>`), incorrectly excluding entries that touched the
exact start or end timestamp of the queried range.
### How?
**Performance optimizations:**
1. **Batch split checks**: Add `insert_without_check` for inserting
entries without triggering a split check per entry. In
`distribute_entries`, entries are moved to children using
`insert_without_check`, and `check_for_split` is called once per child
after all entries are distributed (rather than once per entry).
2. **Lazy optimization with memory reclamation**: Add an `optimize()`
method and a `for_each_in_range_optimize()` traversal method that
distribute and rebalance nodes lazily:
- `store.optimize()` is called when the reader reaches end-of-data
(before waiting for more), doing a bulk pass to properly structure the
tree after initial load. After distributing entries to children, each
node calls `shrink_to_fit()` on its entries vec to release excess
capacity.
- `for_each_in_range_optimize()` distributes and rebalances each node it
visits, so query paths are progressively optimized during normal use.
3. **Fix `rebalance()` bug**: `check_for_split()` was incorrectly called
after merging subtrees during rebalance — replaced with
`distribute_entries()` since the node already has children and needs
redistribution, not a fresh split.
**Correctness fix:**
All range boundary comparisons in `for_each_in_range`,
`for_each_in_range_optimize`, and `lookup_range_count` updated from
exclusive (`<`/`>`) to inclusive (`<=`/`>=`), ensuring entries with
timestamps exactly equal to the range endpoints are included in results.
<!-- NEXT_JS_LLM_PR -->
---------
Co-authored-by: Tobias Koppers <sokra@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>1 parent e2e8ba9 commit 3d3def2
3 files changed
Lines changed: 60 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
| 213 | + | |
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
| |||
308 | 309 | | |
309 | 310 | | |
310 | 311 | | |
| 312 | + | |
311 | 313 | | |
312 | 314 | | |
313 | 315 | | |
| |||
Lines changed: 51 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
56 | 61 | | |
57 | 62 | | |
58 | 63 | | |
| |||
66 | 71 | | |
67 | 72 | | |
68 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
69 | 85 | | |
70 | 86 | | |
71 | 87 | | |
| |||
100 | 116 | | |
101 | 117 | | |
102 | 118 | | |
103 | | - | |
| 119 | + | |
104 | 120 | | |
105 | 121 | | |
106 | | - | |
| 122 | + | |
107 | 123 | | |
108 | 124 | | |
109 | 125 | | |
110 | 126 | | |
111 | 127 | | |
112 | 128 | | |
| 129 | + | |
| 130 | + | |
113 | 131 | | |
114 | 132 | | |
115 | 133 | | |
| |||
188 | 206 | | |
189 | 207 | | |
190 | 208 | | |
191 | | - | |
| 209 | + | |
192 | 210 | | |
193 | 211 | | |
194 | 212 | | |
| |||
198 | 216 | | |
199 | 217 | | |
200 | 218 | | |
201 | | - | |
| 219 | + | |
202 | 220 | | |
203 | 221 | | |
204 | 222 | | |
205 | 223 | | |
206 | 224 | | |
207 | 225 | | |
208 | 226 | | |
209 | | - | |
| 227 | + | |
210 | 228 | | |
211 | 229 | | |
212 | | - | |
| 230 | + | |
213 | 231 | | |
214 | 232 | | |
215 | 233 | | |
| |||
225 | 243 | | |
226 | 244 | | |
227 | 245 | | |
228 | | - | |
| 246 | + | |
229 | 247 | | |
230 | 248 | | |
231 | 249 | | |
| |||
260 | 278 | | |
261 | 279 | | |
262 | 280 | | |
263 | | - | |
| 281 | + | |
264 | 282 | | |
265 | | - | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
266 | 296 | | |
267 | 297 | | |
268 | | - | |
269 | | - | |
| 298 | + | |
| 299 | + | |
270 | 300 | | |
271 | 301 | | |
272 | 302 | | |
273 | 303 | | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
274 | 308 | | |
275 | | - | |
| 309 | + | |
276 | 310 | | |
277 | 311 | | |
278 | 312 | | |
279 | | - | |
280 | | - | |
281 | | - | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
282 | 316 | | |
283 | | - | |
284 | | - | |
| 317 | + | |
| 318 | + | |
285 | 319 | | |
286 | 320 | | |
287 | 321 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
94 | 100 | | |
95 | 101 | | |
96 | 102 | | |
| |||
192 | 198 | | |
193 | 199 | | |
194 | 200 | | |
195 | | - | |
| 201 | + | |
196 | 202 | | |
197 | 203 | | |
198 | 204 | | |
| |||
0 commit comments