perf: cache filtered include flags per [(lib_mode, kept_libs)]#14519
Merged
robinbb merged 1 commit intoJun 13, 2026
Merged
Conversation
This was referenced May 13, 2026
c50afa4 to
38a7871
Compare
d0903f5 to
7205127
Compare
38a7871 to
8634a30
Compare
7205127 to
e28b521
Compare
b8bba4b to
c73a510
Compare
e28b521 to
9b63c9b
Compare
c73a510 to
d12a8e5
Compare
9b63c9b to
ec28e9e
Compare
d12a8e5 to
9dfc6ed
Compare
ec28e9e to
7d86a46
Compare
9dfc6ed to
1c8148e
Compare
7d86a46 to
aa40e40
Compare
1c8148e to
3492d57
Compare
aa40e40 to
7fb8178
Compare
3492d57 to
481c534
Compare
7fb8178 to
334e2a0
Compare
481c534 to
c0f5c20
Compare
334e2a0 to
b606c5f
Compare
c0f5c20 to
8fa201a
Compare
b606c5f to
90239f6
Compare
8fa201a to
7595ce1
Compare
90239f6 to
750995d
Compare
7595ce1 to
8a4c3cb
Compare
750995d to
25b83ba
Compare
8a4c3cb to
3207942
Compare
25b83ba to
fcf767c
Compare
3207942 to
aec12ff
Compare
fcf767c to
c93dbda
Compare
aec12ff to
b9f223d
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces a per-Compilation_context cache for per-module filtered include flags, so compile rules that compute the same (lib_mode, kept_libs) share a single Action_builder.t (with Action_builder.memoize deduping evaluation). This is part of the ongoing work to narrow per-module dependencies across libraries (#4572 / #14492 stack).
Changes:
- Cache
Compilation_context.filtered_include_flagsresults keyed by(lib_mode, kept_libs)to reduce repeated builder construction/evaluation. - Add
Lib_mode.hashto support the cache key hashing. - Wire
Module_compilation.lib_deps_for_moduleto return filtered include flags and update blackbox tests to assert the filtered-I/rebuild behavior.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/blackbox-tests/test-cases/per-module-lib-deps/per-module-include-flags.t | Updates expectations to assert only referenced libraries’ -I objdirs appear. |
| test/blackbox-tests/test-cases/per-module-lib-deps/add-unreferenced-sibling-lib.t | Updates expectations to assert adding an unreferenced sibling library does not trigger rebuilds. |
| src/dune_rules/module_compilation.ml | Returns per-module filtered include flags (via Compilation_context.filtered_include_flags) along with deps. |
| src/dune_rules/compilation_context.mli | Exposes filtered_include_flags in the public interface with documentation. |
| src/dune_rules/compilation_context.ml | Implements and caches filtered include flags builders per (lib_mode, kept_libs) within a cctx. |
| src/dune_lang/lib_mode.mli | Adds Lib_mode.hash to the module interface. |
| src/dune_lang/lib_mode.ml | Implements Lib_mode.hash. |
cd4c791 to
b2db8e6
Compare
robinbb
added a commit
that referenced
this pull request
Jun 10, 2026
Layer 6 of 9 of #14492. `Compilation_context.filtered_include_flags`: new function returning `-I` / `-H` flags restricted to `kept_libs`. The cctx's `requires_compile` and `requires_hidden` are each filtered by `Lib.Set.mem kept_libs`. No caching yet — layer 7 adds it. `Module_compilation.lib_deps_for_module`: the tight branch was already producing `kept_libs` (unused at layers 4–5); now wired into `filtered_include_flags`. Returns the filtered include args instead of the cctx-wide `Includes`. Effect: a consumer's compile command sees `-I` / `-H` only for libraries its ocamldep reference set actually reaches. Adding an unreferenced sibling to the cctx's `(libraries ...)` no longer changes the compile command, so the rule does not re-execute. Promotes `per-module-include-flags.t` and `add-unreferenced-sibling-lib.t` — both deferred from layer 4 because their assertions depend on this filter. Stack: rebases on #14517. Next: #14519. Part of #14492. Related to #4572. Signed-off-by: Robin Bate Boerop <me@robinbb.com>
Base automatically changed from
robinbb-14492-l6-filtered-include-flags
to
robinbb-14492-test-stack-integration
June 10, 2026 17:29
[Compilation_context.Filtered_includes] caches the [Action_builder.t] returned by [filtered_include_flags] keyed on [(lib_mode, kept_libs)]. Two modules in the same cctx that reach the same set of kept libs share one builder; [Action_builder.memoize] dedupes its evaluation. Cache key omits the cctx's [requires_compile] / [requires_hidden] — they're immutable on the cctx from [create]. The [for_module_generated_at_link_time] exception, where derived cctxs could in principle alter the closure, takes [can_filter = false] in [lib_deps_for_module] and so never reaches this function. [Filtered_includes.Key]: [lib_mode] + [kept_libs : Lib.t list] (the caller passes a sorted list via [Lib.Set.to_list], canonicalising for the cache). [equal] and [hash] derived from the same; [Repr]-derived [to_dyn] for diagnostics. [Lib_mode.hash]: new — used by [Filtered_includes.Key.hash]. Three constants for the three variants ([Ocaml Byte], [Ocaml Native], [Melange]). Signed-off-by: Robin Bate Boerop <me@robinbb.com>
b2db8e6 to
0d280ae
Compare
Alizter
approved these changes
Jun 12, 2026
0ff8b17
into
robinbb-14492-test-stack-integration
30 checks passed
robinbb
added a commit
that referenced
this pull request
Jun 16, 2026
) Layer 8 of 9 of #14492. Pure performance. `Compilation_context.Raw_refs` caches the `Action_builder.t` computed for each ocamldep raw-deps read inside a cctx. Two consumer modules that share trans deps get the same builder. `Raw_refs.Key` distinguishes the two read patterns the per-module filter uses: `Consumer` (the cctx-driving module's own deps, keyed by `ml_kind`) and `Transitive` (a dep module's deps, keyed by `cm_kind` because the impl/intf gating in `need_impl_deps_of` varies by cm_kind on the `Cmx` / opaque path). `Module_compilation.lib_deps_for_module`: wraps the inline `read_dep_m_raw` body with `cached_raw_refs`. Stack: rebases on #14519. Next: #14521. Part of #14492. Related to #4572. Signed-off-by: Robin Bate Boerop <me@robinbb.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Layer 7 of 9 of #14492. Pure performance.
Compilation_context.Filtered_includescaches theAction_builder.treturned byfiltered_include_flagskeyed on(lib_mode, kept_libs). Two modules in the same cctx that reach the same set of kept libs share one builder;Action_builder.memoizededupes its evaluation.Lib_mode.hash: new — used by the cache key.Stack: rebases on #14518. Next: #14520.
Part of #14492. Related to #4572.