Repository navigation
perf(ci): skip pre-compile passes when build.ninja unchanged (#3129 A5) - #3206
Conversation
Every compile_meson() call ran two pre-compile passes:
- _invalidate_stale_pchs(build_dir): walks build_dir.rglob("*.pch")
and re-hashes each PCH depfile.
- normalize_meson_private_include_paths(build_dir): re-reads and
rewrites build.ninja and compile_commands.json to enforce zccache's
strict-paths=absolute mode.
Together: ~5-20 ms per build. Small but on every incremental
"nothing changed" build — exactly the iterations where it should be
free.
Both passes are guaranteed no-ops when build.ninja's mtime hasn't
changed since the last successful normalize, because that mtime
moves on every meson reconfigure (which is the only thing that
re-introduces the strict-path violations and changes the PCH input
graph).
This change records the build.ninja mtime in a sidecar marker
(.last-precompile-build-ninja-mtime) after each successful pass.
Subsequent compile_meson() calls compare the marker to the current
mtime and skip both passes when they match.
Cost in the cold path: an os.stat() + a marker read (sub-millisecond)
to decide whether to skip ~5-20 ms of work.
Failure modes:
- Marker missing → run the passes (first build, fresh build_dir).
- build.ninja missing → run the passes (defensive).
- Marker unreadable / bad format → run the passes.
- Normalize raises → don't update the marker; next compile retries.
Part of #3129 (Section A5).
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
📝 WalkthroughWalkthrough
Changesbuild.ninja mtime-based pre-compile skip
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
ci/meson/compile.py (1)
271-271: ⚡ Quick winMove this import to the module import block.
This changed local import should live with the other
ci.mesonimports unless it must remain lazy to avoid a cycle. As per coding guidelines, “All system imports must be at the top of the file in proper order: standard library, third-party, local”.♻️ Proposed import placement cleanup
from ci.meson.cache_utils import ( check_ninja_skip, save_ninja_skip_state, ) +from ci.meson.build_config import normalize_meson_private_include_paths from ci.meson.compiler import get_meson_executable- from ci.meson.build_config import normalize_meson_private_include_paths - normalize_succeeded = False🤖 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 `@ci/meson/compile.py` at line 271, The import statement for normalize_meson_private_include_paths from ci.meson.build_config is currently placed as a local import inside a function. Move this import statement to the module-level import block at the top of the file, grouping it with the other ci.meson imports, and remove it from its current location at line 271. This ensures compliance with the coding guidelines that require all local imports to be at the top of the file with proper ordering.Source: Coding guidelines
🤖 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.
Inline comments:
In `@ci/meson/compile.py`:
- Around line 175-181: The function `_precompile_passes_can_be_skipped`
currently gates both `_invalidate_stale_pchs()` and the strict-path normalize
call with the same condition (build.ninja mtime), but this is unsafe because
build.ninja mtime only proves the Meson graph didn't change, not that PCH inputs
are fresh. Headers can change while Ninja misses the PCH dependency. Remove the
gate that skips `_invalidate_stale_pchs()` so it always runs unconditionally,
keeping only the strict-path normalizer gated by the build.ninja marker.
Alternatively, if you want to optimize further, create a separate PCH-aware
marker that considers PCH depfiles and inputs in addition to the build.ninja
check.
---
Nitpick comments:
In `@ci/meson/compile.py`:
- Line 271: The import statement for normalize_meson_private_include_paths from
ci.meson.build_config is currently placed as a local import inside a function.
Move this import statement to the module-level import block at the top of the
file, grouping it with the other ci.meson imports, and remove it from its
current location at line 271. This ensures compliance with the coding guidelines
that require all local imports to be at the top of the file with proper
ordering.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: c76c0e40-5e56-4aa7-878a-81aa5ac0fe43
📒 Files selected for processing (1)
ci/meson/compile.py
| def _precompile_passes_can_be_skipped(build_dir: Path) -> bool: | ||
| """Return True iff build.ninja mtime matches the marker. | ||
|
|
||
| When True, both ``_invalidate_stale_pchs`` and the strict-path | ||
| normalize call are no-ops by construction (no meson reconfigure has | ||
| rewritten build.ninja since they last ran), and the ~5-20 ms cost | ||
| can be skipped. See #3129 A5. |
There was a problem hiding this comment.
Keep PCH invalidation out of the build.ninja marker gate.
build.ninja mtime only proves the Meson graph did not change; it does not prove PCH inputs are fresh. The comment at Lines 259-261 says headers can change while Ninja misses the PCH dependency, so skipping _invalidate_stale_pchs() here can reintroduce stale-PCH build failures. Gate only the strict-path normalizer, or add a PCH-aware marker keyed to the PCH depfiles/inputs.
🐛 Proposed fix: keep the PCH safety pass unconditional
- # Pre-compile passes (PCH staleness check + strict-path normalize) run
- # in two cases:
+ # Strict-path normalization runs in two cases:
# 1. First build of this build_dir (marker absent).
# 2. build.ninja mtime changed since the last successful pass (meson
# reconfigured, or build.ninja was edited externally).
- # When neither is true, both passes are guaranteed no-ops by construction
- # and the ~5-20 ms cost is skipped. See `#3129` A5.
+ # PCH staleness must still be checked because header changes can stale
+ # existing PCHs without rewriting build.ninja.
+ # See `#3129` A5.
+ _invalidate_stale_pchs(build_dir)
+
if not _precompile_passes_can_be_skipped(build_dir):
- # Check for stale PCH before invoking Ninja. Compilers on Windows
- # emit absolute backslash paths in depfiles which Ninja may fail to
- # track correctly, leaving the PCH stale even though headers changed.
- _invalidate_stale_pchs(build_dir)
-
# Re-normalize strict-path include flags before every compile (`#2378`).Also applies to: 251-263
🤖 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 `@ci/meson/compile.py` around lines 175 - 181, The function
`_precompile_passes_can_be_skipped` currently gates both
`_invalidate_stale_pchs()` and the strict-path normalize call with the same
condition (build.ninja mtime), but this is unsafe because build.ninja mtime only
proves the Meson graph didn't change, not that PCH inputs are fresh. Headers can
change while Ninja misses the PCH dependency. Remove the gate that skips
`_invalidate_stale_pchs()` so it always runs unconditionally, keeping only the
strict-path normalizer gated by the build.ninja marker. Alternatively, if you
want to optimize further, create a separate PCH-aware marker that considers PCH
depfiles and inputs in addition to the build.ninja check.
Summary
Every
compile_meson()call ran two pre-compile passes:_invalidate_stale_pchs(build_dir)— walksbuild_dir.rglob("*.pch")and re-hashes each PCH depfile.normalize_meson_private_include_paths(build_dir)— re-reads and rewritesbuild.ninjaandcompile_commands.jsonto enforce zccache's--strict-paths=absolute.Together: ~5–20 ms per build. Small but unconditional, on every incremental "nothing changed" build.
Why we can skip
Both passes are guaranteed no-ops when
build.ninja's mtime hasn't changed since the last successful normalize, because that mtime moves on every meson reconfigure — and reconfigure is the only thing that re-introduces strict-path violations or shifts the PCH input graph.Implementation
.last-precompile-build-ninja-mtimerecordsbuild.ninja's mtime after each successful pass.normalize_meson_private_include_pathssucceeds — so a transient normalize failure causes the next compile to retry.Failure modes
build.ninjamissingCost in the cold path: one
os.stat()+ a marker read (sub-millisecond) to decide whether to skip ~5–20 ms of work.1 file changed, 101 insertions(+), 27 deletions(-).
Validation
bash lint --cppcleanfrom ci.meson import compile)Part of #3129 (Section A5).
🤖 Generated with Claude Code
Summary by CodeRabbit