Commit 4f13a9c5 authored by Ariel Mashraki's avatar Ariel Mashraki Committed by Emmanuel Odeke

text/template/parse: use strings.Builder in String methods

As mentioned in godoc, strings.Builder is more efficient for
concatenating and building strings.
Running a simple bench test on VariableNode.String() gives:

benchmark                     old ns/op     new ns/op     delta
BenchmarkParseLarge-8         25676831      24453285      -4.77%
BenchmarkVariableString-8     296           115           -61.15%

benchmark                     old allocs     new allocs     delta
BenchmarkVariableString-8     8              3              -62.50%

benchmark                     old bytes     new bytes     delta
BenchmarkVariableString-8     112           72            -35.71%

Change-Id: I13c9340080738fcad1edeed859d33ba608e4b05a
Reviewed-on: https://go-review.googlesource.com/c/go/+/198078Reviewed-by: default avatarEmmanuel Odeke <emm.odeke@gmail.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent c1635ad8
...@@ -160,23 +160,23 @@ func (p *PipeNode) append(command *CommandNode) { ...@@ -160,23 +160,23 @@ func (p *PipeNode) append(command *CommandNode) {
} }
func (p *PipeNode) String() string { func (p *PipeNode) String() string {
s := "" var sb strings.Builder
if len(p.Decl) > 0 { if len(p.Decl) > 0 {
for i, v := range p.Decl { for i, v := range p.Decl {
if i > 0 { if i > 0 {
s += ", " sb.WriteString(", ")
} }
s += v.String() sb.WriteString(v.String())
} }
s += " := " sb.WriteString(" := ")
} }
for i, c := range p.Cmds { for i, c := range p.Cmds {
if i > 0 { if i > 0 {
s += " | " sb.WriteString(" | ")
} }
s += c.String() sb.WriteString(c.String())
} }
return s return sb.String()
} }
func (p *PipeNode) tree() *Tree { func (p *PipeNode) tree() *Tree {
...@@ -249,18 +249,20 @@ func (c *CommandNode) append(arg Node) { ...@@ -249,18 +249,20 @@ func (c *CommandNode) append(arg Node) {
} }
func (c *CommandNode) String() string { func (c *CommandNode) String() string {
s := "" var sb strings.Builder
for i, arg := range c.Args { for i, arg := range c.Args {
if i > 0 { if i > 0 {
s += " " sb.WriteByte(' ')
} }
if arg, ok := arg.(*PipeNode); ok { if arg, ok := arg.(*PipeNode); ok {
s += "(" + arg.String() + ")" sb.WriteByte('(')
sb.WriteString(arg.String())
sb.WriteByte(')')
continue continue
} }
s += arg.String() sb.WriteString(arg.String())
} }
return s return sb.String()
} }
func (c *CommandNode) tree() *Tree { func (c *CommandNode) tree() *Tree {
...@@ -333,14 +335,14 @@ func (t *Tree) newVariable(pos Pos, ident string) *VariableNode { ...@@ -333,14 +335,14 @@ func (t *Tree) newVariable(pos Pos, ident string) *VariableNode {
} }
func (v *VariableNode) String() string { func (v *VariableNode) String() string {
s := "" var sb strings.Builder
for i, id := range v.Ident { for i, id := range v.Ident {
if i > 0 { if i > 0 {
s += "." sb.WriteByte('.')
} }
s += id sb.WriteString(id)
} }
return s return sb.String()
} }
func (v *VariableNode) tree() *Tree { func (v *VariableNode) tree() *Tree {
...@@ -426,11 +428,12 @@ func (t *Tree) newField(pos Pos, ident string) *FieldNode { ...@@ -426,11 +428,12 @@ func (t *Tree) newField(pos Pos, ident string) *FieldNode {
} }
func (f *FieldNode) String() string { func (f *FieldNode) String() string {
s := "" var sb strings.Builder
for _, id := range f.Ident { for _, id := range f.Ident {
s += "." + id sb.WriteByte('.')
sb.WriteString(id)
} }
return s return sb.String()
} }
func (f *FieldNode) tree() *Tree { func (f *FieldNode) tree() *Tree {
......
...@@ -553,3 +553,19 @@ func BenchmarkParseLarge(b *testing.B) { ...@@ -553,3 +553,19 @@ func BenchmarkParseLarge(b *testing.B) {
} }
} }
} }
var sink string
func BenchmarkVariableString(b *testing.B) {
v := &VariableNode{
Ident: []string{"$", "A", "BB", "CCC", "THIS_IS_THE_VARIABLE_BEING_PROCESSED"},
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
sink = v.String()
}
if sink == "" {
b.Fatal("Benchmark was not run")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment