Skip to content

Commit

Permalink
remove RegexpLen() function, since it returns almost the same as len(…
Browse files Browse the repository at this point in the history
…regexp)

This should increase the max cache size by roughly 2x
  • Loading branch information
valyala committed Jun 30, 2022
1 parent 5e45378 commit a0b9b65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
13 changes: 3 additions & 10 deletions regexp_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ type regexpCacheValue struct {
err error
}

func (rcv *regexpCacheValue) RegexpLen() int {
if r := rcv.r; r != nil {
return len(r.String())
}
return len(rcv.err.Error())
}

type regexpCache struct {
// Move atomic counters to the top of struct for 8-byte alignment on 32-bit arch.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/212
Expand Down Expand Up @@ -131,10 +124,10 @@ func (rc *regexpCache) Put(regexp string, rcv *regexpCacheValue) {
if rc.charsCurrent > rc.charsLimit {
// Remove items accounting for 10% chars from the cache.
overflow := int(float64(rc.charsLimit) * 0.1)
for k, v := range rc.m {
for k := range rc.m {
delete(rc.m, k)

size := len(k) + v.RegexpLen()
size := len(k)
overflow -= size
rc.charsCurrent -= size

Expand All @@ -144,6 +137,6 @@ func (rc *regexpCache) Put(regexp string, rcv *regexpCacheValue) {
}
}
rc.m[regexp] = rcv
rc.charsCurrent += len(regexp) + rcv.RegexpLen()
rc.charsCurrent += len(regexp)
rc.mu.Unlock()
}
18 changes: 9 additions & 9 deletions regexp_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ func TestRegexpCache(t *testing.T) {
}
}

fn(10, []string{"a", "b", "c"}, 3, 6)
fn(2, []string{"a", "b", "c"}, 2, 4) // overflow by 1 entry is allowed
fn(2, []string{"a", "b", "c", "d"}, 2, 4)
fn(1, []string{"a", "b", "c"}, 1, 2)
fn(2, []string{"abcd", "efgh", "ijkl"}, 1, 8) // overflow by 1 entry is allowed
fn(5, []string{"123", "fd{456", "789"}, 1, 6) // overflow by 1 entry is allowed
fn(18, []string{"123", "fd{456", "789"}, 3, 24) // overflow by 1 entry is allowed
fn(24, []string{"123", "fd{456", "789"}, 3, 24)
fn(30, []string{"123", "fd{456", "789"}, 3, 24)
fn(10, []string{"a", "b", "c"}, 3, 3)
fn(2, []string{"a", "b", "c"}, 3, 3) // overflow by 1 entry is allowed
fn(2, []string{"a", "b", "c", "d"}, 3, 3)
fn(1, []string{"a", "b", "c"}, 2, 2) // overflow by 1 tnery is allowed
fn(2, []string{"abcd", "efgh", "ijkl"}, 1, 4) // overflow by 1 entry is allowed
fn(2, []string{"123", "fd{456", "789"}, 1, 3) // overflow by 1 entry is allowed
fn(9, []string{"123", "fd{456", "789"}, 3, 12) // overflow by 1 entry is allowed
fn(12, []string{"123", "fd{456", "789"}, 3, 12)
fn(15, []string{"123", "fd{456", "789"}, 3, 12)
}

0 comments on commit a0b9b65

Please sign in to comment.