Skip to content

Commit 0cd3ecb

Browse files
committed
cmd/compile: reduce allocs some more
Also: update fmt_test.go. Together with the previous commits, we are now at or below c85b77c levels in terms of allocation for the benchmark described in #16897 (old = c85b77c, new = this commit): name old time/op new time/op delta Template 297ms ± 5% 284ms ± 3% -4.53% (p=0.000 n=27+29) Unicode 159ms ± 5% 151ms ± 5% -4.91% (p=0.000 n=28+30) GoTypes 985ms ± 5% 935ms ± 2% -5.13% (p=0.000 n=28+29) name old alloc/op new alloc/op delta Template 46.8MB ± 0% 45.7MB ± 0% -2.37% (p=0.000 n=30+30) Unicode 37.8MB ± 0% 37.9MB ± 0% +0.29% (p=0.000 n=29+30) GoTypes 143MB ± 0% 138MB ± 0% -3.64% (p=0.000 n=29+30) name old allocs/op new allocs/op delta Template 444k ± 0% 440k ± 0% -0.94% (p=0.000 n=30+30) Unicode 369k ± 0% 369k ± 0% +0.19% (p=0.000 n=29+30) GoTypes 1.35M ± 0% 1.34M ± 0% -1.24% (p=0.000 n=30+30) For #16897. Change-Id: Iedbeb408e2f1e68dd4a3201bf8813c8066ebf7ed Reviewed-on: https://go-review.googlesource.com/29089 Reviewed-by: Matthew Dempsky <[email protected]>
1 parent b6946fb commit 0cd3ecb

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

src/cmd/compile/fmt_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -552,19 +552,15 @@ var knownFormats = map[string]string{
552552
"*cmd/compile/internal/gc.Node %j": "",
553553
"*cmd/compile/internal/gc.Node %p": "",
554554
"*cmd/compile/internal/gc.Node %v": "",
555-
"*cmd/compile/internal/gc.Sym % v": "",
556555
"*cmd/compile/internal/gc.Sym %+v": "",
557556
"*cmd/compile/internal/gc.Sym %-v": "",
558557
"*cmd/compile/internal/gc.Sym %0S": "",
559558
"*cmd/compile/internal/gc.Sym %S": "",
560559
"*cmd/compile/internal/gc.Sym %p": "",
561560
"*cmd/compile/internal/gc.Sym %v": "",
562-
"*cmd/compile/internal/gc.Type % -v": "",
563561
"*cmd/compile/internal/gc.Type %#v": "",
564562
"*cmd/compile/internal/gc.Type %+v": "",
565-
"*cmd/compile/internal/gc.Type %- v": "",
566563
"*cmd/compile/internal/gc.Type %-S": "",
567-
"*cmd/compile/internal/gc.Type %-v": "",
568564
"*cmd/compile/internal/gc.Type %0S": "",
569565
"*cmd/compile/internal/gc.Type %L": "",
570566
"*cmd/compile/internal/gc.Type %S": "",

src/cmd/compile/internal/gc/fmt.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
// See the respective function's documentation for details.
1818
type FmtFlag int
1919

20+
// TODO(gri) The ' ' flag is not used anymore in %-formats.
21+
// Eliminate eventually.
22+
2023
const ( // fmt.Format flag/prec or verb
2124
FmtLeft FmtFlag = 1 << iota // '-'
2225
FmtSharp // '#'
@@ -1556,6 +1559,7 @@ func (n *Node) nodedump(s fmt.State, flag FmtFlag) {
15561559
}
15571560
}
15581561

1562+
// "%S" suppresses qualifying with package
15591563
func (s *Sym) Format(f fmt.State, verb rune) {
15601564
switch verb {
15611565
case 'v', 'S':
@@ -1570,7 +1574,7 @@ func (s *Sym) String() string {
15701574
return s.sconv(0)
15711575
}
15721576

1573-
// "%S" suppresses qualifying with package
1577+
// See #16897 before changing the implementation of sconv.
15741578
func (s *Sym) sconv(flag FmtFlag) string {
15751579
if flag&FmtLong != 0 {
15761580
panic("linksymfmt")
@@ -1671,6 +1675,9 @@ func Fldconv(f *Field, flag FmtFlag) string {
16711675
return str
16721676
}
16731677

1678+
// "%L" print definition, not name
1679+
// "%S" omit 'func' and receiver from function types, short type names
1680+
// "% v" package name, not prefix (FTypeId mode, sticky)
16741681
func (t *Type) Format(s fmt.State, verb rune) {
16751682
switch verb {
16761683
case 'v', 'S', 'L':
@@ -1681,9 +1688,7 @@ func (t *Type) Format(s fmt.State, verb rune) {
16811688
}
16821689
}
16831690

1684-
// "%L" print definition, not name
1685-
// "%S" omit 'func' and receiver from function types, short type names
1686-
// "% v" package name, not prefix (FTypeId mode, sticky)
1691+
// See #16897 before changing the implementation of tconv.
16871692
func (t *Type) tconv(flag FmtFlag) string {
16881693
if t == nil {
16891694
return "<T>"

src/cmd/compile/internal/gc/subr.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1147,10 +1147,11 @@ func syslook(name string) *Node {
11471147
// typehash computes a hash value for type t to use in type switch
11481148
// statements.
11491149
func typehash(t *Type) uint32 {
1150-
// fmt.Sprintf("%- v", t) already contains all the necessary logic to generate
1151-
// a representation that completely describes the type, so using
1150+
// t.tconv(FmtLeft | FmtUnsigned) already contains all the necessary logic
1151+
// to generate a representation that completely describes the type, so using
11521152
// it here avoids duplicating that code.
1153-
p := fmt.Sprintf("%- v", t)
1153+
// See the comments in exprSwitch.checkDupCases.
1154+
p := t.tconv(FmtLeft | FmtUnsigned)
11541155

11551156
// Using MD5 is overkill, but reduces accidental collisions.
11561157
h := md5.Sum([]byte(p))

src/cmd/compile/internal/gc/swt.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
package gc
66

7-
import (
8-
"fmt"
9-
"sort"
10-
)
7+
import "sort"
118

129
const (
1310
// expression switch
@@ -647,9 +644,9 @@ func (s *exprSwitch) checkDupCases(cc []caseClause) {
647644
}
648645
n := c.node.Left
649646
tv := typeVal{
650-
// fmt.Sprintf("% -v", n.Type) here serves to completely describe the type.
647+
// n.Type.tconv(FmtLeft | FmtUnsigned) here serves to completely describe the type.
651648
// See the comments in func typehash.
652-
typ: fmt.Sprintf("% -v", n.Type),
649+
typ: n.Type.tconv(FmtLeft | FmtUnsigned),
653650
val: n.Val().Interface(),
654651
}
655652
prev, dup := seen[tv]

0 commit comments

Comments
 (0)