mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Autotag optimisation (#2368)
* Add duration to autotag finish message * No sorting scene/image/gallery where not specified * Use an LRU cache for sqlite regexp function * Compile path separator regex once * Cache objects with single letter first names * Move finished auto-tag log * Add more verbose logging * Add new changelog
This commit is contained in:
42
pkg/database/regex.go
Normal file
42
pkg/database/regex.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
)
|
||||
|
||||
// size of the regex LRU cache in elements.
|
||||
// A small number number was chosen because it's most likely use is for a
|
||||
// single query - this function gets called for every row in the (filtered)
|
||||
// results. It's likely to only need no more than 1 or 2 in any given query.
|
||||
// After that point, it's just sitting in the cache and is unlikely to be used
|
||||
// again.
|
||||
const regexCacheSize = 10
|
||||
|
||||
var regexCache *lru.Cache
|
||||
|
||||
func init() {
|
||||
regexCache, _ = lru.New(regexCacheSize)
|
||||
}
|
||||
|
||||
// regexFn is registered as an SQLite function as "regexp"
|
||||
// It uses an LRU cache to cache recent regex patterns to reduce CPU load over
|
||||
// identical patterns.
|
||||
func regexFn(re, s string) (bool, error) {
|
||||
entry, ok := regexCache.Get(re)
|
||||
var compiled *regexp.Regexp
|
||||
|
||||
if !ok {
|
||||
var err error
|
||||
compiled, err = regexp.Compile(re)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
regexCache.Add(re, compiled)
|
||||
} else {
|
||||
compiled = entry.(*regexp.Regexp)
|
||||
}
|
||||
|
||||
return compiled.MatchString(s), nil
|
||||
}
|
||||
Reference in New Issue
Block a user