Skip to content

Commit

Permalink
cache compiled regex
Browse files Browse the repository at this point in the history
  • Loading branch information
kaanaktas committed Dec 12, 2023
1 parent 013a8ec commit cc96254
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
5 changes: 3 additions & 2 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"github.com/kaanaktas/go-slm/config"
"github.com/kaanaktas/go-slm/executor"
"io/ioutil"
"io"
"log"
"os"
"testing"
Expand All @@ -17,7 +17,7 @@ func init() {

log.Println("Starting with number of worker", config.NumberOfWorker)
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
}

func Benchmark(b *testing.B) {
Expand All @@ -27,6 +27,7 @@ func Benchmark(b *testing.B) {
data := string(content)
serviceName := "test3"

b.ResetTimer()
for i := 0; i < b.N; i++ {
executor.Apply(data, serviceName, config.Request)
}
Expand Down
18 changes: 11 additions & 7 deletions datafilter/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ type Validator interface {

// PatternValidator represents a validation rule based on a regular expression patternValidator.
type patternValidator struct {
Name string `json:"name"`
Rule string `json:"rule"`
Sample string `json:"sample"`
Message string `json:"message"`
Disable bool `json:"disable"`
Name string `yaml:"name"`
Regex *regexp.Regexp `yaml:"-"`
Rule string `yaml:"rule"`
Sample string `yaml:"sample"`
Message string `yaml:"message"`
Disable bool `yaml:"disable"`
}

// Validate checks whether the given data string satisfies the regular expression pattern.
func (pv *patternValidator) Validate(data *string) bool {
matched, _ := regexp.MatchString(pv.Rule, *data)
return matched
return pv.Regex.MatchString(*data)
}

// ToString returns a string representation of the validation rule.
Expand All @@ -36,3 +36,7 @@ func (pv *patternValidator) ToString() string {
func (pv *patternValidator) IsDisabled() bool {
return pv.Disable
}

func (pv *patternValidator) compileRule() {
pv.Regex = regexp.MustCompile(pv.Rule)
}
4 changes: 3 additions & 1 deletion datafilter/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"
)

//filter types
// filter types
const (
PAN = "pan"
OWASP = "owasp"
Expand Down Expand Up @@ -50,10 +50,12 @@ func LoadDataFilterRules(dataFilterRuleSetPath string) {
switch set.Type {
case PAN:
for i, v := range patterns {
v.compileRule()
validateRule[i] = &pan{patternValidator: v}
}
case OWASP:
for i, v := range patterns {
v.compileRule()
validateRule[i] = &owasp{patternValidator: v}
}
}
Expand Down
4 changes: 1 addition & 3 deletions datafilter/pan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package datafilter

import (
"math"
"regexp"
"strconv"
"strings"
)
Expand All @@ -13,8 +12,7 @@ type pan struct {

func (p *pan) Validate(data *string) bool {
dataWithoutSpace := strings.ReplaceAll(*data, " ", "")
r := regexp.MustCompile(p.Rule)
matchList := r.FindAllString(dataWithoutSpace, -1)
matchList := p.Regex.FindAllString(dataWithoutSpace, -1)
for _, v := range matchList {
if isValidPan(v) {
return true
Expand Down

0 comments on commit cc96254

Please sign in to comment.