storage/benchfmt: reuse maps when identical
Change-Id: Ia38657da41f5ba5b68347f8e3e3dea112f8130ea
Reviewed-on: https://go-review.googlesource.com/35673
Reviewed-by: Russ Cox <rsc@golang.org>
diff --git a/storage/benchfmt/benchfmt.go b/storage/benchfmt/benchfmt.go
index 00e696a..37f33b7 100644
--- a/storage/benchfmt/benchfmt.go
+++ b/storage/benchfmt/benchfmt.go
@@ -34,6 +34,9 @@
// file or provided by AddLabels. They cannot be overridden.
permLabels Labels
lineNum int
+ // cached from last call to newResult, to save on allocations
+ lastName string
+ lastNameLabels Labels
// cached from the last call to Next
result *Result
err error
@@ -160,15 +163,19 @@
}
// newResult parses a line and returns a Result object for the line.
-func newResult(labels Labels, lineNum int, name, content string) *Result {
- r := &Result{
- Labels: labels,
- NameLabels: make(Labels),
- LineNum: lineNum,
- Content: content,
+func (r *Reader) newResult(labels Labels, lineNum int, name, content string) *Result {
+ res := &Result{
+ Labels: labels,
+ LineNum: lineNum,
+ Content: content,
}
- parseNameLabels(name, r.NameLabels)
- return r
+ if r.lastName != name {
+ r.lastName = name
+ r.lastNameLabels = make(Labels)
+ parseNameLabels(name, r.lastNameLabels)
+ }
+ res.NameLabels = r.lastNameLabels
+ return res
}
// copy returns a new copy of the labels map, to protect against
@@ -221,7 +228,7 @@
}
}
if fullName, ok := parseBenchmarkLine(line); ok {
- r.result = newResult(r.labels, r.lineNum, fullName, line)
+ r.result = r.newResult(r.labels, r.lineNum, fullName, line)
return true
}
}