From ce60b08c8d87f40a9cd4614c9e8c262ab6c794e4 Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Sun, 30 Jul 2017 05:10:46 +0200 Subject: [PATCH] small optimizations --- Makefile | 5 +++-- choice.go | 37 ++++++++++++++++++++++--------------- context.go | 6 +++--- mml.parser | 8 ++++---- notes.txt | 1 + sequence.go | 14 +++++++++----- 6 files changed, 42 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 4e2a6db..fc268dd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ SOURCES = $(shell find . -name '*.go') +PARSERS = $(shell find . -name '*.parser') default: build @@ -8,13 +9,13 @@ imports: build: $(SOURCES) go build ./... -check: build +check: build $(PARSERS) go test ./... -test.short -run ^Test fmt: $(SOURCES) @gofmt -w -s $(SOURCES) -cpu.out: $(SOURCES) +cpu.out: $(SOURCES) $(PARSERS) go test -v -run TestMMLFile -cpuprofile cpu.out cpu: cpu.out diff --git a/choice.go b/choice.go index 5db1676..b6f44eb 100644 --- a/choice.go +++ b/choice.go @@ -151,14 +151,14 @@ func (p *choiceParser) parse(t Trace, c *context) { // t.Out1("parsing choice", c.offset) // TODO: don't add documentation - if p.commit&Documentation != 0 { - // t.Out1("fail, doc") - c.fail(c.offset) - return - } + // if p.commit&Documentation != 0 { + // // t.Out1("fail, doc") + // c.fail(c.offset) + // return + // } - if _, ok := c.fromStore(p.id); ok { - // t.Out1("found in store, match:", m) + if c.fromStore(p.id) { + // t.Out1("found in store, match:") return } @@ -173,10 +173,11 @@ func (p *choiceParser) parse(t Trace, c *context) { to := c.offset var match bool - var nextTo int var elementIndex int var foundMatch bool + var excludedIncluded []int + for { foundMatch = false elementIndex = 0 @@ -184,23 +185,29 @@ func (p *choiceParser) parse(t Trace, c *context) { for elementIndex < len(p.elements) { p.elements[elementIndex].parse(t, c) elementIndex++ - nextTo = c.offset - c.offset = from - if !c.match || match && nextTo <= to { + if !c.match || match && c.offset <= to { + c.offset = from continue } match = true foundMatch = true - to = nextTo + to = c.offset + c.offset = from c.store.setMatch(from, p.id, to) - for _, includedBy := range p.includedBy { - if c.excluded(from, includedBy) { - // t.Out1("storing included", includedBy) + if match { + for _, includedBy := range excludedIncluded { c.store.setMatch(from, includedBy, to) } + } else { + for _, includedBy := range p.includedBy { + if c.excluded(from, includedBy) { + excludedIncluded = append(excludedIncluded, includedBy) + c.store.setMatch(from, includedBy, to) + } + } } } diff --git a/context.go b/context.go index eaec093..852927c 100644 --- a/context.go +++ b/context.go @@ -110,10 +110,10 @@ func (c *context) include(offset int, id int) { } } -func (c *context) fromStore(id int) (bool, bool) { +func (c *context) fromStore(id int) bool { to, m, ok := c.store.getMatch(c.offset, id) if !ok { - return false, false + return false } if m { @@ -122,7 +122,7 @@ func (c *context) fromStore(id int) (bool, bool) { c.fail(c.offset) } - return m, true + return true } func (c *context) success(to int) { diff --git a/mml.parser b/mml.parser index 1c5012b..b0cfa22 100644 --- a/mml.parser +++ b/mml.parser @@ -98,8 +98,8 @@ mutable-struct = "~" wsnlc* struct-fact; channel = "<>" | "<" wsnlc* int wsnlc* ">"; -and-expression:doc = "and" wsc* "(" (wsnlc | ",")* expression-list? (wsnlc | ",")* ")"; -or-expression:doc = "or" wsc* "(" (wsnlc | ",")* expression-list? (wsnlc | ",")* ")"; +// and-expression:doc = "and" wsc* "(" (wsnlc | ",")* expression-list? (wsnlc | ",")* ")"; +// or-expression:doc = "or" wsc* "(" (wsnlc | ",")* expression-list? (wsnlc | ",")* ")"; argument-list:alias = static-symbol (list-sep static-symbol)*; collect-symbol = "..." wsnlc* static-symbol; @@ -373,8 +373,8 @@ primary-expression:alias = int | struct | mutable-struct | channel - | and-expression // only documentation - | or-expression // only documentation + // | and-expression // only documentation + // | or-expression // only documentation | function | effect | indexer diff --git a/notes.txt b/notes.txt index 265eff0..5d2565e 100644 --- a/notes.txt +++ b/notes.txt @@ -5,6 +5,7 @@ error reporting - print the deepest non-alias node name - print the documentation of the node name read, with error reporting +what was the bug with the large json from eskip? [next] optimization diff --git a/sequence.go b/sequence.go index deefa83..9c7ced5 100644 --- a/sequence.go +++ b/sequence.go @@ -8,6 +8,7 @@ type sequenceDefinition struct { includedBy []int ranges [][]int sbuilder *sequenceBuilder + allChars bool } type sequenceParser struct { @@ -17,6 +18,7 @@ type sequenceParser struct { items []parser ranges [][]int includedBy []int + allChars bool } type sequenceBuilder struct { @@ -82,6 +84,7 @@ func (d *sequenceDefinition) init(r *registry) error { d.sbuilder.ranges = d.ranges d.sbuilder.allChars = allChars + d.allChars = allChars if !d.includeItems() { return nil @@ -134,6 +137,7 @@ func (d *sequenceDefinition) parser(r *registry, parsers *idSet) (parser, error) id: d.id, commit: d.commit, includedBy: d.includedBy, + allChars: d.allChars, } r.setParser(sp) @@ -186,11 +190,11 @@ func (p *sequenceParser) parse(t Trace, c *context) { // t = t.Extend(p.name) // t.Out1("parsing sequence", c.offset) - if p.commit&Documentation != 0 { - // t.Out1("fail, doc") - c.fail(c.offset) - return - } + // if p.commit&Documentation != 0 { + // // t.Out1("fail, doc") + // c.fail(c.offset) + // return + // } if c.excluded(c.offset, p.id) { // t.Out1("fail, excluded")