MCPcopy
hub / github.com/grafana/tempo / shouldMemoize

Function shouldMemoize

pkg/regexp/regexp.go:111–123  ·  view source on GitHub ↗

shouldMemoize returns true if we believe that memoizing this regex would be faster the evaluating it directly. see thoughts below.

(m *labels.FastRegexMatcher)

Source from the content-addressed store, hash-verified

109// shouldMemoize returns true if we believe that memoizing this regex would be faster
110// the evaluating it directly. see thoughts below.
111func shouldMemoize(m *labels.FastRegexMatcher) bool {
112 // This is unsafe but validated by tests.
113 cheat := (*cheatToSeeInternals)(unsafe.Pointer(m))
114
115 // TODO: research and improve this. we're making a guess on whether an optimization will improve the regex
116 // performance enough that its faster to not memoize. See compileMatchStringFunction() in the labels
117 // package. maybe there's even a way to do this dynamically?
118 return cheat.stringMatcher == nil && // a stringMatcher definitively rejects or accepts. if a string matcher is present the regex will never be executed
119 len(cheat.setMatches) == 0 && // setMatches definitively reject or accept. if len != 0 the regex will never be executed, but perhaps if there are a very large # of setMatches we prefer memoizing anyway?
120 cheat.prefix == "" && // prefix and suffix _do not_ prevent the regex from executing, but they are quick to evaluate and tend to nicely filter down.
121 cheat.suffix == "" // perhaps a length requirement would be an improvement? i.e. require a prefix or suffix of at least 3 chars?
122 // len(cheat.contains) == 0 // in testing, it was faster to memoize with a contains filter. perhaps if the filters are long enough we don't memoize?
123}

Callers 2

TestShouldMemoizeFunction · 0.85
NewRegexpFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestShouldMemoizeFunction · 0.68