Skip to content

Commit 5b9b531

Browse files
committed
Use forEachSymbolOccurrence to find parent container symbol
This improves performance when searching for `only` in SourceKit-LSP by ~30%.
1 parent 970c5a5 commit 5b9b531

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Sources/SemanticIndex/CheckedIndex.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,18 @@ package final class CheckedIndex {
268268
containerSymbol = extendedSymbol
269269
}
270270
let result: [String]
271-
if let containerDefinition = primaryDefinitionOrDeclarationOccurrence(ofUSR: containerSymbol.usr) {
271+
272+
// Use `forEachSymbolOccurrence` instead of `primaryDefinitionOrDeclarationOccurrence` to get a symbol occurrence
273+
// for the container because it can be significantly faster: Eg. when searching for a C++ namespace (such as `llvm`),
274+
// it may be declared in many files. Finding the canonical definition means that we would need to scan through all
275+
// of these files. But we expect all all of these declarations to have the same parent container names and we don't
276+
// care about locations here.
277+
var containerDefinition: SymbolOccurrence?
278+
forEachSymbolOccurrence(byUSR: containerSymbol.usr, roles: [.definition, .declaration]) { occurrence in
279+
containerDefinition = occurrence
280+
return false // stop iteration
281+
}
282+
if let containerDefinition {
272283
result = self.containerNames(of: containerDefinition) + [containerSymbol.name]
273284
} else {
274285
result = [containerSymbol.name]

0 commit comments

Comments
 (0)