From f40cb19affbb3be090b3519f957b5198744022be Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Mart=C3=AD?= Date: Wed, 27 Feb 2019 13:09:22 +0100 Subject: [PATCH] internal/lazyregexp: add a lazy Regexp package MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This was implemented as part of go/doc, but it's going to be useful in other packages. In particular, many packages under cmd/go like web and vcs make somewhat heavy use of global regexes, which add a non-trivial amount of init work to the cmd/go program. A lazy wrapper around regexp.Regexp will make it trivial to get rid of the extra cost with a trivial refactor, so make it possible for other packages in the repository to make use of it. While naming the package, give the members better names, such as lazyregexp.New and lazyregexp.Regexp. We're also considering adding some form of a lazy API to the public regexp package, so this internal package will allow us to get some initial experience across std and cmd. For #29382. Change-Id: I30b0e72871d5267c309786f95f4cb15c68b2393d Reviewed-on: https://go-review.googlesource.com/c/164040 Run-TryBot: Daniel Martí Reviewed-by: Brad Fitzpatrick Reviewed-by: Bryan C. Mills TryBot-Result: Gobot Gobot --- src/go/build/deps_test.go | 11 ++++++----- src/go/doc/comment.go | 5 +++-- src/go/doc/reader.go | 7 ++++--- src/{go/doc => internal/lazyregexp}/lazyre.go | 18 +++++++++--------- 4 files changed, 22 insertions(+), 19 deletions(-) rename src/{go/doc => internal/lazyregexp}/lazyre.go (66%) diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go index 2c29a3e601..9d6d038dab 100644 --- a/src/go/build/deps_test.go +++ b/src/go/build/deps_test.go @@ -192,10 +192,11 @@ var pkgDeps = map[string][]string{ "runtime/trace": {"L0", "context", "fmt"}, "text/tabwriter": {"L2"}, - "testing": {"L2", "flag", "fmt", "internal/race", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"}, - "testing/iotest": {"L2", "log"}, - "testing/quick": {"L2", "flag", "fmt", "reflect", "time"}, - "internal/testenv": {"L2", "OS", "flag", "testing", "syscall"}, + "testing": {"L2", "flag", "fmt", "internal/race", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"}, + "testing/iotest": {"L2", "log"}, + "testing/quick": {"L2", "flag", "fmt", "reflect", "time"}, + "internal/testenv": {"L2", "OS", "flag", "testing", "syscall"}, + "internal/lazyregexp": {"L2", "OS", "regexp"}, // L4 is defined as L3+fmt+log+time, because in general once // you're using L3 packages, use of fmt, log, or time is not a big deal. @@ -208,7 +209,7 @@ var pkgDeps = map[string][]string{ // Go parser. "go/ast": {"L4", "OS", "go/scanner", "go/token"}, - "go/doc": {"L4", "OS", "go/ast", "go/token", "regexp", "text/template"}, + "go/doc": {"L4", "OS", "go/ast", "go/token", "regexp", "internal/lazyregexp", "text/template"}, "go/parser": {"L4", "OS", "go/ast", "go/scanner", "go/token"}, "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"}, "go/scanner": {"L4", "OS", "go/token"}, diff --git a/src/go/doc/comment.go b/src/go/doc/comment.go index 73857330fa..31ee93e44f 100644 --- a/src/go/doc/comment.go +++ b/src/go/doc/comment.go @@ -8,6 +8,7 @@ package doc import ( "bytes" + "internal/lazyregexp" "io" "strings" "text/template" // for HTMLEscape @@ -69,7 +70,7 @@ const ( urlRx = protoPart + `://` + hostPart + pathPart ) -var matchRx = newLazyRE(`(` + urlRx + `)|(` + identRx + `)`) +var matchRx = lazyregexp.New(`(` + urlRx + `)|(` + identRx + `)`) var ( html_a = []byte(` 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test") -func newLazyRE(str string) *lazyRE { - lr := &lazyRE{str: str} +func New(str string) *Regexp { + lr := &Regexp{str: str} if inTest { // In tests, always compile the regexps early. lr.re() -- 2.50.0