beads/internal/initprocs
Julian Knutsen d28f56cd46
perf(startup): initialize packages on four Ps, not every CPU (#7701)
* perf(startup): initialize packages on four Ps, not every CPU

bd links several thousand packages whose init functions allocate about
20 MB before main runs, which triggers four or five garbage collections.
Each one starts mark workers and stops the world across every P, and on a
large host that costs more than the collection itself: on a 192-CPU machine
`bd version` spent about 20 of its 110 ms there, paid again by every bd
process a script or an orchestrator starts.

internal/initprocs lowers GOMAXPROCS to 4 for the duration of package
initialization. It imports only runtime and syscall, so it initializes
ahead of the heavy packages, and main hands GOMAXPROCS back first thing:
to the GOMAXPROCS environment value when one is set, else to the runtime
default, which keeps tracking the CPU limit.

cmd/bd's TestMain does the same hand-back, since tests never run main, and
resets the -test.parallel default testing derived from the capped count.

Measured on that host (40 runs each, `bd version`): p50 103 -> 86 ms.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of gascity-agent
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(initprocs): tell an explicit GOMAXPROCS hand-back from the runtime default

Review follow-up. TestDoneHonorsAnExplicitGOMAXPROCS swaps the two runtime
calls for recorders and pins that Done restores the exact value when
GOMAXPROCS is set and calls SetDefaultGOMAXPROCS when it is not; the earlier
test only saw the resulting count, which is the same either way on a host
whose default equals the saved value.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of gascity-agent
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-19 17:15:54 +00:00
..
BUILD.bazel perf(startup): initialize packages on four Ps, not every CPU (#7701) 2026-10-19 17:15:54 +00:00
initprocs.go perf(startup): initialize packages on four Ps, not every CPU (#7701) 2026-10-19 17:15:54 +00:00
initprocs_test.go perf(startup): initialize packages on four Ps, not every CPU (#7701) 2026-10-19 17:15:54 +00:00