Reuse AST storage between top-level expressions
``` Comparing: old: v2-wip/1da2fc7 (2021-03-25 20:38:05 -0400 -0400) run: v2-wip/3f23ab9 (2021-03-25 22:35:06 -0400 -0400) ----------------------------------------------------------- name old time/op new time/op delta UnmarshalSimple/v2-8 700ns ± 3% 705ns ± 2% ~ (p=0.690 n=5+5) UnmarshalSimple/v1-8 3.85µs ± 1% 4.02µs ± 4% +4.19% (p=0.032 n=5+5) UnmarshalSimple/bs-8 2.34µs ± 2% 2.38µs ± 3% ~ (p=0.310 n=5+5) ReferenceFile/v2-8 32.2µs ±13% 23.9µs ± 1% -25.79% (p=0.008 n=5+5) ReferenceFile/v1-8 270µs ± 2% 264µs ± 2% ~ (p=0.095 n=5+5) ReferenceFile/bs-8 291µs ± 0% 294µs ± 0% +0.88% (p=0.008 n=5+5) name old alloc/op new alloc/op delta ReferenceFile/v2-8 37.1kB ± 0% 6.7kB ± 0% -81.91% (p=0.008 n=5+5) ReferenceFile/v1-8 131kB ± 0% 131kB ± 0% ~ (p=0.444 n=5+5) ReferenceFile/bs-8 80.8kB ± 0% 80.8kB ± 0% ~ (p=0.571 n=5+5) name old allocs/op new allocs/op delta ReferenceFile/v2-8 152 ± 0% 148 ± 0% -2.63% (p=0.008 n=5+5) ReferenceFile/v1-8 2.65k ± 0% 2.65k ± 0% ~ (all equal) ReferenceFile/bs-8 1.73k ± 0% 1.73k ± 0% ~ (all equal) ~/s/g/p/g/benchmark$ go test -bench=. goos: linux goarch: amd64 pkg: github.com/pelletier/go-toml/v2/benchmark cpu: Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz BenchmarkUnmarshalSimple/v2-8 1692444 710.7 ns/op BenchmarkUnmarshalSimple/v1-8 307609 3862 ns/op BenchmarkUnmarshalSimple/bs-8 520429 2285 ns/op BenchmarkReferenceFile/v2-8 50395 24006 ns/op 6704 B/op 148 allocs/op BenchmarkReferenceFile/v1-8 4144 264655 ns/op 130567 B/op 2649 allocs/op BenchmarkReferenceFile/bs-8 3969 293635 ns/op 80784 B/op 1729 allocs/op PASS ok github.com/pelletier/go-toml/v2/benchmark 8.143s ```
This commit is contained in:
+105
-110
@@ -119,23 +119,22 @@ func TestParser_AST_Numbers(t *testing.T) {
|
||||
for _, e := range examples {
|
||||
t.Run(e.desc, func(t *testing.T) {
|
||||
p := parser{}
|
||||
err := p.parse([]byte(`A = ` + e.input))
|
||||
p.Reset([]byte(`A = ` + e.input))
|
||||
p.NextExpression()
|
||||
err := p.Error()
|
||||
if e.err {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
expected := astRoot{
|
||||
astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{Kind: e.kind, Data: []byte(e.input)},
|
||||
{Kind: ast.Key, Data: []byte(`A`)},
|
||||
},
|
||||
expected := astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{Kind: e.kind, Data: []byte(e.input)},
|
||||
{Kind: ast.Key, Data: []byte(`A`)},
|
||||
},
|
||||
}
|
||||
|
||||
compareAST(t, expected, p.builder.Finish())
|
||||
compareNode(t, expected, p.Expression())
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -153,6 +152,13 @@ func compareAST(t *testing.T, expected astRoot, actual *ast.Root) {
|
||||
compareIterator(t, expected, it)
|
||||
}
|
||||
|
||||
func compareNode(t *testing.T, e astNode, n ast.Node) {
|
||||
require.Equal(t, e.Kind, n.Kind)
|
||||
require.Equal(t, e.Data, n.Data)
|
||||
|
||||
compareIterator(t, e.Children, n.Children())
|
||||
}
|
||||
|
||||
func compareIterator(t *testing.T, expected []astNode, actual ast.Iterator) {
|
||||
idx := 0
|
||||
|
||||
@@ -164,10 +170,7 @@ func compareIterator(t *testing.T, expected []astNode, actual ast.Iterator) {
|
||||
}
|
||||
e := expected[idx]
|
||||
|
||||
require.Equal(t, e.Kind, n.Kind)
|
||||
require.Equal(t, e.Data, n.Data)
|
||||
|
||||
compareIterator(t, e.Children, n.Children())
|
||||
compareNode(t, e, n)
|
||||
|
||||
idx++
|
||||
}
|
||||
@@ -199,7 +202,7 @@ func (r astRoot) toOrig() *ast.Root {
|
||||
}
|
||||
}
|
||||
|
||||
return builder.Finish()
|
||||
return builder.Tree()
|
||||
}
|
||||
|
||||
func childrenToOrig(b *ast.Builder, nodes []astNode) ast.Reference {
|
||||
@@ -229,24 +232,22 @@ func TestParser_AST(t *testing.T) {
|
||||
examples := []struct {
|
||||
desc string
|
||||
input string
|
||||
ast astRoot
|
||||
ast astNode
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
desc: "simple string assignment",
|
||||
input: `A = "hello"`,
|
||||
ast: astRoot{
|
||||
astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`hello`),
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`A`),
|
||||
},
|
||||
ast: astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`hello`),
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`A`),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -254,18 +255,16 @@ func TestParser_AST(t *testing.T) {
|
||||
{
|
||||
desc: "simple bool assignment",
|
||||
input: `A = true`,
|
||||
ast: astRoot{
|
||||
astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.Bool,
|
||||
Data: []byte(`true`),
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`A`),
|
||||
},
|
||||
ast: astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.Bool,
|
||||
Data: []byte(`true`),
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`A`),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -273,36 +272,34 @@ func TestParser_AST(t *testing.T) {
|
||||
{
|
||||
desc: "array of strings",
|
||||
input: `A = ["hello", ["world", "again"]]`,
|
||||
ast: astRoot{
|
||||
astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.Array,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`hello`),
|
||||
},
|
||||
{
|
||||
Kind: ast.Array,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`world`),
|
||||
},
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`again`),
|
||||
},
|
||||
ast: astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.Array,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`hello`),
|
||||
},
|
||||
{
|
||||
Kind: ast.Array,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`world`),
|
||||
},
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`again`),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`A`),
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`A`),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -310,27 +307,25 @@ func TestParser_AST(t *testing.T) {
|
||||
{
|
||||
desc: "array of arrays of strings",
|
||||
input: `A = ["hello", "world"]`,
|
||||
ast: astRoot{
|
||||
astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.Array,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`hello`),
|
||||
},
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`world`),
|
||||
},
|
||||
ast: astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.Array,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`hello`),
|
||||
},
|
||||
{
|
||||
Kind: ast.String,
|
||||
Data: []byte(`world`),
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`A`),
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`A`),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -338,33 +333,31 @@ func TestParser_AST(t *testing.T) {
|
||||
{
|
||||
desc: "inline table",
|
||||
input: `name = { first = "Tom", last = "Preston-Werner" }`,
|
||||
ast: astRoot{
|
||||
astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.InlineTable,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{Kind: ast.String, Data: []byte(`Tom`)},
|
||||
{Kind: ast.Key, Data: []byte(`first`)},
|
||||
},
|
||||
ast: astNode{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.InlineTable,
|
||||
Children: []astNode{
|
||||
{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{Kind: ast.String, Data: []byte(`Tom`)},
|
||||
{Kind: ast.Key, Data: []byte(`first`)},
|
||||
},
|
||||
{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{Kind: ast.String, Data: []byte(`Preston-Werner`)},
|
||||
{Kind: ast.Key, Data: []byte(`last`)},
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: ast.KeyValue,
|
||||
Children: []astNode{
|
||||
{Kind: ast.String, Data: []byte(`Preston-Werner`)},
|
||||
{Kind: ast.Key, Data: []byte(`last`)},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`name`),
|
||||
},
|
||||
},
|
||||
{
|
||||
Kind: ast.Key,
|
||||
Data: []byte(`name`),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -374,12 +367,14 @@ func TestParser_AST(t *testing.T) {
|
||||
for _, e := range examples {
|
||||
t.Run(e.desc, func(t *testing.T) {
|
||||
p := parser{}
|
||||
err := p.parse([]byte(e.input))
|
||||
p.Reset([]byte(e.input))
|
||||
p.NextExpression()
|
||||
err := p.Error()
|
||||
if e.err {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
compareAST(t, e.ast, p.builder.Finish())
|
||||
compareNode(t, e.ast, p.Expression())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user