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
Closed
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
58 changes: 53 additions & 5 deletions matchtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func (t *symbolRegexpMatchTree) matches(cp *contentProvider, cost int, known map
type symbolSubstrMatchTree struct {
*substrMatchTree

exact bool
patternSize uint32
fileEndRunes []uint32
fileEndSymbol []uint32
Expand Down Expand Up @@ -292,12 +293,19 @@ func (t *symbolSubstrMatchTree) prepare(doc uint32) {
continue
}

if end <= sections[secIdx].End {
t.current[0].symbol = true
t.current[0].symbolIdx = uint32(secIdx)
trimmed = append(trimmed, t.current[0])
if end > sections[secIdx].End {
t.current = t.current[1:]
continue
}

if t.exact && !(start == sections[secIdx].Start && end == sections[secIdx].End) {
t.current = t.current[1:]
continue
}

t.current[0].symbol = true
t.current[0].symbolIdx = uint32(secIdx)
trimmed = append(trimmed, t.current[0])
camdencheek marked this conversation as resolved.
Show resolved Hide resolved
t.current = t.current[1:]
}
t.current = trimmed
Expand Down Expand Up @@ -983,14 +991,20 @@ func (d *indexData) newMatchTree(q query.Q, opt matchTreeOpt) (matchTree, error)
optCopy := opt
optCopy.DisableWordMatchOptimization = true

subMT, err := d.newMatchTree(s.Expr, optCopy)
expr, wasAnchored := s.Expr, false
if regexpExpr, ok := expr.(*query.Regexp); ok {
expr, wasAnchored = stripAnchors(regexpExpr)
}

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.

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.

patternSize: uint32(utf8.RuneCountInString(substr.query.Pattern)),
fileEndRunes: d.fileEndRunes,
fileEndSymbol: d.fileEndSymbol,
Expand Down Expand Up @@ -1256,3 +1270,37 @@ func pruneMatchTree(mt matchTree) (matchTree, error) {
}
return mt, err
}

func stripAnchors(in *query.Regexp) (out *query.Regexp, stripped bool) {
stripRegexpAnchors := func(in *syntax.Regexp) (out *syntax.Regexp, stripped bool) {
if in.Op != syntax.OpConcat {
return out, false
}

if len(in.Sub) < 3 {
return out, false
}

firstOp, lastOp := in.Sub[0].Op, in.Sub[len(in.Sub)-1].Op

if firstOp != syntax.OpBeginLine && firstOp != syntax.OpBeginText {
return out, false
}
if lastOp != syntax.OpEndLine && lastOp != syntax.OpEndText {
return out, false
}

inCopy := *in
inCopy.Sub = in.Sub[1 : len(in.Sub)-1] // remove the first and last ops, which are the anchors
return &inCopy, true
}

newRegexp, stripped := stripRegexpAnchors(in.Regexp)
if !stripped {
return in, false
}

inCopy := *in
inCopy.Regexp = newRegexp
return &inCopy, true
}
Loading