Skip to content
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

Symbol search: improve performance for anchored queries #682

Closed
wants to merge 5 commits into from

Conversation

camdencheek
Copy link
Member

@camdencheek camdencheek commented Nov 6, 2023

Search-based code intel makes heavy use of exact, anchored searches for symbol names like ^NewFoo$. It uses regex patterns to ensure that the symbol name exactly matches the symbol being hovered over. Zoekt does not efficiently handle anchors, falling back to a brute-force regex search that executes the regex against every symbol in the index.

This is a dirty PR that fixes the issue, but in a probably non-ideal way. I'm looking for early feedback on whether this is the correct approach. If so, I'll do some more extensive and re-request review before merge.

Explanations inline.

@camdencheek camdencheek marked this pull request as draft November 6, 2023 19:50
matchtree.go Outdated
Comment on lines 994 to 1010
anchored := false
expr := s.Expr
switch e := s.Expr.(type) {
case *query.Regexp:
pattern := e.Regexp.String()
if strings.HasPrefix(pattern, "^") && strings.HasSuffix(pattern, "$") {
pattern = pattern[1 : len(pattern)-1]
parsedPattern, err := syntax.Parse(pattern, e.Regexp.Flags)
if err != nil {
return nil, err
}
eCopy := *e
eCopy.Regexp = parsedPattern
expr = &eCopy
anchored = true
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

When constructing the match tree for symbols, try to strip anchors off the input pattern. We keep track of whether the pattern was anchored, which we then use to post-filter the results so we only return ones that exactly match the symbol boundaries.

matchtree.go Outdated
Comment on lines 301 to 304
if t.anchored && !(start == sections[secIdx].Start && end == sections[secIdx].End) {
t.current = t.current[1:]
continue
}
Copy link
Member Author

Choose a reason for hiding this comment

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

If the pattern was anchored, check that the match is not just contained in a symbol's range, but matches it exactly.

Copy link
Member

Choose a reason for hiding this comment

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

based on this code should the variable name be more something along the lines of exact? This is on substr not a regexp, I think anchored can be misinterpreted a bit. There are others parts in the zoekt API where we use the word exact to mean the string must match exactly.

}
}

subMT, err := d.newMatchTree(expr, optCopy)
Copy link
Member Author

Choose a reason for hiding this comment

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

I looked at putting this logic into newMatchTree, but the problem is that anchors aren't meaningful until we get to the symbol layer because we want the anchors to match the start and end of the symbol name which are not available lower in the tree.

Copy link
Member

@keegancsmith keegancsmith left a comment

Choose a reason for hiding this comment

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

Overall the idea looks good to me and nice find. I think how you decide this in newMatchTree needs to be more robust/clearer before merging.

Looking at the symbolSubstrMatchTree with fresh eyes, I can see it has lots of opportunity to be better. From what I can tell we find all substring matches, then filter those based on document sections. It feels like we could treat document sections like we treat the trigram iterator. IE use it to help filter candidate matches inside the substring match tree. That can be a follow-up.

Out of interest here is another perf PR we never finished up to help speed up regex search #216

matchtree.go Outdated
Comment on lines 301 to 304
if t.anchored && !(start == sections[secIdx].Start && end == sections[secIdx].End) {
t.current = t.current[1:]
continue
}
Copy link
Member

Choose a reason for hiding this comment

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

based on this code should the variable name be more something along the lines of exact? This is on substr not a regexp, I think anchored can be misinterpreted a bit. There are others parts in the zoekt API where we use the word exact to mean the string must match exactly.

matchtree.go Outdated Show resolved Hide resolved
matchtree.go Outdated Show resolved Hide resolved
matchtree.go Show resolved Hide resolved
if err != nil {
return nil, err
}

if substr, ok := subMT.(*substrMatchTree); ok {
return &symbolSubstrMatchTree{
substrMatchTree: substr,
exact: wasAnchored,
Copy link
Member

Choose a reason for hiding this comment

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

one last bug here, I think the regexp we pass to symbolRegexpMatchTree (if we get to that path) will now exclude anchors.

@camdencheek
Copy link
Member Author

Going to close this for now since the perf benefit I was aiming for came mostly from fixing the prepare stuff. Will maybe bring this back if I run out of more levers to pull, but perf seems much better now.

@keegancsmith keegancsmith deleted the cc/exact-symbols branch November 14, 2023 08:20
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.

2 participants