2022-09-30 07:38:44 +00:00
|
|
|
package bytesutil
|
|
|
|
|
|
|
|
import (
|
2022-10-14 06:49:53 +00:00
|
|
|
"strings"
|
2022-09-30 07:38:44 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2022-12-12 22:31:16 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2022-09-30 07:38:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FastStringMatcher implements fast matcher for strings.
|
|
|
|
//
|
|
|
|
// It caches string match results and returns them back on the next calls
|
|
|
|
// without calling the matchFunc, which may be expensive.
|
|
|
|
type FastStringMatcher struct {
|
2022-12-12 22:31:16 +00:00
|
|
|
lastCleanupTime uint64
|
|
|
|
|
|
|
|
m sync.Map
|
2022-09-30 07:38:44 +00:00
|
|
|
|
|
|
|
matchFunc func(s string) bool
|
|
|
|
}
|
|
|
|
|
2022-12-12 22:31:16 +00:00
|
|
|
type fsmEntry struct {
|
|
|
|
lastAccessTime uint64
|
|
|
|
ok bool
|
|
|
|
}
|
|
|
|
|
2022-09-30 07:38:44 +00:00
|
|
|
// NewFastStringMatcher creates new matcher, which applies matchFunc to strings passed to Match()
|
|
|
|
//
|
|
|
|
// matchFunc must return the same result for the same input.
|
|
|
|
func NewFastStringMatcher(matchFunc func(s string) bool) *FastStringMatcher {
|
2022-12-12 22:31:16 +00:00
|
|
|
return &FastStringMatcher{
|
|
|
|
lastCleanupTime: fasttime.UnixTimestamp(),
|
|
|
|
matchFunc: matchFunc,
|
|
|
|
}
|
2022-09-30 07:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Match applies matchFunc to s and returns the result.
|
|
|
|
func (fsm *FastStringMatcher) Match(s string) bool {
|
2023-02-27 22:15:49 +00:00
|
|
|
if isSkipCache(s) {
|
|
|
|
return fsm.matchFunc(s)
|
|
|
|
}
|
|
|
|
|
2022-12-12 22:31:16 +00:00
|
|
|
ct := fasttime.UnixTimestamp()
|
|
|
|
v, ok := fsm.m.Load(s)
|
2022-09-30 07:38:44 +00:00
|
|
|
if ok {
|
|
|
|
// Fast path - s match result is found in the cache.
|
2022-12-12 22:31:16 +00:00
|
|
|
e := v.(*fsmEntry)
|
|
|
|
if atomic.LoadUint64(&e.lastAccessTime)+10 < ct {
|
|
|
|
// Reduce the frequency of e.lastAccessTime update to once per 10 seconds
|
|
|
|
// in order to improve the fast path speed on systems with many CPU cores.
|
|
|
|
atomic.StoreUint64(&e.lastAccessTime, ct)
|
|
|
|
}
|
|
|
|
return e.ok
|
2022-09-30 07:38:44 +00:00
|
|
|
}
|
|
|
|
// Slow path - run matchFunc for s and store the result in the cache.
|
|
|
|
b := fsm.matchFunc(s)
|
2022-12-12 22:31:16 +00:00
|
|
|
e := &fsmEntry{
|
|
|
|
lastAccessTime: ct,
|
|
|
|
ok: b,
|
|
|
|
}
|
2022-10-14 06:49:53 +00:00
|
|
|
// Make a copy of s in order to limit memory usage to the s length,
|
|
|
|
// since the s may point to bigger string.
|
|
|
|
// This also protects from the case when s contains unsafe string, which points to a temporary byte slice.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3227
|
|
|
|
s = strings.Clone(s)
|
2022-12-12 22:31:16 +00:00
|
|
|
fsm.m.Store(s, e)
|
|
|
|
|
2022-12-21 20:57:28 +00:00
|
|
|
if needCleanup(&fsm.lastCleanupTime, ct) {
|
|
|
|
// Perform a global cleanup for fsm.m by removing items, which weren't accessed during the last 5 minutes.
|
2022-12-12 22:31:16 +00:00
|
|
|
m := &fsm.m
|
2023-02-27 22:15:49 +00:00
|
|
|
deadline := ct - uint64(cacheExpireDuration.Seconds())
|
2022-12-12 22:31:16 +00:00
|
|
|
m.Range(func(k, v interface{}) bool {
|
|
|
|
e := v.(*fsmEntry)
|
2023-02-27 22:15:49 +00:00
|
|
|
if atomic.LoadUint64(&e.lastAccessTime) < deadline {
|
2022-12-12 22:31:16 +00:00
|
|
|
m.Delete(k)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2022-09-30 07:38:44 +00:00
|
|
|
}
|
2022-12-12 22:31:16 +00:00
|
|
|
|
2022-09-30 07:38:44 +00:00
|
|
|
return b
|
|
|
|
}
|
2022-12-21 20:57:28 +00:00
|
|
|
|
|
|
|
func needCleanup(lastCleanupTime *uint64, currentTime uint64) bool {
|
|
|
|
lct := atomic.LoadUint64(lastCleanupTime)
|
|
|
|
if lct+61 >= currentTime {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Atomically compare and swap the current time with the lastCleanupTime
|
|
|
|
// in order to guarantee that only a single goroutine out of multiple
|
|
|
|
// concurrently executing goroutines gets true from the call.
|
|
|
|
return atomic.CompareAndSwapUint64(lastCleanupTime, lct, currentTime)
|
|
|
|
}
|