mirror of
https://github.com/hanzoai/turborepo.git
synced 2026-10-30 19:44:51 +00:00
## Summary TLS initialization (~95ms) previously blocked the critical path at the start of every `turbo run`. This PR moves it to a background thread so it overlaps with package graph construction, SCM indexing, and other startup work. ## How it works 1. `cli::run` spawns TLS init on `spawn_blocking` immediately — before arg parsing 2. Telemetry uses a new `DeferredTelemetryClient` that resolves the HTTP client lazily on first flush, so telemetry init never blocks 3. `RunBuilder::build()` resolves the HTTP client via a shared `OnceCell` after the package graph and SCM tasks have been running concurrently — by that point, TLS has had the full startup pipeline to complete in the background 4. Analytics startup moved inside `build()` since it needs the resolved API client, so `build()` now returns `(Run, Option<AnalyticsHandle>)` ## Profiling results Measured with `--profile` on three repos of different sizes. The key metric is `resolve_api_client` — how long the main thread blocks waiting for TLS init after all the overlapping work has completed. Lower is better (0ms = fully hidden). | Repo size | `build_http_client` (TLS) | `resolve_api_client` (wait) | Critical path savings | |---|---|---|---| | ~1000 packages | 91ms | 0ms | **91ms** | | ~125 packages | 94ms | 26ms | **68ms** | | ~6 packages | 92ms | 89ms | **3ms** | Savings scale with repo size: larger repos have more `build()` work (package graph construction, lockfile parsing, SCM indexing) running concurrently with TLS init. Small repos see minimal improvement since there's almost nothing to overlap with, but they don't regress — the worst case is parity with the baseline. ## Testing - `cargo check --workspace` clean (zero errors, zero warnings) - `cargo test -p turborepo-api-client --lib` — 18 passed - Verified `--profile` captures `resolve_api_client`, `http_client_init`, and `build_http_client` spans correctly - Verified only one `build_http_client` call occurs (OnceCell deduplication works)