Skip to content

Commit 4ee934a

Browse files
committed
cmd/compile: remove references to *os.File from ssa package
This reduces the size of the ssa export data by 10%, from 76154 to 67886. It doesn't appear that #20084, which would do this automatically, is going to be fixed soon. Do it manually for now. This speeds up compiling cmd/compile/internal/amd64 and presumably its comrades as well: name old time/op new time/op delta CompileAMD64 89.6ms ± 6% 86.7ms ± 5% -3.29% (p=0.000 n=49+47) name old user-time/op new user-time/op delta CompileAMD64 116ms ± 5% 112ms ± 5% -3.51% (p=0.000 n=45+42) name old alloc/op new alloc/op delta CompileAMD64 26.7MB ± 0% 25.8MB ± 0% -3.26% (p=0.008 n=5+5) name old allocs/op new allocs/op delta CompileAMD64 223k ± 0% 213k ± 0% -4.46% (p=0.008 n=5+5) Updates #20084 Change-Id: I49e8951c5bfce63ad2b7f4fc3bfa0868c53114f9 Reviewed-on: https://go-review.googlesource.com/41493 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent cdeda79 commit 4ee934a

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/cmd/compile/internal/ssa/func.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ import (
88
"cmd/internal/src"
99
"crypto/sha1"
1010
"fmt"
11+
"io"
1112
"math"
1213
"os"
1314
"strings"
1415
)
1516

17+
type writeSyncer interface {
18+
io.Writer
19+
Sync() error
20+
}
21+
1622
// A Func represents a Go func declaration (or function literal) and its body.
1723
// This package compiles each Func independently.
1824
// Funcs are single-use; a new Func must be created for every compiled function.
@@ -30,7 +36,7 @@ type Func struct {
3036

3137
// Given an environment variable used for debug hash match,
3238
// what file (if any) receives the yes/no logging?
33-
logfiles map[string]*os.File
39+
logfiles map[string]writeSyncer
3440
HTMLWriter *HTMLWriter // html writer, for debugging
3541
DebugTest bool // default true unless $GOSSAHASH != ""; as a debugging aid, make new code conditional on this and use GOSSAHASH to binary search for failing cases
3642

@@ -590,7 +596,7 @@ func (f *Func) DebugHashMatch(evname, name string) bool {
590596

591597
func (f *Func) logDebugHashMatch(evname, name string) {
592598
if f.logfiles == nil {
593-
f.logfiles = make(map[string]*os.File)
599+
f.logfiles = make(map[string]writeSyncer)
594600
}
595601
file := f.logfiles[evname]
596602
if file == nil {
@@ -604,8 +610,7 @@ func (f *Func) logDebugHashMatch(evname, name string) {
604610
}
605611
f.logfiles[evname] = file
606612
}
607-
s := fmt.Sprintf("%s triggered %s\n", evname, name)
608-
file.WriteString(s)
613+
fmt.Fprintf(file, "%s triggered %s\n", evname, name)
609614
file.Sync()
610615
}
611616

src/cmd/compile/internal/ssa/html.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import (
1515

1616
type HTMLWriter struct {
1717
Logger
18-
*os.File
18+
w io.WriteCloser
1919
}
2020

2121
func NewHTMLWriter(path string, logger Logger, funcname string) *HTMLWriter {
2222
out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
2323
if err != nil {
2424
logger.Fatalf(src.NoXPos, "%v", err)
2525
}
26-
html := HTMLWriter{File: out, Logger: logger}
26+
html := HTMLWriter{w: out, Logger: logger}
2727
html.start(funcname)
2828
return &html
2929
}
@@ -299,11 +299,11 @@ func (w *HTMLWriter) Close() {
299299
if w == nil {
300300
return
301301
}
302-
w.WriteString("</tr>")
303-
w.WriteString("</table>")
304-
w.WriteString("</body>")
305-
w.WriteString("</html>")
306-
w.File.Close()
302+
io.WriteString(w.w, "</tr>")
303+
io.WriteString(w.w, "</table>")
304+
io.WriteString(w.w, "</body>")
305+
io.WriteString(w.w, "</html>")
306+
w.w.Close()
307307
}
308308

309309
// WriteFunc writes f in a column headed by title.
@@ -328,13 +328,13 @@ func (w *HTMLWriter) WriteColumn(title string, html string) {
328328
}
329329

330330
func (w *HTMLWriter) Printf(msg string, v ...interface{}) {
331-
if _, err := fmt.Fprintf(w.File, msg, v...); err != nil {
331+
if _, err := fmt.Fprintf(w.w, msg, v...); err != nil {
332332
w.Fatalf(src.NoXPos, "%v", err)
333333
}
334334
}
335335

336336
func (w *HTMLWriter) WriteString(s string) {
337-
if _, err := w.File.WriteString(s); err != nil {
337+
if _, err := io.WriteString(w.w, s); err != nil {
338338
w.Fatalf(src.NoXPos, "%v", err)
339339
}
340340
}

src/cmd/compile/internal/ssa/rewrite.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package ssa
77
import (
88
"cmd/internal/obj"
99
"fmt"
10+
"io"
1011
"math"
1112
"os"
1213
"path/filepath"
@@ -561,7 +562,7 @@ func logRule(s string) {
561562
}
562563
}
563564

564-
var ruleFile *os.File
565+
var ruleFile io.Writer
565566

566567
func min(x, y int64) int64 {
567568
if x < y {

0 commit comments

Comments
 (0)