-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathregexp_cache.go
107 lines (98 loc) · 4.03 KB
/
regexp_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package authr
import (
"container/list"
"regexp"
"sync"
)
type regexpCacheEntry struct {
p string
r *regexp.Regexp
}
type regexpCache interface {
add(pattern string, r *regexp.Regexp)
find(pattern string) (*regexp.Regexp, bool)
}
// regexpListCache is a runtime cache for preventing needless regexp.Compile
// operations since it can be expensive in hot areas.
//
// this cache has an LRU eviction policy and uses a double-linked list to
// efficiently shift/remove entries without introducing more CPU overhead.
//
// this cache *significantly* reduces the expense of regexp operators in authr as
// proven by benchmarks (old is noopRegexpCache, new is regexpListCache):
//
// benchmark old ns/op new ns/op delta
// BenchmarkRegexpOperatorSerial/int(33)~*^foo$=>false-8 3755 336 -91.05%
// BenchmarkRegexpOperatorSerial/"foo-one"~*^Foo=>true-8 5803 315 -94.57%
// BenchmarkRegexpOperatorSerial/"bar-two"~^Bar=>false-8 5678 206 -96.37%
// BenchmarkRegexpOperatorParallel/int(33)~*^foo$=>false-8 1428 417 -70.80%
// BenchmarkRegexpOperatorParallel/"foo-one"~*^Foo=>true-8 6516 427 -93.45%
// BenchmarkRegexpOperatorParallel/"bar-two"~^Bar=>false-8 6194 384 -93.80%
// BenchmarkRegexpOperatorThrash-8 6019 2355 -60.87%
//
// benchmark old allocs new allocs delta
// BenchmarkRegexpOperatorSerial/int(33)~*^foo$=>false-8 52 3 -94.23%
// BenchmarkRegexpOperatorSerial/"foo-one"~*^Foo=>true-8 29 2 -93.10%
// BenchmarkRegexpOperatorSerial/"bar-two"~^Bar=>false-8 28 1 -96.43%
// BenchmarkRegexpOperatorParallel/int(33)~*^foo$=>false-8 52 3 -94.23%
// BenchmarkRegexpOperatorParallel/"foo-one"~*^Foo=>true-8 29 2 -93.10%
// BenchmarkRegexpOperatorParallel/"bar-two"~^Bar=>false-8 28 1 -96.43%
// BenchmarkRegexpOperatorThrash-8 36 14 -61.11%
//
// benchmark old bytes new bytes delta
// BenchmarkRegexpOperatorSerial/int(33)~*^foo$=>false-8 3297 32 -99.03%
// BenchmarkRegexpOperatorSerial/"foo-one"~*^Foo=>true-8 39016 24 -99.94%
// BenchmarkRegexpOperatorSerial/"bar-two"~^Bar=>false-8 39008 16 -99.96%
// BenchmarkRegexpOperatorParallel/int(33)~*^foo$=>false-8 3299 32 -99.03%
// BenchmarkRegexpOperatorParallel/"foo-one"~*^Foo=>true-8 39016 24 -99.94%
// BenchmarkRegexpOperatorParallel/"bar-two"~^Bar=>false-8 39008 16 -99.96%
// BenchmarkRegexpOperatorThrash-8 27071 9086 -66.44%
type regexpListCache struct {
sync.Mutex
// capacity
c int
// current length
s int
l *list.List
}
func newRegexpListCache(capacity int) *regexpListCache {
if capacity < 0 {
panic("negative regexp cache")
}
return ®expListCache{
c: capacity,
s: 0,
l: list.New().Init(),
}
}
func (r *regexpListCache) add(pattern string, _r *regexp.Regexp) {
r.Lock()
defer r.Unlock()
if r.s > r.c {
panic("regexpListCache overflow")
}
if r.s == r.c {
r.l.Remove(r.l.Back())
r.s--
}
r.l.PushFront(®expCacheEntry{p: pattern, r: _r})
r.s++
}
func (r *regexpListCache) find(pattern string) (*regexp.Regexp, bool) {
r.Lock()
defer r.Unlock()
var e *list.Element = r.l.Front()
for e != nil {
if e.Value.(*regexpCacheEntry).p == pattern {
r.l.MoveToFront(e)
return e.Value.(*regexpCacheEntry).r, true
}
e = e.Next()
}
return nil, false
}
type noopRegexpCache struct{}
func (n *noopRegexpCache) add(_ string, _ *regexp.Regexp) {}
func (n *noopRegexpCache) find(_ string) (*regexp.Regexp, bool) {
return nil, false
}