Repository navigation
perf(nix): compile cohort test binaries once, share via GOCACHE - #1292
Conversation
The 5 flake-check cohorts each recompiled the race+coverage-instrumented test binaries from scratch (Go's build cache isn't shared across Nix sandboxes). The 4 backend cohorts compile byte-identical binaries — cgo mattn/go-sqlite3 under -race, ~2 min apiece on CI's 4-core runners — so 3 of those compiles were pure waste that core contention couldn't parallelize away (dd9622c's 8m->14m flake-check regression). Add a _ncps-test-cache derivation that compiles both cohort test sets once (`go test -run '^$'`) and exports the populated GOCACHE; each cohort seeds a writable copy of it before `go test`, turning compilation into a cache hit. Cohort isolation, per-cohort cover.out, the merged Codecov profile, the race detector, and t.Skip gating are all unchanged. Verified: cache builds (821M); cmd cohort passes reusing the cache (compile served from cache, not rebuilt). The postgres cohort's local TestSchemaEquivalence failure is environmental — it fails identically without this change and passes in CI. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
This change is part of the following stack: Change managed by git-spice. |
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR introduces a shared Go test compilation cache that compiles race+coverage-instrumented test binaries once in a dedicated derivation, then has all cohort derivations consume that cache to avoid redundant recompilation. The Nix flake module is refactored to factor compile-affecting flags, seed writable caches in cohorts, and expose the cache derivation as a Nix package. Supporting design, proposal, and specification documents are added to the OpenSpec archive. ChangesShared Test Cache Infrastructure
Sequence DiagramsequenceDiagram
participant testCache as testCache<br/>(one-time build)
participant cohort1 as Backend Cohort 1<br/>(e.g., postgres)
participant cohort2 as Backend Cohort 2<br/>(e.g., mysql)
participant cohort3 as Cmd Cohort<br/>(unit tests)
participant gotest as go test
testCache->>testCache: Compile race+coverage<br/>test binaries once<br/>(go test -run '^$')
testCache->>testCache: Export GOCACHE
cohort1->>cohort1: Copy shared GOCACHE<br/>to writable preCheck cache
cohort1->>gotest: Run go test<br/>(GOCACHE hits)
gotest->>cohort1: Reuse compiled objects<br/>Skip recompilation
cohort2->>cohort2: Copy shared GOCACHE<br/>to writable preCheck cache
cohort2->>gotest: Run go test<br/>(GOCACHE hits)
gotest->>cohort2: Reuse compiled objects<br/>Skip recompilation
cohort3->>cohort3: Copy shared GOCACHE<br/>to writable preCheck cache
cohort3->>gotest: Run go test<br/>(GOCACHE hits)
gotest->>cohort3: Reuse compiled objects<br/>Skip recompilation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Code Review
This pull request optimizes the CI pipeline by introducing a shared Go test compilation cache (_ncps-test-cache) in Nix. Instead of each test cohort recompiling the race- and coverage-instrumented binaries from scratch, the binaries are compiled once in a dedicated derivation and shared via a populated GOCACHE. This is integrated into nix/checks/flake-module.nix and documented with design, proposal, and specification files under openspec/. There are no review comments to address.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
openspec/changes/archive/2026-05-29-speed-up-flake-check/design.md (1)
36-36: ⚡ Quick winAdd language identifiers to fenced code blocks.
markdownlint(MD040) flags both fences; addingbashkeeps docs lint-clean.Proposed patch
-``` +```bash export GOCACHE=$TMPDIR/gocache # backend cohort binary set go test -race -count=1 -covermode=atomic \ -coverpkg=./pkg/...,./internal/...,./migrations/...,./testhelper/... \ -run '^$' ./pkg/... ./internal/... ./migrations/... ./testhelper/... # cmd cohort binary set go test -race -count=1 -covermode=atomic -coverpkg=./cmd/...,./ent/...,. \ -run '^$' ./cmd/... ./ent/... . cp -r "$GOCACHE" "$out"-
+bash
export GOCACHE="$TMPDIR/gocache"
cp -r --no-preserve=mode,ownership ${testCache}/* "$GOCACHE"/Also applies to: 56-56
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@openspec/changes/archive/2026-05-29-speed-up-flake-check/design.md` at line 36, Update the two fenced code blocks in the design document so their opening fences include the bash language identifier (change ``` to ```bash for the blocks containing the GOCACHE/export snippets); ensure both occurrences (the block with the go test commands and the block with cp -r --no-preserve usage) are updated so markdownlint MD040 is satisfied.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@openspec/changes/archive/2026-05-29-speed-up-flake-check/design.md`:
- Line 36: Update the two fenced code blocks in the design document so their
opening fences include the bash language identifier (change ``` to ```bash for
the blocks containing the GOCACHE/export snippets); ensure both occurrences (the
block with the go test commands and the block with cp -r --no-preserve usage)
are updated so markdownlint MD040 is satisfied.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: e754c83c-571b-4883-95bd-74b9802d9fd3
📒 Files selected for processing (7)
nix/checks/flake-module.nixopenspec/changes/archive/2026-05-29-speed-up-flake-check/.openspec.yamlopenspec/changes/archive/2026-05-29-speed-up-flake-check/design.mdopenspec/changes/archive/2026-05-29-speed-up-flake-check/proposal.mdopenspec/changes/archive/2026-05-29-speed-up-flake-check/specs/flake-check-topology/spec.mdopenspec/changes/archive/2026-05-29-speed-up-flake-check/tasks.mdopenspec/specs/flake-check-topology/spec.md
## Why After compile-once (#1292) CI is ~10m, bottlenecked by a single `nix flake check` building **~13 check derivations on one contended 4-core runner**. They're logically parallel but CPU-starved; the remaining costs (backend-cohort test execution, golangci's own analysis) aren't cacheable — they need more cores in parallel. ## What Replace the monolithic check with a **CI matrix**: - `check-matrix` job enumerates checks: `nix eval .#checks.x86_64-linux --apply builtins.attrNames --json`. - `checks` job runs each as its own parallel runner: `nix build .#checks.x86_64-linux.<name>` (`fail-fast: false`), pulling the shared `_ncps-test-cache`/`_ncps-base` from Cachix instead of rebuilding. - `coverage` job (`needs: checks`) merges per-cohort profiles → Codecov. - The shared build job sets **`run_flake_check: false`** (kalbasit/gh-actions#9) so it only builds/pushes the OCI image. Wall-clock collapses from sum-under-contention (~10m) to **slowest-single-check + nix setup (~3–4m)** — the path to the ~5m target. The per-job times on this run also give us the first clean per-check breakdown. ##⚠️ Merge order Depends on **kalbasit/gh-actions#9** (adds `run_flake_check`). The caller is **temporarily pinned to the gh-actions branch** to validate now; revert to `@main` before merging once #9 lands. OpenSpec: `flake-check-topology` gains "CI runs each check as an independent parallel job" + "Coverage is produced by a job consuming the fanned-out cohorts". --------- Co-authored-by: Claude Opus 4.8 <[email protected]>
Why
After #1291, the
x86_64-linuxbuild leg (~17m) is CI's bottleneck, and the "Flake check" step is 16.3m of it (measured, run 26661047200). dd9622c split flake-check into 5 cohort derivations, each of which recompiles the race+coverage-instrumented test binaries from scratch — Go's build cache isn't shared across Nix sandboxes. The 4 backend cohorts compile byte-identical binaries (cgomattn/go-sqlite3under-race, ~2 min apiece on CI's 4-core runners), so 3 of those compiles are pure waste that core contention can't parallelize away (this is dd9622c's 8m→14m regression).What
Add a
_ncps-test-cachederivation that compiles both cohort test sets exactly once (go test -run '^$', runs zero tests so no backend is needed) and exports the populatedGOCACHE. Each cohort seeds a writable copy of that cache before itsgo test, turning compilation into a cache hit. Cohort isolation, per-cohortcover.out, the merged Codecov profile, the race detector, andt.Skipgating are all unchanged.Two Nix-specific details (documented in the code):
disallowedReferences = [ ]— the cache legitimately references the Go toolchain.dontFixup = true— patchelf/strip would rewrite cached objects and invalidate Go's content hashes.chmod -R u+w—--no-preserve=modewould strip thecovdatahelper's +x bit and break coverage.Verification
_ncps-test-cachebuilds (821M cache).covdatabinary, proving compilation was served from cache, not rebuilt).TestSchemaEquivalencefailure is environmental: it fails identically with this change stashed, and the cohort passes in CI on this branch.This PR's own CI run will give the real before/after x86-leg number.
OpenSpec:
flake-check-topologyspec gains "Race+coverage test binaries are compiled once and shared".