Skip to content

perf: cache filtered include flags per [(lib_mode, kept_libs)]#14519

Merged
robinbb merged 1 commit into
robinbb-14492-test-stack-integrationfrom
robinbb-14492-l7-filtered-includes-cache
Jun 13, 2026
Merged

perf: cache filtered include flags per [(lib_mode, kept_libs)]#14519
robinbb merged 1 commit into
robinbb-14492-test-stack-integrationfrom
robinbb-14492-l7-filtered-includes-cache

Conversation

@robinbb

@robinbb robinbb commented May 13, 2026

Copy link
Copy Markdown
Collaborator

Layer 7 of 9 of #14492. Pure performance.

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.

Lib_mode.hash: new — used by the cache key.

Stack: rebases on #14518. Next: #14520.

Part of #14492. Related to #4572.

@robinbb robinbb self-assigned this May 13, 2026
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from c50afa4 to 38a7871 Compare May 14, 2026 00:36
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch 2 times, most recently from d0903f5 to 7205127 Compare May 14, 2026 23:57
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from 38a7871 to 8634a30 Compare May 15, 2026 02:57
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from 7205127 to e28b521 Compare May 15, 2026 02:57
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch 3 times, most recently from b8bba4b to c73a510 Compare May 21, 2026 01:34
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from e28b521 to 9b63c9b Compare May 21, 2026 01:34
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from c73a510 to d12a8e5 Compare May 22, 2026 02:05
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from 9b63c9b to ec28e9e Compare May 22, 2026 03:29
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from d12a8e5 to 9dfc6ed Compare May 23, 2026 05:18
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from ec28e9e to 7d86a46 Compare May 23, 2026 05:18
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from 9dfc6ed to 1c8148e Compare May 23, 2026 21:24
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from 7d86a46 to aa40e40 Compare May 23, 2026 21:24
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from 1c8148e to 3492d57 Compare May 24, 2026 18:57
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from aa40e40 to 7fb8178 Compare May 24, 2026 21:04
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from 3492d57 to 481c534 Compare May 29, 2026 00:11
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from 7fb8178 to 334e2a0 Compare May 29, 2026 00:11
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from 481c534 to c0f5c20 Compare May 29, 2026 06:43
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from 334e2a0 to b606c5f Compare May 29, 2026 06:43
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from c0f5c20 to 8fa201a Compare June 8, 2026 01:05
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from b606c5f to 90239f6 Compare June 8, 2026 01:05
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from 8fa201a to 7595ce1 Compare June 8, 2026 01:39
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from 90239f6 to 750995d Compare June 8, 2026 01:39
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from 7595ce1 to 8a4c3cb Compare June 8, 2026 18:40
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from 750995d to 25b83ba Compare June 8, 2026 18:40
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from 8a4c3cb to 3207942 Compare June 8, 2026 23:27
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from 25b83ba to fcf767c Compare June 8, 2026 23:27
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from 3207942 to aec12ff Compare June 9, 2026 14:49
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from fcf767c to c93dbda Compare June 9, 2026 14:50
@robinbb robinbb force-pushed the robinbb-14492-l6-filtered-include-flags branch from aec12ff to b9f223d Compare June 10, 2026 16:33
@robinbb robinbb requested a review from Copilot June 10, 2026 16:38

Copilot AI 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.

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_flags results keyed by (lib_mode, kept_libs) to reduce repeated builder construction/evaluation.
  • Add Lib_mode.hash to support the cache key hashing.
  • Wire Module_compilation.lib_deps_for_module to 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.

Comment thread src/dune_rules/compilation_context.ml
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch 2 times, most recently from cd4c791 to b2db8e6 Compare June 10, 2026 16:50
@robinbb robinbb requested a review from Copilot June 10, 2026 16:51

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@robinbb robinbb marked this pull request as ready for review June 10, 2026 16:59
@robinbb robinbb requested a review from Alizter June 10, 2026 16:59
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>
@robinbb robinbb force-pushed the robinbb-14492-l7-filtered-includes-cache branch from b2db8e6 to 0d280ae Compare June 10, 2026 20:03
@robinbb robinbb merged commit 0ff8b17 into robinbb-14492-test-stack-integration Jun 13, 2026
30 checks passed
@robinbb robinbb deleted the robinbb-14492-l7-filtered-includes-cache branch June 13, 2026 21:21
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants