Speed up filter matches by re-using global match object

Instead of compiling filter regexp on every filter match pre-compile it and use same instance
This commit is contained in:
Łukasz Mierzwa
2017-05-05 22:36:50 +01:00
parent 7bf0b9d5d0
commit 003010f73b
2 changed files with 10 additions and 2 deletions

View File

@@ -96,8 +96,7 @@ func NewFilter(expression string) FilterT {
// we have "filter=" part, lookup filter that matches
for _, fc := range AllFilters {
f := fc.Factory()
labelRe := regexp.MustCompile("^(?:" + fc.Label + ")$")
if !labelRe.MatchString(matched) {
if !fc.LabelRe.MatchString(matched) {
// filter name doesn't match, keep searching
continue
}

View File

@@ -1,5 +1,7 @@
package filters
import "regexp"
const (
equalOperator string = "="
notEqualOperator string = "!="
@@ -29,6 +31,7 @@ var matcherConfig = map[string]matcherT{
type filterConfig struct {
Label string
LabelRe *regexp.Regexp
SupportedOperators []string
Factory newFilterFactory
Autocomplete autocompleteFactory
@@ -39,36 +42,42 @@ type filterConfig struct {
var AllFilters = []filterConfig{
filterConfig{
Label: "@status",
LabelRe: regexp.MustCompile("^@status$"),
SupportedOperators: []string{equalOperator, notEqualOperator},
Factory: newstatusFilter,
Autocomplete: statusAutocomplete,
},
filterConfig{
Label: "@age",
LabelRe: regexp.MustCompile("^@age$"),
SupportedOperators: []string{lessThanOperator, moreThanOperator},
Factory: newAgeFilter,
Autocomplete: ageAutocomplete,
},
filterConfig{
Label: "@silence_jira",
LabelRe: regexp.MustCompile("^@silence_jira$"),
SupportedOperators: []string{regexpOperator, negativeRegexOperator, equalOperator, notEqualOperator},
Factory: newSilenceJiraFilter,
Autocomplete: sinceJiraIDAutocomplete,
},
filterConfig{
Label: "@silence_author",
LabelRe: regexp.MustCompile("^@silence_author$"),
SupportedOperators: []string{regexpOperator, negativeRegexOperator, equalOperator, notEqualOperator},
Factory: newSilenceAuthorFilter,
Autocomplete: sinceAuthorAutocomplete,
},
filterConfig{
Label: "@limit",
LabelRe: regexp.MustCompile("^@limit$"),
SupportedOperators: []string{equalOperator},
Factory: newLimitFilter,
Autocomplete: limitAutocomplete,
},
filterConfig{
Label: "[a-zA-Z_][a-zA-Z0-9_]*",
LabelRe: regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$"),
SupportedOperators: []string{regexpOperator, negativeRegexOperator, equalOperator, notEqualOperator, lessThanOperator, moreThanOperator},
Factory: newLabelFilter,
Autocomplete: labelAutocomplete,