Optimize ninjaString.ValueWithEscaper

ninjaString.ValueWithEscaper is a relatively hot function,
rewrite it with strings.Builder to avoid repeated string
concatenation, which requires an allocation each time.

Before:
BenchmarkNinjaString_Value/constant/1-72       	100000000	        11.9 ns/op
BenchmarkNinjaString_Value/constant/10-72      	100000000	        18.9 ns/op
BenchmarkNinjaString_Value/constant/100-72     	50000000	        22.1 ns/op
BenchmarkNinjaString_Value/constant/1000-72    	30000000	        39.3 ns/op
BenchmarkNinjaString_Value/variable/1-72       	20000000	        95.1 ns/op
BenchmarkNinjaString_Value/variable/10-72      	10000000	       223 ns/op
BenchmarkNinjaString_Value/variable/100-72     	 3000000	       437 ns/op
BenchmarkNinjaString_Value/variable/1000-72    	 2000000	       948 ns/op
BenchmarkNinjaString_Value/variables/1-72      	10000000	       161 ns/op
BenchmarkNinjaString_Value/variables/2-72      	 5000000	       368 ns/op
BenchmarkNinjaString_Value/variables/3-72      	 3000000	       560 ns/op
BenchmarkNinjaString_Value/variables/4-72      	 2000000	       795 ns/op
BenchmarkNinjaString_Value/variables/5-72      	 1000000	      1004 ns/op
BenchmarkNinjaString_Value/variables/10-72     	 1000000	      2275 ns/op
BenchmarkNinjaString_Value/variables/100-72    	   50000	     39667 ns/op
BenchmarkNinjaString_Value/variables/1000-72   	    1000	   2146592 ns/op

After:
BenchmarkNinjaString_Value/constant/1-72       	200000000	        11.3 ns/op
BenchmarkNinjaString_Value/constant/10-72      	100000000	        17.2 ns/op
BenchmarkNinjaString_Value/constant/100-72     	50000000	        21.7 ns/op
BenchmarkNinjaString_Value/constant/1000-72    	30000000	        38.3 ns/op
BenchmarkNinjaString_Value/variable/1-72       	20000000	        91.8 ns/op
BenchmarkNinjaString_Value/variable/10-72      	10000000	       199 ns/op
BenchmarkNinjaString_Value/variable/100-72     	 5000000	       377 ns/op
BenchmarkNinjaString_Value/variable/1000-72    	 2000000	       855 ns/op
BenchmarkNinjaString_Value/variables/1-72      	10000000	       141 ns/op
BenchmarkNinjaString_Value/variables/2-72      	 5000000	       312 ns/op
BenchmarkNinjaString_Value/variables/3-72      	 5000000	       362 ns/op
BenchmarkNinjaString_Value/variables/4-72      	 3000000	       417 ns/op
BenchmarkNinjaString_Value/variables/5-72      	 2000000	       621 ns/op
BenchmarkNinjaString_Value/variables/10-72     	 2000000	       837 ns/op
BenchmarkNinjaString_Value/variables/100-72    	  200000	      9141 ns/op
BenchmarkNinjaString_Value/variables/1000-72   	   20000	     95094 ns/op

Test: ninja_strings_test.go
Change-Id: I6c61e747d8e67f7f1e6cff0cc0c705745301a35f
This commit is contained in:
Colin Cross 2019-06-19 23:25:39 -07:00
parent 4f6631f74f
commit 19ff727ad5
2 changed files with 57 additions and 5 deletions

View file

@ -277,12 +277,20 @@ func (n *ninjaString) Value(pkgNames map[*packageContext]string) string {
func (n *ninjaString) ValueWithEscaper(pkgNames map[*packageContext]string,
escaper *strings.Replacer) string {
str := escaper.Replace(n.strings[0])
for i, v := range n.variables {
str += "${" + v.fullName(pkgNames) + "}"
str += escaper.Replace(n.strings[i+1])
if len(n.strings) == 1 {
return escaper.Replace(n.strings[0])
}
return str
str := strings.Builder{}
str.WriteString(escaper.Replace(n.strings[0]))
for i, v := range n.variables {
str.WriteString("${")
str.WriteString(v.fullName(pkgNames))
str.WriteString("}")
str.WriteString(escaper.Replace(n.strings[i+1]))
}
return str.String()
}
func (n *ninjaString) Eval(variables map[Variable]*ninjaString) (string, error) {

View file

@ -16,6 +16,8 @@ package blueprint
import (
"reflect"
"strconv"
"strings"
"testing"
)
@ -161,3 +163,45 @@ func TestParseNinjaStringWithImportedVar(t *testing.T) {
t.Errorf(" got: %#v", output)
}
}
func BenchmarkNinjaString_Value(b *testing.B) {
b.Run("constant", func(b *testing.B) {
for _, l := range []int{1, 10, 100, 1000} {
ns := simpleNinjaString(strings.Repeat("a", l))
b.Run(strconv.Itoa(l), func(b *testing.B) {
for n := 0; n < b.N; n++ {
ns.Value(nil)
}
})
}
})
b.Run("variable", func(b *testing.B) {
for _, l := range []int{1, 10, 100, 1000} {
scope := newLocalScope(nil, "")
scope.AddLocalVariable("a", strings.Repeat("b", l/3))
ns, _ := parseNinjaString(scope, strings.Repeat("a", l/3)+"${a}"+strings.Repeat("a", l/3))
b.Run(strconv.Itoa(l), func(b *testing.B) {
for n := 0; n < b.N; n++ {
ns.Value(nil)
}
})
}
})
b.Run("variables", func(b *testing.B) {
for _, l := range []int{1, 2, 3, 4, 5, 10, 100, 1000} {
scope := newLocalScope(nil, "")
str := strings.Repeat("a", 10)
for i := 0; i < l; i++ {
scope.AddLocalVariable("a"+strconv.Itoa(i), strings.Repeat("b", 10))
str += "${a" + strconv.Itoa(i) + "}"
}
ns, _ := parseNinjaString(scope, str)
b.Run(strconv.Itoa(l), func(b *testing.B) {
for n := 0; n < b.N; n++ {
ns.Value(nil)
}
})
}
})
}