add a match arena to regexp to avoid generating garbage.

simple regexps run 20x faster.
the regex-dna benchmark goes 3x faster.

R=rsc
CC=golang-dev
https://golang.org/cl/156108
This commit is contained in:
Rob Pike 2009-11-19 23:12:01 -08:00
parent b30f753dc3
commit c62069cc1f
2 changed files with 134 additions and 47 deletions

View file

@ -60,6 +60,7 @@ type tester struct {
}
var matches = []tester{
tester{`^abcdefg`, "abcdefg", vec{0, 7}},
tester{`a+`, "baaab", vec{1, 4}},
tester{"abcd..", "abcdef", vec{0, 6}},
tester{``, "", vec{0, 0}},
@ -450,3 +451,29 @@ func TestAllMatches(t *testing.T) {
}
}
}
func BenchmarkLiteral(b *testing.B) {
x := strings.Repeat("x", 50);
b.StopTimer();
re, _ := Compile(x);
b.StartTimer();
for i := 0; i < b.N; i++ {
if !re.MatchString(x) {
println("no match!");
break;
}
}
}
func BenchmarkNotLiteral(b *testing.B) {
x := strings.Repeat("x", 49);
b.StopTimer();
re, _ := Compile("^" + x);
b.StartTimer();
for i := 0; i < b.N; i++ {
if !re.MatchString(x) {
println("no match!");
break;
}
}
}