Precompile bits/stdc++.h in the runner, and move it to clang-tidy 22
Test and publish the site / test (push) Successful in 1m25s
Test and publish the site / publish (push) Successful in 5m18s

The first publish failed its smoke test: on the production host
(2 slow cores) clang-tidy took over 30s on a two-line file and hit the
runner's 20s lint limit, and even a trivial build took 4s, nearly all
of it parsing <bits/stdc++.h>.

The runner image now precompiles that header for g++ (.gch) and clang
(.pch) with the flags the runner passes, and the runner uses them when
they are there. g++ lint gains -O2 so the build's header matches it.
Alpine 3.24's clang-tidy 22 walks far less of the system headers than
3.22's clang-tidy 20. Measured on the host: a build 3.9s to 0.6s, and
clang-tidy 32.5s to 4.3s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 11:43:04 +01:00
co-authored by Claude Opus 5.5
parent af070ffcf9
commit c6b82588b1
5 changed files with 83 additions and 9 deletions
+51
View File
@@ -0,0 +1,51 @@
package runner
import (
"os"
"path/filepath"
)
// Every solution includes <bits/stdc++.h>, and parsing it is most of the
// work of a build or a lint: on the production host (2 slow cores) a
// trivial program took 3.9s to build without a precompiled header and 0.6s
// with one. Dockerfile.runner builds both kinds into pchDir; without them
// (go test, a run outside the image) the same commands just take longer.
var pchDir = func() string {
if d := os.Getenv("PCH_DIR"); d != "" {
return d
}
return "/opt/pch"
}()
// gccPCH puts pchDir/bits/stdc++.h.gch ahead of the real header. g++ checks
// that it was built with matching options (-std=c++17, -O2, LOCAL) and
// silently reads the real header when it was not, so it can only be slow,
// never wrong.
func gccPCH() []string {
if _, err := os.Stat(filepath.Join(pchDir, "bits", "stdc++.h.gch")); err != nil {
return nil
}
return []string{"-I" + pchDir}
}
// tidyPCH preloads clang's precompiled bits/stdc++.h; main.cpp's own
// #include of it is then a no-op behind its include guard. Unlike g++,
// clang refuses a mismatched one, so the image builds it with the same
// -std and -DLOCAL that lintTidy passes.
func tidyPCH() []string {
pch := filepath.Join(pchDir, "stdc++.pch")
if _, err := os.Stat(pch); err != nil {
return nil
}
return []string{"-include-pch", pch}
}
// withGCCPCH is a g++ command line with gccPCH's -I after the program name.
func withGCCPCH(args []string) []string {
pch := gccPCH()
if len(pch) == 0 {
return args
}
out := append([]string{args[0]}, pch...)
return append(out, args[1:]...)
}