This helps users who wish to use separate Regexps in each goroutine to
avoid lock contention. Previously they had to parse the expression
multiple times to achieve this.
I used variants of the included benchmark to evaluate this change. I
used the arguments -benchtime 20s -cpu 1,2,4,8,16 on a machine with 16
hardware cores.
Comparing a single shared Regexp vs. copied Regexps, we can see that
lock contention causes huge slowdowns at higher levels of parallelism.
The copied version shows the expected linear speedup.
name old time/op new time/op delta
MatchParallel 366ns ± 0% 370ns ± 0% +1.09% (p=0.000 n=10+8)
MatchParallel-2 324ns ±28% 184ns ± 1% -43.37% (p=0.000 n=10+10)
MatchParallel-4 352ns ± 5% 93ns ± 1% -73.70% (p=0.000 n=9+10)
MatchParallel-8 480ns ± 3% 46ns ± 0% -90.33% (p=0.000 n=9+8)
MatchParallel-16 510ns ± 8% 24ns ± 6% -95.36% (p=0.000 n=10+8)
I also compared a modified version of Regexp that has no mutex and a
single machine (the "RegexpForSingleGoroutine" rsc mentioned in
https://github.com/golang/go/issues/8232#issuecomment-
66096128).
In this next test, I compared using N copied Regexps vs. N separate
RegexpForSingleGoroutines. This shows that, even for this relatively
simple regex, avoiding the lock entirely would only buy about 10-12%
further improvement.
name old time/op new time/op delta
MatchParallel 370ns ± 0% 322ns ± 0% -12.97% (p=0.000 n=8+8)
MatchParallel-2 184ns ± 1% 162ns ± 1% -11.60% (p=0.000 n=10+10)
MatchParallel-4 92.7ns ± 1% 81.1ns ± 2% -12.43% (p=0.000 n=10+10)
MatchParallel-8 46.4ns ± 0% 41.8ns ±10% -9.78% (p=0.000 n=8+10)
MatchParallel-16 23.7ns ± 6% 20.6ns ± 1% -13.14% (p=0.000 n=8+8)
Updates #8232.
Change-Id: I15201a080c363d1b44104eafed46d8df5e311902
Reviewed-on: https://go-review.googlesource.com/16110
Reviewed-by: Russ Cox <rsc@golang.org>
}
}
+func copyMatchTest(t *testing.T, test *FindTest) {
+ re := compileTest(t, test.pat, "")
+ if re == nil {
+ return
+ }
+ m1 := re.MatchString(test.text)
+ m2 := re.Copy().MatchString(test.text)
+ if m1 != m2 {
+ t.Errorf("Copied Regexp match failure on %s: original gave %t; copy gave %t; should be %t",
+ test, m1, m2, len(test.matches) > 0)
+ }
+}
+
+func TestCopyMatch(t *testing.T) {
+ for _, test := range findTests {
+ copyMatchTest(t, &test)
+ }
+}
+
type ReplaceTest struct {
pattern, replacement, input, output string
}
re.Match(x)
}
}
+
+func BenchmarkMatchParallelShared(b *testing.B) {
+ x := []byte("this is a long line that contains foo bar baz")
+ re := MustCompile("foo (ba+r)? baz")
+ b.ResetTimer()
+ b.RunParallel(func(pb *testing.PB) {
+ for pb.Next() {
+ re.Match(x)
+ }
+ })
+}
+
+func BenchmarkMatchParallelCopied(b *testing.B) {
+ x := []byte("this is a long line that contains foo bar baz")
+ re := MustCompile("foo (ba+r)? baz")
+ b.ResetTimer()
+ b.RunParallel(func(pb *testing.PB) {
+ re := re.Copy()
+ for pb.Next() {
+ re.Match(x)
+ }
+ })
+}
return re.expr
}
+// Copy returns a new Regexp object copied from re.
+//
+// When using a Regexp in multiple goroutines, giving each goroutine
+// its own copy helps to avoid lock contention.
+func (re *Regexp) Copy() *Regexp {
+ r := *re
+ r.mu = sync.Mutex{}
+ r.machine = nil
+ return &r
+}
+
// Compile parses a regular expression and returns, if successful,
// a Regexp object that can be used to match against text.
//