Skip to content

Commit

Permalink
FastRegexMatcher: use stack memory for lowercase copy of string
Browse files Browse the repository at this point in the history
Up to 32-byte values this saves garbage, runs faster.
For prefixes, only `toLower` the part we need for the map lookup.

Split toNormalisedLower into fast and slow paths, to avoid a penalty
for the `copy` call in the case where no allocations are done.

Signed-off-by: Bryan Boreham <[email protected]>
  • Loading branch information
bboreham committed Nov 8, 2024
1 parent a84c500 commit 43f18cf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
54 changes: 39 additions & 15 deletions model/labels/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ type equalMultiStringMapMatcher struct {

func (m *equalMultiStringMapMatcher) add(s string) {
if !m.caseSensitive {
s = toNormalisedLower(s)
s = toNormalisedLower(s, nil) // Don't pass a stack buffer here - it will always escape to heap.
}

m.values[s] = struct{}{}
Expand Down Expand Up @@ -940,15 +940,24 @@ func (m *equalMultiStringMapMatcher) setMatches() []string {
}

func (m *equalMultiStringMapMatcher) Matches(s string) bool {
if !m.caseSensitive {
s = toNormalisedLower(s)
if len(m.values) > 0 {
sNorm := s
var a [32]byte
if !m.caseSensitive {
sNorm = toNormalisedLower(s, a[:])
}
if _, ok := m.values[sNorm]; ok {
return true
}
}

if _, ok := m.values[s]; ok {
return true
}
if m.minPrefixLen > 0 && len(s) >= m.minPrefixLen {
for _, matcher := range m.prefixes[s[:m.minPrefixLen]] {
prefix := s[:m.minPrefixLen]
var a [32]byte
if !m.caseSensitive {
prefix = toNormalisedLower(s[:m.minPrefixLen], a[:])
}
for _, matcher := range m.prefixes[prefix] {
if matcher.Matches(s) {
return true
}
Expand All @@ -959,22 +968,37 @@ func (m *equalMultiStringMapMatcher) Matches(s string) bool {

// toNormalisedLower normalise the input string using "Unicode Normalization Form D" and then convert
// it to lower case.
func toNormalisedLower(s string) string {
var buf []byte
func toNormalisedLower(s string, a []byte) string {
for i := 0; i < len(s); i++ {
c := s[i]
if c >= utf8.RuneSelf {
return strings.Map(unicode.ToLower, norm.NFKD.String(s))
}
if 'A' <= c && c <= 'Z' {
if buf == nil {
buf = []byte(s)
}
buf[i] = c + 'a' - 'A'
return toNormalisedLowerSlow(s, i, a)
}
}
if buf == nil {
return s
return s
}

// toNormalisedLowerSlow is split from toNormalisedLower because having a call
// to `copy` slows it down even when it is not called.
func toNormalisedLowerSlow(s string, i int, a []byte) string {
var buf []byte
if cap(a) > len(s) {
buf = a[:len(s)]
copy(buf, s)
} else {
buf = []byte(s)
}
for ; i < len(s); i++ {
c := s[i]
if c >= utf8.RuneSelf {
return strings.Map(unicode.ToLower, norm.NFKD.String(s))
}
if 'A' <= c && c <= 'Z' {
buf[i] = c + 'a' - 'A'
}
}
return yoloString(buf)
}
Expand Down
5 changes: 3 additions & 2 deletions model/labels/regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ func BenchmarkToNormalizedLower(b *testing.B) {
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
toNormalisedLower(inputs[n%len(inputs)])
var a [256]byte
toNormalisedLower(inputs[n%len(inputs)], a[:])
}
})
}
Expand Down Expand Up @@ -1759,6 +1760,6 @@ func TestToNormalisedLower(t *testing.T) {
"ſſAſſa": "ssassa",
}
for input, expectedOutput := range testCases {
require.Equal(t, expectedOutput, toNormalisedLower(input))
require.Equal(t, expectedOutput, toNormalisedLower(input, nil))
}
}

0 comments on commit 43f18cf

Please sign in to comment.