Skip to content

Commit 61149f2

Browse files
authored
Merge pull request github#11232 from grddev/patch-1
Go: Optimize trap.Writer by buffering gzip writes
2 parents 1e6ef99 + 3514694 commit 61149f2

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

go/extractor/trap/labels.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (l *Labeler) GlobalID(key string) Label {
5757
label, exists := l.keyLabels[key]
5858
if !exists {
5959
id := l.nextID()
60-
fmt.Fprintf(l.tw.zip, "%s=@\"%s\"\n", id, escapeString(key))
60+
fmt.Fprintf(l.tw.wzip, "%s=@\"%s\"\n", id, escapeString(key))
6161
label = Label{id}
6262
l.keyLabels[key] = label
6363
}
@@ -90,7 +90,7 @@ func (l *Labeler) LocalID(nd interface{}) Label {
9090
// FreshID creates a fresh label and returns it
9191
func (l *Labeler) FreshID() Label {
9292
id := l.nextID()
93-
fmt.Fprintf(l.tw.zip, "%s=*\n", id)
93+
fmt.Fprintf(l.tw.wzip, "%s=*\n", id)
9494
return Label{id}
9595
}
9696

go/extractor/trap/trapwriter.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
// A Writer provides methods for writing data to a TRAP file
2020
type Writer struct {
2121
zip *gzip.Writer
22-
w *bufio.Writer
22+
wzip *bufio.Writer
23+
wfile *bufio.Writer
2324
file *os.File
2425
Labeler *Labeler
2526
path string
@@ -54,11 +55,13 @@ func NewWriter(path string, pkg *packages.Package) (*Writer, error) {
5455
if err != nil {
5556
return nil, err
5657
}
57-
bufioWriter := bufio.NewWriter(tmpFile)
58-
zipWriter := gzip.NewWriter(bufioWriter)
58+
bufioFileWriter := bufio.NewWriter(tmpFile)
59+
zipWriter := gzip.NewWriter(bufioFileWriter)
60+
bufioZipWriter := bufio.NewWriter(zipWriter)
5961
tw := &Writer{
6062
zipWriter,
61-
bufioWriter,
63+
bufioZipWriter,
64+
bufioFileWriter,
6265
tmpFile,
6366
nil,
6467
path,
@@ -88,13 +91,19 @@ func trapFolder() (string, error) {
8891

8992
// Close the underlying file writer
9093
func (tw *Writer) Close() error {
91-
err := tw.zip.Close()
94+
err := tw.wzip.Flush()
95+
if err != nil {
96+
// throw away file close error
97+
tw.file.Close()
98+
return err
99+
}
100+
err = tw.zip.Close()
92101
if err != nil {
93102
// return zip-close error, but ignore file-close error
94103
tw.file.Close()
95104
return err
96105
}
97-
err = tw.w.Flush()
106+
err = tw.wfile.Flush()
98107
if err != nil {
99108
// throw away close error because write errors are likely to be more important
100109
tw.file.Close()
@@ -145,24 +154,24 @@ func capStringLength(s string) string {
145154

146155
// Emit writes out a tuple of values for the given `table`
147156
func (tw *Writer) Emit(table string, values []interface{}) error {
148-
fmt.Fprintf(tw.zip, "%s(", table)
157+
fmt.Fprintf(tw.wzip, "%s(", table)
149158
for i, value := range values {
150159
if i > 0 {
151-
fmt.Fprint(tw.zip, ", ")
160+
fmt.Fprint(tw.wzip, ", ")
152161
}
153162
switch value := value.(type) {
154163
case Label:
155-
fmt.Fprint(tw.zip, value.id)
164+
fmt.Fprint(tw.wzip, value.id)
156165
case string:
157-
fmt.Fprintf(tw.zip, "\"%s\"", escapeString(capStringLength(value)))
166+
fmt.Fprintf(tw.wzip, "\"%s\"", escapeString(capStringLength(value)))
158167
case int:
159-
fmt.Fprintf(tw.zip, "%d", value)
168+
fmt.Fprintf(tw.wzip, "%d", value)
160169
case float64:
161-
fmt.Fprintf(tw.zip, "%e", value)
170+
fmt.Fprintf(tw.wzip, "%e", value)
162171
default:
163172
return errors.New("Cannot emit value")
164173
}
165174
}
166-
fmt.Fprintf(tw.zip, ")\n")
175+
fmt.Fprintf(tw.wzip, ")\n")
167176
return nil
168177
}

0 commit comments

Comments
 (0)