Commit b3c0f46
authored
perf: Parallelize task hash computation across topological waves (#11969)
## Summary
Moves task hash computation out of the serial dispatch loop and into a
parallel pre-computation phase using rayon. Tasks are grouped into
topological waves by dependency depth, and each wave is hashed
concurrently. Also parallelizes external dependency hash precomputation.
Previously, every task's hash and execution environment were computed
one-at-a-time inside the `while let` visitor dispatch loop. With ~1700
tasks at ~160μs each, this was ~270ms of serial work on the critical
path. Now, the same work completes in ~53ms using rayon's thread pool.
Also cleans up `Engine::dependencies()`/`dependents()` to return `Vec`
instead of `HashSet`, and `calculate_dependency_hashes` to take a
`&[&TaskNode]` slice, since all callers only iterate — no set operations
are performed.
## Benchmarks
Profiled with `--profile` across three monorepos of varying size (full
cache hit scenario, median of 5 warm runs):
| Repo | Tasks | Baseline | After | Delta |
|------|-------|----------|-------|-------|
| Small (~5 packages) | 15 | 170ms | 169ms | ~flat |
| Medium (~125 packages) | 700 | 876ms | 876ms | ~flat |
| Large (~1000 packages) | 1700 | 867ms | 775ms | **-92ms (-10.6%)** |
`queue_task` self-time in the large repo dropped from **268ms to 78ms**
(71% reduction). The improvement scales with task count.
## Changes
- **`crates/turborepo-lib/src/task_graph/visitor/mod.rs`** — Added
`precompute_task_hashes()` which groups tasks into topological waves and
hashes each wave in parallel via rayon. The dispatch loop now looks up
pre-computed hashes instead of computing them inline.
- **`crates/turborepo-engine/src/lib.rs`** — `dependencies()`,
`dependents()`, and `neighbors()` return `Vec` instead of `HashSet`.
Updated `EngineInfo` impl's associated type accordingly.
- **`crates/turborepo-task-hash/src/lib.rs`** — `calculate_task_hash`
takes `&[&TaskNode]` instead of `HashSet<&TaskNode>`.
`calculate_dependency_hashes` takes a slice.
`precompute_external_deps_hashes` uses `par_iter` instead of a
sequential loop.1 parent 69a89b3 commit b3c0f46
3 files changed
+155
-41
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
417 | 417 | | |
418 | 418 | | |
419 | 419 | | |
420 | | - | |
| 420 | + | |
421 | 421 | | |
422 | 422 | | |
423 | 423 | | |
424 | | - | |
| 424 | + | |
425 | 425 | | |
426 | 426 | | |
427 | 427 | | |
| |||
468 | 468 | | |
469 | 469 | | |
470 | 470 | | |
471 | | - | |
| 471 | + | |
472 | 472 | | |
473 | 473 | | |
474 | 474 | | |
| |||
521 | 521 | | |
522 | 522 | | |
523 | 523 | | |
524 | | - | |
| 524 | + | |
525 | 525 | | |
526 | 526 | | |
527 | 527 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
184 | 184 | | |
185 | 185 | | |
186 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
187 | 305 | | |
188 | 306 | | |
189 | 307 | | |
| |||
194 | 312 | | |
195 | 313 | | |
196 | 314 | | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
197 | 325 | | |
198 | 326 | | |
199 | 327 | | |
| |||
222 | 350 | | |
223 | 351 | | |
224 | 352 | | |
225 | | - | |
226 | | - | |
227 | 353 | | |
228 | 354 | | |
229 | 355 | | |
230 | 356 | | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
231 | 360 | | |
232 | 361 | | |
233 | 362 | | |
| |||
245 | 374 | | |
246 | 375 | | |
247 | 376 | | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
262 | 382 | | |
263 | 383 | | |
264 | 384 | | |
| |||
284 | 404 | | |
285 | 405 | | |
286 | 406 | | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | 407 | | |
295 | 408 | | |
296 | 409 | | |
| |||
301 | 414 | | |
302 | 415 | | |
303 | 416 | | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | 417 | | |
310 | 418 | | |
311 | 419 | | |
| |||
319 | 427 | | |
320 | 428 | | |
321 | 429 | | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
322 | 433 | | |
323 | 434 | | |
324 | 435 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
297 | 297 | | |
298 | 298 | | |
299 | 299 | | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
305 | 308 | | |
306 | 309 | | |
307 | 310 | | |
| |||
311 | 314 | | |
312 | 315 | | |
313 | 316 | | |
314 | | - | |
| 317 | + | |
315 | 318 | | |
316 | 319 | | |
317 | 320 | | |
| |||
453 | 456 | | |
454 | 457 | | |
455 | 458 | | |
456 | | - | |
| 459 | + | |
457 | 460 | | |
458 | 461 | | |
459 | 462 | | |
| |||
462 | 465 | | |
463 | 466 | | |
464 | 467 | | |
465 | | - | |
| 468 | + | |
466 | 469 | | |
467 | 470 | | |
468 | 471 | | |
| |||
0 commit comments