• Caleb Spare's avatar
    regexp: add Copy method to Regexp · 937172b2
    Caleb Spare authored
    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/16110Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
    937172b2
Name
Last commit
Last update
..
archive Loading commit data...
bufio Loading commit data...
builtin Loading commit data...
bytes Loading commit data...
cmd Loading commit data...
compress Loading commit data...
container Loading commit data...
crypto Loading commit data...
database/sql Loading commit data...
debug Loading commit data...
encoding Loading commit data...
errors Loading commit data...
expvar Loading commit data...
flag Loading commit data...
fmt Loading commit data...
go Loading commit data...
hash Loading commit data...
html Loading commit data...
image Loading commit data...
index/suffixarray Loading commit data...
internal Loading commit data...
io Loading commit data...
log Loading commit data...
math Loading commit data...
mime Loading commit data...
net Loading commit data...
os Loading commit data...
path Loading commit data...
reflect Loading commit data...
regexp Loading commit data...
runtime Loading commit data...
sort Loading commit data...
strconv Loading commit data...
strings Loading commit data...
sync Loading commit data...
syscall Loading commit data...
testing Loading commit data...
text Loading commit data...
time Loading commit data...
unicode Loading commit data...
unsafe Loading commit data...
vendor/golang.org/x/net/http2/hpack Loading commit data...
Make.dist Loading commit data...
all.bash Loading commit data...
all.bat Loading commit data...
all.rc Loading commit data...
androidtest.bash Loading commit data...
bootstrap.bash Loading commit data...
buildall.bash Loading commit data...
clean.bash Loading commit data...
clean.bat Loading commit data...
clean.rc Loading commit data...
iostest.bash Loading commit data...
make.bash Loading commit data...
make.bat Loading commit data...
make.rc Loading commit data...
nacltest.bash Loading commit data...
race.bash Loading commit data...
race.bat Loading commit data...
run.bash Loading commit data...
run.bat Loading commit data...
run.rc Loading commit data...