Skip to content

Speed up source ordering and annotation lookups with header modules. - #3267

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_1035115305
Dec 4, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
test_1035115305

Conversation

@copybara-service

Copy link
Copy Markdown

Speed up source ordering and annotation lookups with header modules.

With header modules, rs_bindings_from_cc is much slower on small targets: a
namespace like absl or std has thousands of redeclarations (one for each
copy of each header that reopens it), and two hot paths do work for each of
them that doesn't depend on the declaration:

  • IsBeforeInSourceOrder builds a position key for both locations on every
    comparison by walking the include stack of the location within its module.
    The include stack only depends on the file, so it is now memoized per
    FileID (SourceOrderCache::loaded_include_stacks, which replaces
    RepresentativeCopyCache and also holds the representative copies).
  • ImportDecl looks up three annotations (crubit_must_bind,
    crubit_pub_crate, crubit_do_not_bind) for each redeclaration of a
    namespace, and each lookup visits all redeclarations: quadratic time. The
    lookups only read AnnotateAttrs, so they are now skipped for namespaces
    where no redeclaration has one (NamespaceHasAnnotations, memoized per
    canonical declaration and recomputed if the redeclaration chain grows).

Neither change affects the output.

With header modules, `rs_bindings_from_cc` is much slower on small targets: a
namespace like `absl` or `std` has thousands of redeclarations (one for each
copy of each header that reopens it), and two hot paths do work for each of
them that doesn't depend on the declaration:

* `IsBeforeInSourceOrder` builds a position key for both locations on every
  comparison by walking the include stack of the location within its module.
  The include stack only depends on the file, so it is now memoized per
  `FileID` (`SourceOrderCache::loaded_include_stacks`, which replaces
  `RepresentativeCopyCache` and also holds the representative copies).
* `ImportDecl` looks up three annotations (`crubit_must_bind`,
  `crubit_pub_crate`, `crubit_do_not_bind`) for each redeclaration of a
  namespace, and each lookup visits all redeclarations: quadratic time. The
  lookups only read `AnnotateAttr`s, so they are now skipped for namespaces
  where no redeclaration has one (`NamespaceHasAnnotations`, memoized per
  canonical declaration and recomputed if the redeclaration chain grows).

Neither change affects the output.

PiperOrigin-RevId: 1036371973
@copybara-service
copybara-service Bot merged commit abcd1e6 into main Dec 4, 2026
2 checks passed
@copybara-service
copybara-service Bot deleted the test_1035115305 branch December 4, 2026 17:00

This branch was previously deployed

1 inactive deployment
github-pages — abcd1e65 Deployed Dec 4, 2026 by copybara-service[bot] via deploy #322
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.

1 participant