aboutsummaryrefslogtreecommitdiff
path: root/src/text
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na92@gmail.com>2019-05-06 18:53:46 +0900
committerEmmanuel Odeke <emm.odeke@gmail.com>2019-08-27 16:54:06 +0000
commit997086b7e6db6c74926500aa15fd3c02bc4939b8 (patch)
tree37aa41a586356f8b137f9933e2c5ed02ac29b97a /src/text
parentfc4663d56f2ac550c0deca6ed0261d894be49465 (diff)
downloadgo-997086b7e6db6c74926500aa15fd3c02bc4939b8.tar.gz
go-997086b7e6db6c74926500aa15fd3c02bc4939b8.zip
text/template: replace bytes.Buffer with strings.Builder
After Go.1.10+ strings.Builder is known as more efficient in concatenating and building strings than bytes.Buffer. In this CL, there is a minor logic fix for getting advantage of strings.builder. name old time/op new time/op delta DefinedTemplate-8 543ns ± 3% 512ns ± 2% -5.73% (p=0.000 n=8+8) name old alloc/op new alloc/op delta DefinedTemplate-8 192B ± 0% 160B ± 0% -16.67% (p=0.000 n=8+8) name old allocs/op new allocs/op delta DefinedTemplate-8 5.00 ± 0% 5.00 ± 0% ~ (all equal) Change-Id: Icda0054d146e6c5e32ed8a4d13221bb6850d31b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/175261 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/text')
-rw-r--r--src/text/template/exec.go13
1 files changed, 5 insertions, 8 deletions
diff --git a/src/text/template/exec.go b/src/text/template/exec.go
index 4db63bfa09..97c9d1f574 100644
--- a/src/text/template/exec.go
+++ b/src/text/template/exec.go
@@ -5,7 +5,6 @@
package template
import (
- "bytes"
"fmt"
"internal/fmtsort"
"io"
@@ -230,21 +229,19 @@ func (t *Template) DefinedTemplates() string {
if t.common == nil {
return ""
}
- var b bytes.Buffer
+ var b strings.Builder
for name, tmpl := range t.tmpl {
if tmpl.Tree == nil || tmpl.Root == nil {
continue
}
- if b.Len() > 0 {
+ if b.Len() == 0 {
+ b.WriteString("; defined templates are: ")
+ } else {
b.WriteString(", ")
}
fmt.Fprintf(&b, "%q", name)
}
- var s string
- if b.Len() > 0 {
- s = "; defined templates are: " + b.String()
- }
- return s
+ return b.String()
}
// Walk functions step through the major pieces of the template structure,