Skip to content

[Index] Only hash Decl once when generating the hash value for a record #10846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions clang/lib/Index/IndexRecordHasher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ struct IndexRecordHasher {
llvm::HashBuilder<llvm::TruncatedBLAKE3<8>, llvm::endianness::little>
HashBuilder;

/// Maps declarations that have already been hashed in this
/// `IndexRecordHasher` to a unique ID that identifies this declaration.
///
/// The ID assigned to a declaration is consistent across multiple runs of the
/// `IndexRecordHasher` on the same AST structure, even across multiple
/// process runs.
///
/// See `hashDecl` for its use.
llvm::DenseMap<const Decl *, size_t> HashedDecls;

explicit IndexRecordHasher(ASTContext &context) : Ctx(context) {}

void hashDecl(const Decl *D);
Expand Down Expand Up @@ -184,6 +194,19 @@ void IndexRecordHasher::hashMacro(const IdentifierInfo *name,
void IndexRecordHasher::hashDecl(const Decl *D) {
assert(D->isCanonicalDecl());

auto emplaceResult = HashedDecls.try_emplace(D, HashedDecls.size());
bool inserted = emplaceResult.second;
if (!inserted) {
// If we have already serialized this declaration, just add the
// declaration's hash to the hash builder. This is significantly
// cheaper than visiting the declaration again.
HashBuilder.add(emplaceResult.first->second);
return;
}

// If we haven't serialized the declaration yet,`try_emplace` will insert the
// new unique ID into `HashedDecls`. We just need to hash the declaration
// once.
if (auto *NS = dyn_cast<NamespaceDecl>(D)) {
if (NS->isAnonymousNamespace()) {
HashBuilder.add("@aN");
Expand Down