aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Martí <mvdan@mvdan.cc>2023-01-08 23:07:43 +0000
committerDaniel Martí <mvdan@mvdan.cc>2023-01-20 09:27:47 +0000
commitf53137f95cfb1f33e592e2818fcb5e8d7ac80b66 (patch)
tree397e90d3eeddfb632edc7f33f4c62956c0e5b9f9 /src
parent1bef0707a537d19bbbfda09dde05e5874a2fb199 (diff)
downloadgo-f53137f95cfb1f33e592e2818fcb5e8d7ac80b66.tar.xz
internal/profile: use internal/lazyregexp for the legacy parser
Per benchinit, this makes a big difference to init times: name old time/op new time/op delta InternalProfile 185µs ± 1% 6µs ± 1% -96.51% (p=0.008 n=5+5) name old alloc/op new alloc/op delta InternalProfile 101kB ± 0% 4kB ± 0% -95.72% (p=0.008 n=5+5) name old allocs/op new allocs/op delta InternalProfile 758 ± 0% 25 ± 0% -96.70% (p=0.008 n=5+5) The fixed 0.2ms init cost is saved for any importer of net/http/pprof, but also for cmd/compile, as it supports PGO now. A Go program parsing profiles might not even need to compile these regular expressions at all, if it doesn't encounter any legacy files. I suspect this will be the case with most invocations of cmd/compile. Updates #26775. Change-Id: I8374dc64459f0b6bb09bbdf9d0b6c55d7ae1646e Reviewed-on: https://go-review.googlesource.com/c/go/+/460545 Reviewed-by: Michael Pratt <mpratt@google.com> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/go/build/deps_test.go2
-rw-r--r--src/internal/profile/legacy_profile.go26
-rw-r--r--src/internal/profile/profile.go4
3 files changed, 16 insertions, 16 deletions
diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go
index 08452c7b1d..23e0dee1d2 100644
--- a/src/go/build/deps_test.go
+++ b/src/go/build/deps_test.go
@@ -520,7 +520,7 @@ var depsRules = `
FMT, compress/gzip, encoding/binary, text/tabwriter
< runtime/pprof;
- OS, compress/gzip, regexp
+ OS, compress/gzip, internal/lazyregexp
< internal/profile;
html, internal/profile, net/http, runtime/pprof, runtime/trace
diff --git a/src/internal/profile/legacy_profile.go b/src/internal/profile/legacy_profile.go
index b102c95904..373a6c04ca 100644
--- a/src/internal/profile/legacy_profile.go
+++ b/src/internal/profile/legacy_profile.go
@@ -11,34 +11,34 @@ import (
"bufio"
"bytes"
"fmt"
+ "internal/lazyregexp"
"io"
"math"
- "regexp"
"strconv"
"strings"
)
var (
- countStartRE = regexp.MustCompile(`\A(\w+) profile: total \d+\n\z`)
- countRE = regexp.MustCompile(`\A(\d+) @(( 0x[0-9a-f]+)+)\n\z`)
+ countStartRE = lazyregexp.New(`\A(\w+) profile: total \d+\n\z`)
+ countRE = lazyregexp.New(`\A(\d+) @(( 0x[0-9a-f]+)+)\n\z`)
- heapHeaderRE = regexp.MustCompile(`heap profile: *(\d+): *(\d+) *\[ *(\d+): *(\d+) *\] *@ *(heap[_a-z0-9]*)/?(\d*)`)
- heapSampleRE = regexp.MustCompile(`(-?\d+): *(-?\d+) *\[ *(\d+): *(\d+) *] @([ x0-9a-f]*)`)
+ heapHeaderRE = lazyregexp.New(`heap profile: *(\d+): *(\d+) *\[ *(\d+): *(\d+) *\] *@ *(heap[_a-z0-9]*)/?(\d*)`)
+ heapSampleRE = lazyregexp.New(`(-?\d+): *(-?\d+) *\[ *(\d+): *(\d+) *] @([ x0-9a-f]*)`)
- contentionSampleRE = regexp.MustCompile(`(\d+) *(\d+) @([ x0-9a-f]*)`)
+ contentionSampleRE = lazyregexp.New(`(\d+) *(\d+) @([ x0-9a-f]*)`)
- hexNumberRE = regexp.MustCompile(`0x[0-9a-f]+`)
+ hexNumberRE = lazyregexp.New(`0x[0-9a-f]+`)
- growthHeaderRE = regexp.MustCompile(`heap profile: *(\d+): *(\d+) *\[ *(\d+): *(\d+) *\] @ growthz`)
+ growthHeaderRE = lazyregexp.New(`heap profile: *(\d+): *(\d+) *\[ *(\d+): *(\d+) *\] @ growthz`)
- fragmentationHeaderRE = regexp.MustCompile(`heap profile: *(\d+): *(\d+) *\[ *(\d+): *(\d+) *\] @ fragmentationz`)
+ fragmentationHeaderRE = lazyregexp.New(`heap profile: *(\d+): *(\d+) *\[ *(\d+): *(\d+) *\] @ fragmentationz`)
- threadzStartRE = regexp.MustCompile(`--- threadz \d+ ---`)
- threadStartRE = regexp.MustCompile(`--- Thread ([[:xdigit:]]+) \(name: (.*)/(\d+)\) stack: ---`)
+ threadzStartRE = lazyregexp.New(`--- threadz \d+ ---`)
+ threadStartRE = lazyregexp.New(`--- Thread ([[:xdigit:]]+) \(name: (.*)/(\d+)\) stack: ---`)
- procMapsRE = regexp.MustCompile(`([[:xdigit:]]+)-([[:xdigit:]]+)\s+([-rwxp]+)\s+([[:xdigit:]]+)\s+([[:xdigit:]]+):([[:xdigit:]]+)\s+([[:digit:]]+)\s*(\S+)?`)
+ procMapsRE = lazyregexp.New(`([[:xdigit:]]+)-([[:xdigit:]]+)\s+([-rwxp]+)\s+([[:xdigit:]]+)\s+([[:xdigit:]]+):([[:xdigit:]]+)\s+([[:digit:]]+)\s*(\S+)?`)
- briefMapsRE = regexp.MustCompile(`\s*([[:xdigit:]]+)-([[:xdigit:]]+):\s*(\S+)(\s.*@)?([[:xdigit:]]+)?`)
+ briefMapsRE = lazyregexp.New(`\s*([[:xdigit:]]+)-([[:xdigit:]]+):\s*(\S+)(\s.*@)?([[:xdigit:]]+)?`)
// LegacyHeapAllocated instructs the heapz parsers to use the
// allocated memory stats instead of the default in-use memory. Note
diff --git a/src/internal/profile/profile.go b/src/internal/profile/profile.go
index 29568aa4b5..c779bb2b11 100644
--- a/src/internal/profile/profile.go
+++ b/src/internal/profile/profile.go
@@ -11,8 +11,8 @@ import (
"bytes"
"compress/gzip"
"fmt"
+ "internal/lazyregexp"
"io"
- "regexp"
"strings"
"time"
)
@@ -192,7 +192,7 @@ func parseUncompressed(data []byte) (*Profile, error) {
return p, nil
}
-var libRx = regexp.MustCompile(`([.]so$|[.]so[._][0-9]+)`)
+var libRx = lazyregexp.New(`([.]so$|[.]so[._][0-9]+)`)
// setMain scans Mapping entries and guesses which entry is main
// because legacy profiles don't obey the convention of putting main