From 4d908753d2e7fd59d8043d5ff5d0a5833e13fa79 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 6 Jul 2026 16:59:20 +0800 Subject: [PATCH] cltest: share one go-build cache across captured runs Every RunAndCapture created a fresh temp GOCACHE and deleted it after, so each of the ~170 golden-dir run tests re-typechecked the entire dependency graph from scratch. Share one per-process temp cache instead: isolation from the developer cache is unchanged (still a fresh temp dir per test process), and the go build cache is content+flag keyed, so sharing across build configs is safe. Measured on darwin/arm64 (controlled A/B, run-mode _testrt suite, 63 dirs): 157.8s -> 119.6s (-24%). Full ./cl and ./ssa suites green with the change. The win should be larger on CI mac runners, where the per-test cache churn hits slow disks; the mac coverage job's cl package alone runs 37-45 minutes there. --- cl/cltest/cltest.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/cl/cltest/cltest.go b/cl/cltest/cltest.go index 51f199bf94..c59db54e24 100644 --- a/cl/cltest/cltest.go +++ b/cl/cltest/cltest.go @@ -33,6 +33,7 @@ import ( "runtime" "slices" "strings" + "sync" "testing" "github.com/goplus/gogen/packages" @@ -313,6 +314,21 @@ func testRunAndTestFrom(t *testing.T, pkgDir, relPkg, sel string, opts runOption } } +// sharedGoCacheDir returns a per-process go-build cache reused by every +// captured run. Isolation from the developer cache is preserved (fresh +// temp dir per test process); sharing across tests avoids re-typechecking +// the whole dependency graph for each of the ~170 golden dirs. +var goCacheOnce sync.Once +var goCacheDir string +var goCacheErr error + +func sharedGoCacheDir() (string, error) { + goCacheOnce.Do(func() { + goCacheDir, goCacheErr = os.MkdirTemp("", "llgo-gocache-*") + }) + return goCacheDir, goCacheErr +} + func RunAndCapture(relPkg, pkgDir string) ([]byte, error) { conf := build.NewDefaultConf(build.ModeRun) return RunAndCaptureWithConf(relPkg, pkgDir, conf) @@ -376,11 +392,10 @@ func runWithConf(relPkg, pkgDir string, conf *build.Config) ([]byte, error) { } func doWithConf(relPkg, pkgDir string, conf *build.Config, action string) ([]byte, error) { - cacheDir, err := os.MkdirTemp("", "llgo-gocache-*") + cacheDir, err := sharedGoCacheDir() if err != nil { return nil, err } - defer os.RemoveAll(cacheDir) oldCache := os.Getenv("GOCACHE") if err := os.Setenv("GOCACHE", cacheDir); err != nil { return nil, err