Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 2 additions & 5 deletions util/builtin_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,8 @@ func KeyMatch5Func(args ...interface{}) (interface{}, error) {

// RegexMatch determines whether key1 matches the pattern of key2 in regular expression.
func RegexMatch(key1 string, key2 string) bool {
res, err := regexp.MatchString(key2, key1)
if err != nil {
panic(err)
}
return res
re := mustCompileOrGet(key2)
return re.MatchString(key1)
}

// RegexMatchFunc is the wrapper for RegexMatch.
Expand Down
37 changes: 37 additions & 0 deletions util/builtin_operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,40 @@
testTimeMatch(t, "0000-01-01 00:00:00", "_", true)
testTimeMatch(t, "9999-12-30 00:00:00", "_", false)
}

// BenchmarkRegexMatch benchmarks the RegexMatch function with cached regex compilation

Check failure on line 666 in util/builtin_operators_test.go

View workflow job for this annotation

GitHub Actions / golangci

Comment should end in a period (godot)
func BenchmarkRegexMatch(b *testing.B) {
patterns := []string{

Check failure on line 668 in util/builtin_operators_test.go

View workflow job for this annotation

GitHub Actions / golangci

File is not `goimports`-ed (goimports)
"/topic/create",
"/topic/edit/[0-9]+",
"/topic/delete/[0-9]+",
"^/api/[a-z]+/[0-9]+$",
"^/resource/[a-zA-Z0-9_-]+/action$",
}
inputs := []string{
"/topic/create",
"/topic/edit/123",
"/topic/delete/456",
"/api/users/789",
"/resource/test_resource/action",
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
pattern := patterns[i%len(patterns)]
input := inputs[i%len(inputs)]
RegexMatch(input, pattern)
}
}

// BenchmarkRegexMatchSamePattern benchmarks RegexMatch with the same pattern repeatedly
// This demonstrates the caching benefit

Check failure on line 692 in util/builtin_operators_test.go

View workflow job for this annotation

GitHub Actions / golangci

Comment should end in a period (godot)
func BenchmarkRegexMatchSamePattern(b *testing.B) {
pattern := "/topic/edit/[0-9]+"
input := "/topic/edit/123"

b.ResetTimer()
for i := 0; i < b.N; i++ {
RegexMatch(input, pattern)
}
}
Loading