Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(filter): replace map with slice in hot path #1115

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions filter/series_by_tag_pattern_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ type SeriesByTagPatternIndex struct {
// namesPrefixTree stores MatchingHandler's for patterns that have name tag in prefix tree structure
namesPrefixTree *PrefixTree
// withoutStrictNameTagPatternMatchers stores MatchingHandler's for patterns that have no name tag
withoutStrictNameTagPatternMatchers map[string]MatchingHandler
withoutStrictNameTagPatternMatchers []patternAndHandler
// Flags for compatibility with different graphite behaviours
compatibility Compatibility
}

type patternAndHandler struct {
pattern string
handler MatchingHandler
}

// NewSeriesByTagPatternIndex creates new SeriesByTagPatternIndex using seriesByTag patterns and parsed specs comes from ParseSeriesByTag.
func NewSeriesByTagPatternIndex(
logger moira.Logger,
Expand All @@ -25,7 +30,7 @@ func NewSeriesByTagPatternIndex(
metrics *metrics.FilterMetrics,
) *SeriesByTagPatternIndex {
namesPrefixTree := &PrefixTree{Logger: logger, Root: &PatternNode{}}
withoutStrictNameTagPatternMatchers := make(map[string]MatchingHandler)
withoutStrictNameTagPatternMatchers := make([]patternAndHandler, 0)

var patternMatchingEvicted int64

Expand Down Expand Up @@ -54,7 +59,7 @@ func NewSeriesByTagPatternIndex(
}

if patternMatching.nameTagValue == "" {
withoutStrictNameTagPatternMatchers[pattern] = patternMatching.matchingHandler
withoutStrictNameTagPatternMatchers = append(withoutStrictNameTagPatternMatchers, patternAndHandler{pattern, patternMatching.matchingHandler})
AleksandrMatsko marked this conversation as resolved.
Show resolved Hide resolved
} else {
namesPrefixTree.AddWithPayload(patternMatching.nameTagValue, pattern, patternMatching.matchingHandler)
}
Expand All @@ -80,9 +85,9 @@ func (index *SeriesByTagPatternIndex) MatchPatterns(metricName string, labels ma
}
index.namesPrefixTree.MatchWithValue(metricName, callback)

for pattern, matchingHandler := range index.withoutStrictNameTagPatternMatchers {
if matchingHandler(metricName, labels) {
matchedPatterns = append(matchedPatterns, pattern)
for _, patternAndHandler := range index.withoutStrictNameTagPatternMatchers {
if patternAndHandler.handler(metricName, labels) {
matchedPatterns = append(matchedPatterns, patternAndHandler.pattern)
}
}

Expand Down
Loading