symbols: Cache regex compilations (#49868)

This commit is contained in:
Eric Fritz 2023-03-22 15:54:08 -05:00 committed by GitHub
parent be5762b5c2
commit 3a8be84e27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}