Skip to content

Commit

Permalink
cmd/compile: remove references to *os.File from ssa package
Browse files Browse the repository at this point in the history
DO NOT SUBMIT

This is a demo CL for golang#20084.
It currently reduces the size of the ssa export data
by 10%, from 76154 to 67886.

Change-Id: I49e8951c5bfce63ad2b7f4fc3bfa0868c53114f9
  • Loading branch information
josharian committed Apr 23, 2017
1 parent d40bb73 commit db74779
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/cmd/compile/internal/ssa/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import (
"cmd/internal/src"
"crypto/sha1"
"fmt"
"io"
"math"
"os"
"strings"
)

type writeSyncer interface {
io.Writer
Sync() error
}

// A Func represents a Go func declaration (or function literal) and its body.
// This package compiles each Func independently.
// Funcs are single-use; a new Func must be created for every compiled function.
Expand All @@ -30,7 +36,7 @@ type Func struct {

// Given an environment variable used for debug hash match,
// what file (if any) receives the yes/no logging?
logfiles map[string]*os.File
logfiles map[string]writeSyncer
HTMLWriter *HTMLWriter // html writer, for debugging
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

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

func (f *Func) logDebugHashMatch(evname, name string) {
if f.logfiles == nil {
f.logfiles = make(map[string]*os.File)
f.logfiles = make(map[string]writeSyncer)
}
file := f.logfiles[evname]
if file == nil {
Expand All @@ -604,8 +610,7 @@ func (f *Func) logDebugHashMatch(evname, name string) {
}
f.logfiles[evname] = file
}
s := fmt.Sprintf("%s triggered %s\n", evname, name)
file.WriteString(s)
fmt.Fprintf(file, "%s triggered %s\n", evname, name)
file.Sync()
}

Expand Down
18 changes: 9 additions & 9 deletions src/cmd/compile/internal/ssa/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import (

type HTMLWriter struct {
Logger
*os.File
w io.WriteCloser
}

func NewHTMLWriter(path string, logger Logger, funcname string) *HTMLWriter {
out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
logger.Fatalf(src.NoXPos, "%v", err)
}
html := HTMLWriter{File: out, Logger: logger}
html := HTMLWriter{w: out, Logger: logger}
html.start(funcname)
return &html
}
Expand Down Expand Up @@ -299,11 +299,11 @@ func (w *HTMLWriter) Close() {
if w == nil {
return
}
w.WriteString("</tr>")
w.WriteString("</table>")
w.WriteString("</body>")
w.WriteString("</html>")
w.File.Close()
io.WriteString(w.w, "</tr>")
io.WriteString(w.w, "</table>")
io.WriteString(w.w, "</body>")
io.WriteString(w.w, "</html>")
w.w.Close()
}

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

func (w *HTMLWriter) Printf(msg string, v ...interface{}) {
if _, err := fmt.Fprintf(w.File, msg, v...); err != nil {
if _, err := fmt.Fprintf(w.w, msg, v...); err != nil {
w.Fatalf(src.NoXPos, "%v", err)
}
}

func (w *HTMLWriter) WriteString(s string) {
if _, err := w.File.WriteString(s); err != nil {
if _, err := io.WriteString(w.w, s); err != nil {
w.Fatalf(src.NoXPos, "%v", err)
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/compile/internal/ssa/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ssa
import (
"cmd/internal/obj"
"fmt"
"io"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -561,7 +562,7 @@ func logRule(s string) {
}
}

var ruleFile *os.File
var ruleFile io.Writer

func min(x, y int64) int64 {
if x < y {
Expand Down

0 comments on commit db74779

Please sign in to comment.