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
+18 -1
View File
@@ -13,6 +13,17 @@
# ruff Python lint (optional)
# tini PID 1; see below
# A missing linter only means less lint; see internal/runner.
# Alpine 3.24 for clang-tidy 22, which walks far less of the
# system headers than 3.22's clang-tidy 20: about 4x faster on
# the same file, which on the production host was the
# difference between a 30s lint and a usable one.
#
# It also precompiles <bits/stdc++.h>, which every solution
# includes, into /opt/pch (internal/runner/pch.go): a .gch for
# g++ and a .pch for clang-tidy, built with the flags the runner
# passes (-std=c++17 -O2 -DLOCAL), since both compilers reject
# a header built differently. A build drops from about 4s to
# 0.6s on the production host.
#
# The build context is site/, like Dockerfile: no problems are in the image.
# The site sends the source, its tests and any headers (debugging.h,
@@ -45,9 +56,15 @@ RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/usacorunner ./cmd/usacorunner
FROM alpine:3.22
FROM alpine:3.24
RUN apk add --no-cache g++ musl-dev python3 clang-extra-tools ruff tini \
&& adduser -D -H -u 1000 app
RUN header=$(echo '#include <bits/stdc++.h>' | g++ -std=c++17 -x c++ -E -H - 2>&1 >/dev/null \
| grep -m1 'bits/stdc++\.h$' | sed 's/^\.* //') \
&& test -f "$header" \
&& mkdir -p /opt/pch/bits \
&& g++ -std=c++17 -O2 -DLOCAL -x c++-header "$header" -o /opt/pch/bits/stdc++.h.gch \
&& clang++ -std=c++17 -DLOCAL -x c++-header "$header" -o /opt/pch/stdc++.pch
COPY --from=build /out/usacorunner /app/usacorunner
USER app
ENV TMPDIR=/tmp
+1 -1
View File
@@ -178,7 +178,7 @@ The "Review my explanation" panel (admin only) takes a recording of the admin ex
- **The runner is its own service.** The site holds the Claude, Gitea and S3 secrets and shares `app-network` with the database, so it never runs code. `cmd/usacorunner` does, in a container with no environment, on an `internal` network that only the site joins, read-only apart from an exec-able `/tmp` tmpfs, with memory, CPU and pids limits, all capabilities dropped and tini as PID 1 to reap orphans. The site posts to it (`runner.Client`, `RUNNER_URL`); the browser never sees it.
- **Runs copy the CLI.** The same g++ flags as `lib/common.sh` (`-DLOCAL`, with the repo root's `debugging.h` and `leetcode.h`, and a LeetCode problem's `driver.h`, sent along), the program runs in a directory holding `<io>.in` with that file also on stdin, `<io>.out` is its output if it wrote one, and outputs compare as `usaco test` does, ignoring trailing whitespace and trailing blank lines. Unlike the CLI there are limits: CPU time (2 s by default; TLE), 512 MB of address space, 64 MB files, a wall clock, and 60 s for a whole request, which keeps it synchronous under nginx's 120 s.
- **Lint.** C++ is g++'s warnings plus clang-tidy with the repo's `.clang-tidy` (bugprone, performance and a few readability checks, tuned so `template.cpp` is clean); the runner embeds a copy, and a test keeps the two equal. Python is `compile()` for syntax errors plus ruff (`E4,E7,E9,F,B`). A missing tool is skipped.
- **Lint.** C++ is g++'s warnings plus clang-tidy with the repo's `.clang-tidy` (bugprone, performance and a few readability checks, tuned so `template.cpp` is clean); the runner embeds a copy, and a test keeps the two equal. Python is `compile()` for syntax errors plus ruff (`E4,E7,E9,F,B`). A missing tool is skipped. The runner image precompiles `bits/stdc++.h` for g++ and clang-tidy (`internal/runner/pch.go`), which on the production host takes a build from about 4 s to 0.6 s and a clang-tidy pass from over 30 s to about 4 s (with Alpine 3.24's clang-tidy 22).
## Things that are easy to get wrong
+8 -6
View File
@@ -128,12 +128,14 @@ func runTool(ctx context.Context, ws *workspace, name string, args []string, std
// not GCC's JSON: GCC 16 dropped -fdiagnostics-format=json for SARIF,
// while the image's GCC 14 has both, and the one-line-per-diagnostic text
// is the same on every version. Byte columns, so a tab counts as one
// column as it does in the editor.
// column as it does in the editor. -O2 as in the build: the precompiled
// header only matches the same optimization level, and some warnings
// (maybe-uninitialized) only come with it.
func lintGCC(ctx context.Context, ws *workspace) []Diagnostic {
out := runTool(ctx, ws, "g++", []string{
out := runTool(ctx, ws, "g++", withGCCPCH([]string{
"g++", "-fsyntax-only", "-fdiagnostics-plain-output", "-fdiagnostics-column-unit=byte",
"-std=c++17", "-Wall", "-Wextra", "-DLOCAL", "-I.", "main.cpp",
}, "")
"-O2", "-std=c++17", "-Wall", "-Wextra", "-DLOCAL", "-I.", "main.cpp",
}), "")
return parseGCC(out)
}
@@ -185,10 +187,10 @@ func parseGCC(out string) []Diagnostic {
// lintTidy runs clang-tidy with the embedded checks. clang-tidy only
// reports on the main file unless told otherwise, so headers are quiet.
func lintTidy(ctx context.Context, ws *workspace) []Diagnostic {
out := runTool(ctx, ws, "clang-tidy", []string{
out := runTool(ctx, ws, "clang-tidy", append([]string{
"clang-tidy", "main.cpp", "--quiet", "--config=" + tidyConfig,
"--", "-std=c++17", "-DLOCAL", "-I.",
}, "")
}, tidyPCH()...), "")
main := filepath.Join(ws.work, "main.cpp")
paths := map[string]bool{"main.cpp": true, main: true}
if real, err := filepath.EvalSymlinks(main); err == nil {
+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:]...)
}
+5 -1
View File
@@ -114,9 +114,13 @@ func ioName(io string) string {
func compile(ctx context.Context, ws *workspace, lang language) Compile {
out := ws.capture("compile.txt")
args := lang.compile
if args[0] == "g++" {
args = withGCCPCH(args)
}
o := proc{
dir: ws.work,
args: lang.compile,
args: args,
stdout: out,
stderr: out,
limits: limits{cpuSec: int(compileTimeout / time.Second), memoryKB: compileMemoryKB, fileBlocks: fileBlocks},