Prefilter indexer regexps with literal checks

Go's regexp backtracker dominated indexing. Each pattern now runs only
when a cheap necessary-condition check passes, cutting indexing of the
Go standard library source from 4.7s to 1.4s.
Adds BenchmarkIndexDirectory.
This commit is contained in:
Andy Dixon
2026-01-25 15:57:26 +00:00
parent 5816dea6b1
commit 62c363b031
2 changed files with 71 additions and 20 deletions
+19 -2
View File
@@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
)
@@ -63,8 +64,8 @@ func TestResolvesDottedModulesAsInferred(t *testing.T) {
func TestExtractsNamedJavaScriptImportTarget(t *testing.T) {
var imports []string
for _, re := range importPatterns {
if m := re.FindStringSubmatch("import { render } from './renderer.js';"); m != nil {
for _, p := range importPatterns {
if m := p.re.FindStringSubmatch("import { render } from './renderer.js';"); m != nil {
imports = append(imports, m[1])
}
}
@@ -207,3 +208,19 @@ func TestEmptyDirectoryFails(t *testing.T) {
t.Fatal("expected error for empty directory")
}
}
// BenchmarkIndexDirectory indexes CODENAV_BENCH_DIR, defaulting to the Go standard library source.
func BenchmarkIndexDirectory(b *testing.B) {
dir := os.Getenv("CODENAV_BENCH_DIR")
if dir == "" {
dir = filepath.Join(runtime.GOROOT(), "src")
}
if _, err := os.Stat(dir); err != nil {
b.Skip("no benchmark corpus:", err)
}
for b.Loop() {
if _, err := indexDirectory(dir, func(string, int, int, string) {}); err != nil {
b.Fatal(err)
}
}
}