Backup changes for performance fixes.

This commit is contained in:
Maurice Makaay 2019-07-05 15:06:57 +00:00
parent c97a21b146
commit 68dbf2d78e
7 changed files with 41 additions and 35 deletions

View File

@ -32,7 +32,7 @@ func main() {
flag.Usage()
}
toml, err := parse.RunWithoutSanityChecks(os.Stdin)
toml, err := parse.Run(os.Stdin)
if err != nil {
log.Fatalf("Error decoding TOML: %s", err)
} else {

View File

@ -28,11 +28,10 @@ var (
// A '#' hash symbol marks the rest of the line as a comment.
// All characters up to the end of the line are included in the comment.
comment = c.Seq(a.Hash, a.UntilEndOfLine)
comment = c.Seq(a.Hash, a.UntilEndOfLine.Optional())
optionalComment = comment.Optional()
endOfLineOrComment = c.Seq(whitespace, optionalComment, a.EndOfLine)
whitespaceOrComment = c.ZeroOrMore(whitespaceInclNewlines.Or(comment))
endOfLineOrComment = c.Seq(whitespace, optionalComment, a.EndOfLine)
)
type parser struct {

View File

@ -31,9 +31,10 @@ import (
// 2, # this is ok
// ]
var (
arrayOpen = a.SquareOpen.Then(whitespaceOrComment)
arraySeparator = c.Seq(whitespace, a.Comma, whitespaceOrComment)
arrayClose = c.Seq(whitespace, a.Comma.Optional(), whitespaceOrComment, a.SquareClose)
arrayWhitespaceComment = c.OneOrMore(whitespaceInclNewlines.Or(comment))
arrayOpen = a.SquareOpen.Then(arrayWhitespaceComment.Optional())
arraySeparator = c.Seq(whitespace, a.Comma, arrayWhitespaceComment.Optional())
arrayClose = c.Seq(whitespace, a.Comma.Optional(), arrayWhitespaceComment.Optional(), a.SquareClose)
)
func (t *parser) parseArray(p *parse.API) (*ast.Value, bool) {

View File

@ -14,32 +14,36 @@ func TestStartArray(t *testing.T) {
func TestArray(t *testing.T) {
for _, test := range []parseTest{
{"x=[ape", `{}`, `unexpected input (expected a value) at line 1, column 4`},
{"x=[1", `{}`, `unexpected end of file (expected an array separator) at line 1, column 5`},
{"x=[]", `{"x": []}`, ``},
{"x=[\n]", `{"x": []}`, ``},
{"x=[,]", `{"x": []}`, ``},
{"x=[ , ]", `{"x": []}`, ``},
{"x=[ \n , \r\n ]", `{"x": []}`, ``},
{"x=[ \t , \t ]", `{"x": []}`, ``},
{"x=[\r\n\r\n , \r\n \t\n]", `{"x": []}`, ``},
{"x=[\n#comment on its own line\n]", `{"x": []}`, ``},
{"x=[#comment before close\n]", `{"x": []}`, ``},
{"x=[,#comment after separator\n]", `{"x": []}`, ``},
{"x=[#comment before separator\n,]", `{"x": []}`, ``},
{"x=[#comment before value\n1]", `{"x": [1]}`, ``},
{"x=[1#comment after value\n]", `{"x": [1]}`, ``},
{"x=[1\n#comment on its own line after value\n]", `{"x": [1]}`, ``},
{"x=[1#comment 1\n,\n2]", `{}`, `unexpected input (expected an array separator) at line 1, column 5`},
{"x=[1]", `{"x": [1]}`, ``},
{"x=[1,0x2, 0b11, 0o4]", `{"x": [1, 2, 3, 4]}`, ``},
{"x=[0.1,0.2,3e-1,0.04e+1, nan, inf]", `{"x": [0.1, 0.2, 0.3, 0.4, NaN, +Inf]}`, ``},
{"x=[\n\t 'a', \"b\", '''c''', \"\"\"d\ne\"\"\",\n \t]", `{"x": ["a", "b", "c", "d\ne"]}`, ``},
{`x=[1, 2, 3, "four"]`, `{}`, `type mismatch in array of integers: found an item of type string at line 1, column 19`},
{`x=[[1],['a']]`, `{"x": [[1], ["a"]]}`, ``},
{`x=[[[],[]],[]]`, `{"x": [[[], []], []]}`, ``},
{"x=[\r\n\r\n \t\n [\r\n\r\n\t [],[\t]],\t\n[]\t \t \n ]", `{"x": [[[], []], []]}`, ``},
{`x=[[1],'a']`, `{}`, `type mismatch in array of arrays: found an item of type string at line 1, column 11`},
// {"x=[ape", `{}`, `unexpected input (expected a value) at line 1, column 4`},
// {"x=[1", `{}`, `unexpected end of file (expected an array separator) at line 1, column 5`},
// {"x=[]", `{"x": []}`, ``},
// {"x=[\n]", `{"x": []}`, ``},
// {"x=[,]", `{"x": []}`, ``},
// {"x=[ , ]", `{"x": []}`, ``},
// {"x=[ \n , \r\n ]", `{"x": []}`, ``},
// {"x=[ \t , \t ]", `{"x": []}`, ``},
// {"x=[\r\n\r\n , \r\n \t\n]", `{"x": []}`, ``},
// {"x=[\n#comment on its own line\n]", `{"x": []}`, ``},
// {"x=[#comment before close\n]", `{"x": []}`, ``},
// {"x=[,#comment after separator\n]", `{"x": []}`, ``},
// {"x=[#comment before separator\n,]", `{"x": []}`, ``},
// {"x=[#comment before value\n1]", `{"x": [1]}`, ``},
// {"x=[1#comment after value\n]", `{"x": [1]}`, ``},
// {"x=[1\n#comment on its own line after value\n]", `{"x": [1]}`, ``},
// {"x=[1#comment 1\n,\n2]", `{}`, `unexpected input (expected an array separator) at line 1, column 5`},
// {"#\nx=[ #\n1,#\n2 #\n]#\n#\n", `{"x": [1, 2]}`, ``},
// Freak case that I ran into during optimization of the code.
// It failed with 'unexpected input (expected an array separator) at line 4, column 2'.
{"#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\na=[\n123,\n456]", `{"a": [123, 456]}`, ``},
// {"x=[1]", `{"x": [1]}`, ``},
// {"x=[1,0x2, 0b11, 0o4]", `{"x": [1, 2, 3, 4]}`, ``},
// {"x=[0.1,0.2,3e-1,0.04e+1, nan, inf]", `{"x": [0.1, 0.2, 0.3, 0.4, NaN, +Inf]}`, ``},
// {"x=[\n\t 'a', \"b\", '''c''', \"\"\"d\ne\"\"\",\n \t]", `{"x": ["a", "b", "c", "d\ne"]}`, ``},
// {`x=[1, 2, 3, "four"]`, `{}`, `type mismatch in array of integers: found an item of type string at line 1, column 19`},
// {`x=[[1],['a']]`, `{"x": [[1], ["a"]]}`, ``},
// {`x=[[[],[]],[]]`, `{"x": [[[], []], []]}`, ``},
// {"x=[\r\n\r\n \t\n [\r\n\r\n\t [],[\t]],\t\n[]\t \t \n ]", `{"x": [[[], []], []]}`, ``},
// {`x=[[1],'a']`, `{}`, `type mismatch in array of arrays: found an item of type string at line 1, column 11`},
} {
p := newParser()
testParse(t, p, p.startDocument, test)

View File

@ -91,10 +91,11 @@ func (t *parser) parseDateTime(p *parse.API) (*ast.Value, bool) {
p.Expected("a date and/or time")
return nil, false
}
token := p.Result().Token(0)
result := p.Result()
token := result.Token(0)
layout := ""
for _, l := range token.Value.([]*tokenize.Token) {
for _, l := range token.Value.([]tokenize.Token) {
layout += l.Type.(string)
}
value, err := time.Parse(layout, string(token.Runes))

Binary file not shown.

View File

@ -2,4 +2,5 @@
go build
ppfile=`cat long.toml | ./parse2 2>&1 | grep "cpu profiling enabled" | cut -d, -f2`
#ppfile=`cat short.toml | ./parse2 2>&1 | grep "cpu profiling enabled" | cut -d, -f2`
go tool pprof -http 0.0.0.0:8888 ./parse2 $ppfile