beads/internal/initprocs/initprocs.go
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

51 lines
1.4 KiB
Go

// Package initprocs runs package initialization on a few Ps rather than every
// CPU the host has.
//
// bd links several thousand packages whose init functions allocate about
// 20 MB before main runs, enough for a handful of garbage collections. Each
// collection starts a mark worker and stops the world across every P, which
// on a large host (192 CPUs on the machine this was measured on) costs more
// than the collection itself: about 20 ms of a 110 ms `bd version`.
//
// The package imports only runtime and syscall so that it initializes ahead of
// the heavy packages, and main calls Done before doing anything else.
package initprocs
import (
"runtime"
"syscall"
)
const initProcs = 4
var restore int
// setProcs and setDefault are runtime.GOMAXPROCS and
// runtime.SetDefaultGOMAXPROCS, swappable so a test can tell which one Done
// used.
var (
setProcs = runtime.GOMAXPROCS
setDefault = runtime.SetDefaultGOMAXPROCS
)
func init() {
if runtime.GOMAXPROCS(0) > initProcs {
restore = runtime.GOMAXPROCS(initProcs)
}
}
// Done returns GOMAXPROCS to what the process would have had without this
// package: the value GOMAXPROCS asked for when it is set, else the runtime's
// own default, which keeps tracking the CPU limit.
func Done() {
if restore == 0 {
return
}
n := restore
restore = 0
if _, set := syscall.Getenv("GOMAXPROCS"); set {
setProcs(n)
return
}
setDefault()
}