Skip to content

perf(ci): skip pre-compile passes when build.ninja unchanged (#3129 A5) - #3206

Merged
zackees merged 1 commit into
masterfrom
refactor/3129-a5-skip-pch-invalidate
Jun 17, 2026
Merged

zackees merged 1 commit into
masterfrom
refactor/3129-a5-skip-pch-invalidate

Conversation

@zackees

@zackees zackees commented Jun 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Every compile_meson() call ran two pre-compile passes:

  1. _invalidate_stale_pchs(build_dir) — walks build_dir.rglob("*.pch") and re-hashes each PCH depfile.
  2. normalize_meson_private_include_paths(build_dir) — re-reads and rewrites build.ninja and compile_commands.json to 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

  • Sidecar marker .last-precompile-build-ninja-mtime records build.ninja's mtime after each successful pass.
  • Subsequent compiles read the marker, compare to current mtime, and skip both passes when they match.
  • Marker is updated only when normalize_meson_private_include_paths succeeds — so a transient normalize failure causes the next compile to retry.

Failure modes

State Behaviour
Marker missing (first build) Run the passes
build.ninja missing Run the passes (defensive)
Marker unreadable / bad format Run the passes
Normalize raises Don't update marker; next compile retries

Cost 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 --cpp clean
  • ✅ Python module imports cleanly (from ci.meson import compile)

Part of #3129 (Section A5).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Improved build compilation efficiency by implementing smart detection to skip redundant pre-compilation optimization passes when the build configuration remains unchanged, resulting in faster build times for incremental builds.

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]>
@coderabbitai

coderabbitai Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

ci/meson/compile.py gains a .last-precompile-build-ninja-mtime sidecar marker mechanism. Three new helpers read, write, and evaluate this marker against the current build.ninja st_mtime. compile_meson() now gates _invalidate_stale_pchs and normalize_meson_private_include_paths behind the skip predicate and writes the marker only after successful normalization.

Changes

build.ninja mtime-based pre-compile skip

Layer / File(s) Summary
Sidecar marker helpers
ci/meson/compile.py
Adds the marker filename constant, a float-parsing reader, a best-effort writer, and a skip predicate that compares saved vs. current build.ninja mtime within a small tolerance.
compile_meson() conditional wiring
ci/meson/compile.py
Wraps PCH invalidation and include-path normalization in the skip-predicate check; persists the marker only on normalization success; normalization failures remain non-fatal and suppress marker writes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 A ninja file's mtime, I sniff and I store,
If nothing has changed, I skip like before!
PCH invalidation? No need, friend, not today.
The marker says "fresh" — let's compile away!
With tolerance small and a write done just right,
This rabbit hops faster through each build tonight. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main optimization: skipping pre-compile passes when build.ninja is unchanged, which matches the core objective and change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/3129-a5-skip-pch-invalidate

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
ci/meson/compile.py (1)

271-271: ⚡ Quick win

Move this import to the module import block.

This changed local import should live with the other ci.meson imports 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

📥 Commits

Reviewing files that changed from the base of the PR and between fe5c638 and 0a74880.

📒 Files selected for processing (1)
  • ci/meson/compile.py

Comment thread ci/meson/compile.py
Comment on lines +175 to +181
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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

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.

@zackees
zackees merged commit 54c0b9d into master Jun 17, 2026
12 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 17, 2026
@zackees
zackees deleted the refactor/3129-a5-skip-pch-invalidate branch August 21, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant