From b8e4fbb79c03259545c78974f3422e35497137bb Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 16 Mar 2011 10:32:21 -0700 Subject: [PATCH] testing: compile regexp only once The -test.run and -test.bench flags were compilng the regexp for ever test function, which was mucking up memory profiles. Add a simple wrapper to save the compiled state so that the regexp is compiled only once for each flag. R=rsc CC=golang-dev https://golang.org/cl/4274063 --- src/cmd/gotest/gotest | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/cmd/gotest/gotest b/src/cmd/gotest/gotest index 69eaae730e..a1a1228184 100755 --- a/src/cmd/gotest/gotest +++ b/src/cmd/gotest/gotest @@ -164,6 +164,7 @@ importpath=$(gomake -s importpath) echo 'import "./_xtest_"' fi echo 'import "testing"' + echo 'import __os__ "os"' # rename in case tested package is called os echo 'import __regexp__ "regexp"' # rename in case tested package is called regexp # test array echo @@ -185,11 +186,26 @@ importpath=$(gomake -s importpath) done echo '}' # body - echo - echo 'func main() {' - echo ' testing.Main(__regexp__.MatchString, tests)' - echo ' testing.RunBenchmarks(__regexp__.MatchString, benchmarks)' - echo '}' + echo \ +' +var matchPat string +var matchRe *__regexp__.Regexp + +func matchString(pat, str string) (result bool, err __os__.Error) { + if matchRe == nil || matchPat != pat { + matchPat = pat + matchRe, err = __regexp__.Compile(matchPat) + if err != nil { + return + } + } + return matchRe.MatchString(str), nil +} + +func main() { + testing.Main(matchString, tests) + testing.RunBenchmarks(matchString, benchmarks) +}' }>_testmain.go $GC _testmain.go -- 2.48.1