-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile go/doc's 4 regexps lazily, on demand. Also, add a test for the one that had no test coverage. This reduces init-time CPU as well as heap by ~20KB when they're not used, which seems to be common enough. As an example, cmd/doc only seems to use 1 of them. (as noted by temporary print statements) Updates #26775 Change-Id: I85df89b836327a53fb8e1ace3f92480374270368 Reviewed-on: https://go-review.googlesource.com/127875 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
- Loading branch information
Showing
5 changed files
with
66 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package doc | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type lazyRE struct { | ||
str string | ||
once sync.Once | ||
rx *regexp.Regexp | ||
} | ||
|
||
func (r *lazyRE) re() *regexp.Regexp { | ||
r.once.Do(r.build) | ||
return r.rx | ||
} | ||
|
||
func (r *lazyRE) build() { | ||
r.rx = regexp.MustCompile(r.str) | ||
r.str = "" | ||
} | ||
|
||
func (r *lazyRE) FindStringSubmatchIndex(s string) []int { | ||
return r.re().FindStringSubmatchIndex(s) | ||
} | ||
|
||
func (r *lazyRE) ReplaceAllString(src, repl string) string { | ||
return r.re().ReplaceAllString(src, repl) | ||
} | ||
|
||
func (r *lazyRE) MatchString(s string) bool { | ||
return r.re().MatchString(s) | ||
} | ||
|
||
var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test") | ||
|
||
func newLazyRE(str string) *lazyRE { | ||
lr := &lazyRE{str: str} | ||
if inTest { | ||
// In tests, always compile the regexps early. | ||
lr.re() | ||
} | ||
return lr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters