mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 19:51:50 +00:00
symbols: Cache regex compilations (#49868)
This commit is contained in:
parent
be5762b5c2
commit
3a8be84e27
@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/grafana/regexp"
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
@ -11,7 +12,26 @@ func Init() {
|
||||
sql.Register("sqlite3_with_regexp",
|
||||
&sqlite3.SQLiteDriver{
|
||||
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
||||
return conn.RegisterFunc("REGEXP", regexp.MatchString, true)
|
||||
return conn.RegisterFunc("REGEXP", MatchString, true)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
var (
|
||||
cacheSize = 1000
|
||||
regexCache, _ = lru.New(cacheSize)
|
||||
)
|
||||
|
||||
func MatchString(pattern string, s string) (bool, error) {
|
||||
if re, ok := regexCache.Get(pattern); ok {
|
||||
return re.(*regexp.Regexp).MatchString(s), nil
|
||||
}
|
||||
|
||||
re, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
regexCache.Add(pattern, re)
|
||||
return re.MatchString(s), nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user