]> Cypherpunks repositories - gostls13.git/commitdiff
testing: compile regexp only once
authorRob Pike <r@golang.org>
Wed, 16 Mar 2011 17:32:21 +0000 (10:32 -0700)
committerRob Pike <r@golang.org>
Wed, 16 Mar 2011 17:32:21 +0000 (10:32 -0700)
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

index 69eaae730e0e9f6cebe8fab83b8147079a63d151..a1a12281847dadb55648bca8d3661d8c8b4b80a2 100755 (executable)
@@ -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