* 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>
54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package initprocs
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
func TestDoneRestoresGOMAXPROCS(t *testing.T) {
|
|
want := restore
|
|
if want == 0 {
|
|
t.Skipf("GOMAXPROCS was already at most %d; init left it alone", initProcs)
|
|
}
|
|
if got := runtime.GOMAXPROCS(0); got != initProcs {
|
|
t.Fatalf("GOMAXPROCS before Done = %d, want the init cap %d", got, initProcs)
|
|
}
|
|
Done()
|
|
if got := runtime.GOMAXPROCS(0); got != want {
|
|
t.Fatalf("GOMAXPROCS after Done = %d, want %d", got, want)
|
|
}
|
|
Done()
|
|
if got := runtime.GOMAXPROCS(0); got != want {
|
|
t.Fatalf("a second Done changed GOMAXPROCS to %d", got)
|
|
}
|
|
}
|
|
|
|
// TestDoneHonorsAnExplicitGOMAXPROCS tells the two hand-backs apart: with
|
|
// GOMAXPROCS set, Done restores that exact value; without it, Done hands
|
|
// GOMAXPROCS back to the runtime's default, which keeps tracking the CPU limit.
|
|
func TestDoneHonorsAnExplicitGOMAXPROCS(t *testing.T) {
|
|
saved, savedProcs, savedDefault := restore, setProcs, setDefault
|
|
t.Cleanup(func() { restore, setProcs, setDefault = saved, savedProcs, savedDefault })
|
|
var procs []int
|
|
defaults := 0
|
|
setProcs = func(n int) int { procs = append(procs, n); return n }
|
|
setDefault = func() { defaults++ }
|
|
|
|
t.Setenv("GOMAXPROCS", "7")
|
|
restore = 7
|
|
Done()
|
|
if len(procs) != 1 || procs[0] != 7 || defaults != 0 {
|
|
t.Fatalf("GOMAXPROCS=7: GOMAXPROCS calls %v, default calls %d; want GOMAXPROCS(7) alone", procs, defaults)
|
|
}
|
|
|
|
procs = nil
|
|
if err := os.Unsetenv("GOMAXPROCS"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
restore = 192
|
|
Done()
|
|
if len(procs) != 0 || defaults != 1 {
|
|
t.Fatalf("GOMAXPROCS unset: GOMAXPROCS calls %v, default calls %d; want SetDefaultGOMAXPROCS alone", procs, defaults)
|
|
}
|
|
}
|