Files
Anthony ShewandGitHub aadb378602 perf: Defer TLS initialization to a background thread (#11967)
## 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)
2026-02-23 11:36:56 -07:00

20 lines
326 B
JSON

{
"extends": "@repo/typescript-config/nextjs.json",
"compilerOptions": {
"plugins": [
{
"name": "next"
}
],
"strictNullChecks": true
},
"include": [
"**/*.ts",
"**/*.tsx",
"next-env.d.ts",
"next.config.js",
".next/types/**/*.ts"
],
"exclude": ["node_modules"]
}