Skip to content

Retain module map names as Labels#633

Closed
fmeum wants to merge 2 commits into
bazelbuild:mainfrom
fmeum:header-modules
Closed

Retain module map names as Labels#633
fmeum wants to merge 2 commits into
bazelbuild:mainfrom
fmeum:header-modules

Conversation

@fmeum

@fmeum fmeum commented Mar 12, 2026

Copy link
Copy Markdown
Collaborator

With the exception of special module map names, all of them are effectively labels. This commit ensures that they are retained as Labels, not label string and thus:

  • memory usage is reduced for module maps from external repos, whose module map name previously resulted in a newly retained string;
  • module map names now use properly formatted external repo labels as names, which makes it more obvious what they are;
  • the roundtrip in header module compilation flows is avoided. This actually fixes a bug where the missing @@ prefix for label strings resulted in incorrect or even failing Label calls due to main repo labels being resolved relative to the rules_cc repo and external repo labels failing the validation of the Label constructor.

@armandomontanez armandomontanez added type: bug Something that should be working isn't working P2 We'll consider working on this in future. (Assignee optional) category: core rules labels Mar 24, 2026
@hvadehra
hvadehra removed their request for review April 21, 2026 04:42

if feature_configuration.is_enabled("module_maps") and cpp_module_map:
result[_VARS.MODULE_NAME] = cpp_module_map.name
result[_VARS.MODULE_NAME] = str(cpp_module_map.identifier)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think you need to do the _stringify_module_map conversion here too.

A lot of internal Bazel integration tests failed with layering check issues when I tried this change and adding that conversion seemed to fix it.

That being said, I can't verify whether that will end up working correctly outside of Google.

fmeum added 2 commits May 2, 2026 21:37
With the exception of special module map names, all of them are effectively labels. This commit ensures that they are retained as `Label`s, not label string and thus:
* memory usage is reduced for module maps from external repos, whose module map name previously resulted in a newly retained string;
* module map names now use properly formatted external repo labels as names, which makes it more obvious what they are;
* the roundtrip in header module compilation flows is avoided. This actually fixes a bug where the missing `@@` prefix for label strings resulted in incorrect or even failing `Label` calls due to main repo labels being resolved relative to the rules_cc repo and external repo labels failing the validation of the `Label` constructor.
@fmeum
fmeum force-pushed the header-modules branch from 6301d39 to 6174f30 Compare May 2, 2026 19:37
copybara-service Bot pushed a commit to bazelbuild/bazel that referenced this pull request May 13, 2026
Copybara Import from bazelbuild/rules_cc#633

BEGIN_PUBLIC
Retain module map names as `Label`s (#633)

With the exception of special module map names, all of them are effectively labels. This commit ensures that they are retained as `Label`s, not label string and thus:
* memory usage is reduced for module maps from external repos, whose module map name previously resulted in a newly retained string;
* module map names now use properly formatted external repo labels as names, which makes it more obvious what they are;
* the roundtrip in header module compilation flows is avoided. This actually fixes a bug where the missing `@@` prefix for label strings resulted in incorrect or even failing `Label` calls due to main repo labels being resolved relative to the rules_cc repo and external repo labels failing the validation of the `Label` constructor.

Closes #633
END_PUBLIC

PiperOrigin-RevId: 914893127
Change-Id: I516d7fd41462b0044c504df53e624e96a44850e3
copybara-service Bot pushed a commit that referenced this pull request May 13, 2026
*** Reason for rollback ***

Increases memory usage b/512851570

*** Original change description ***

Retain module map names as `Label`s

Copybara Import from #633

BEGIN_PUBLIC
Retain module map names as `Label`s (#633)

With the exception of special module map names, all of them are effectively labels. This commit ensures that they are retained as `Label`s, not label string and thus:
* memory usage is reduced for module maps from external repos, whose module map name previously resulted in a newly retained string;
* module map names now use properly...

***

PiperOrigin-RevId: 914964373
Change-Id: I417bd251d894b2ec7d9837790301abe96dd88fd3
copybara-service Bot pushed a commit to bazelbuild/bazel that referenced this pull request May 13, 2026
*** Reason for rollback ***

Increases memory usage b/512851570

*** Original change description ***

Retain module map names as `Label`s

Copybara Import from bazelbuild/rules_cc#633

BEGIN_PUBLIC
Retain module map names as `Label`s (#633)

With the exception of special module map names, all of them are effectively labels. This commit ensures that they are retained as `Label`s, not label string and thus:
* memory usage is reduced for module maps from external repos, whose module map name previously resulted in a newly retained string;
* module map names now use properly...

***

PiperOrigin-RevId: 914964373
Change-Id: I1ffb91216c6ccb398feb378876cd5e659a3afcb6
@lilygorsheneva

Copy link
Copy Markdown
Collaborator

This causes some of our builds to see memory usage increase by 0.5-1.0%; we're reverting this.

If the memory usage improvements this is expected to provide for external uses are significant we can consider having different behavior internally/externally.

The @@ bug is probably worth fixing separately but I don't know what the priority on that is.

@fmeum

fmeum commented May 14, 2026

Copy link
Copy Markdown
Collaborator Author

@lilygorsheneva Are you using separate module maps quite heavily internally? That's the only potential source of additional memory usage I see and I could optimize that case.

@trybka

trybka commented May 14, 2026

Copy link
Copy Markdown
Collaborator

I believe we are, for layering check, as well as for low level libraries where we enable clang header modules.

fmeum added a commit to fmeum/rules_cc that referenced this pull request Jul 17, 2026
Module maps generated for a target are named after the target's label.
Since the generated module map file is declared by that very target, the
name can be derived on demand from the file's owner label, which the
File object already retains. This fixes two issues at once:

* The name string previously stored in the module map struct (one per
  target) no longer needs to be retained. This achieves the memory
  savings of bazelbuild#633, which was rolled back because retaining Labels
  instead of strings increased memory usage overall - deriving the name
  on demand retains nothing.
* Module map names are canonical label strings without the leading '@@'
  (e.g. 'rules_cc+//pkg:lib'), which Label() fails to parse for targets
  in external repositories ('invalid package name'), breaking header
  module compilation for all such targets. The new
  get_module_map_label() helper either returns file.owner directly or
  restores the '@@' prefix before parsing.

Special module maps (the crosstool module map and ObjC internal module
maps) keep their explicitly provided names.
fmeum added a commit to fmeum/rules_cc that referenced this pull request Jul 17, 2026
Module maps generated for a target are named after the target's label.
Since the generated module map file is declared by that very target, the
name can be derived on demand from the file's owner label, which the
File object already retains. This fixes two issues at once:

* The name string previously stored in the module map struct (one per
  target) no longer needs to be retained. This achieves the memory
  savings of bazelbuild#633, which was rolled back because retaining Labels
  instead of strings increased memory usage overall - deriving the name
  on demand retains nothing.
* Module map names were generated as
  'workspace_name + "//" + package + ":" + name', which Label() fails
  to parse for targets in external repositories (e.g.
  'rules_cc+//pkg:lib' results in 'invalid package name'), breaking
  header module compilation for all such targets.

Module names are now the unambiguous canonical label string, except that
main-repository labels drop the leading '@@' ('//pkg:lib'), preserving
the traditional module name format for the main repository. Names of
external-repository targets keep the '@@' and are thus valid label
strings; this changes their module names, but header module compilation
never worked for them due to the Label() parsing failure. The new
get_module_map_label() helper returns file.owner directly for derived
names and restores the '@@' prefix for explicitly named main-repository
module maps (such as separate module maps).

Special module maps (the crosstool module map and ObjC internal module
maps) keep their explicitly provided names.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: core rules P2 We'll consider working on this in future. (Assignee optional) type: bug Something that should be working isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants