small optimizations

This commit is contained in:
Arpad Ryszka 2017-07-30 05:10:46 +02:00
parent 59d0a333ed
commit ce60b08c8d
6 changed files with 42 additions and 29 deletions

View File

@ -1,4 +1,5 @@
SOURCES = $(shell find . -name '*.go') SOURCES = $(shell find . -name '*.go')
PARSERS = $(shell find . -name '*.parser')
default: build default: build
@ -8,13 +9,13 @@ imports:
build: $(SOURCES) build: $(SOURCES)
go build ./... go build ./...
check: build check: build $(PARSERS)
go test ./... -test.short -run ^Test go test ./... -test.short -run ^Test
fmt: $(SOURCES) fmt: $(SOURCES)
@gofmt -w -s $(SOURCES) @gofmt -w -s $(SOURCES)
cpu.out: $(SOURCES) cpu.out: $(SOURCES) $(PARSERS)
go test -v -run TestMMLFile -cpuprofile cpu.out go test -v -run TestMMLFile -cpuprofile cpu.out
cpu: cpu.out cpu: cpu.out

View File

@ -151,14 +151,14 @@ func (p *choiceParser) parse(t Trace, c *context) {
// t.Out1("parsing choice", c.offset) // t.Out1("parsing choice", c.offset)
// TODO: don't add documentation // TODO: don't add documentation
if p.commit&Documentation != 0 { // if p.commit&Documentation != 0 {
// t.Out1("fail, doc") // // t.Out1("fail, doc")
c.fail(c.offset) // c.fail(c.offset)
return // return
} // }
if _, ok := c.fromStore(p.id); ok { if c.fromStore(p.id) {
// t.Out1("found in store, match:", m) // t.Out1("found in store, match:")
return return
} }
@ -173,10 +173,11 @@ func (p *choiceParser) parse(t Trace, c *context) {
to := c.offset to := c.offset
var match bool var match bool
var nextTo int
var elementIndex int var elementIndex int
var foundMatch bool var foundMatch bool
var excludedIncluded []int
for { for {
foundMatch = false foundMatch = false
elementIndex = 0 elementIndex = 0
@ -184,23 +185,29 @@ func (p *choiceParser) parse(t Trace, c *context) {
for elementIndex < len(p.elements) { for elementIndex < len(p.elements) {
p.elements[elementIndex].parse(t, c) p.elements[elementIndex].parse(t, c)
elementIndex++ elementIndex++
nextTo = c.offset
c.offset = from
if !c.match || match && nextTo <= to { if !c.match || match && c.offset <= to {
c.offset = from
continue continue
} }
match = true match = true
foundMatch = true foundMatch = true
to = nextTo to = c.offset
c.offset = from
c.store.setMatch(from, p.id, to) c.store.setMatch(from, p.id, to)
for _, includedBy := range p.includedBy { if match {
if c.excluded(from, includedBy) { for _, includedBy := range excludedIncluded {
// t.Out1("storing included", includedBy)
c.store.setMatch(from, includedBy, to) 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)
}
}
} }
} }

View File

@ -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) to, m, ok := c.store.getMatch(c.offset, id)
if !ok { if !ok {
return false, false return false
} }
if m { if m {
@ -122,7 +122,7 @@ func (c *context) fromStore(id int) (bool, bool) {
c.fail(c.offset) c.fail(c.offset)
} }
return m, true return true
} }
func (c *context) success(to int) { func (c *context) success(to int) {

View File

@ -98,8 +98,8 @@ mutable-struct = "~" wsnlc* struct-fact;
channel = "<>" | "<" wsnlc* int wsnlc* ">"; channel = "<>" | "<" wsnlc* int wsnlc* ">";
and-expression:doc = "and" wsc* "(" (wsnlc | ",")* expression-list? (wsnlc | ",")* ")"; // and-expression:doc = "and" wsc* "(" (wsnlc | ",")* expression-list? (wsnlc | ",")* ")";
or-expression:doc = "or" wsc* "(" (wsnlc | ",")* expression-list? (wsnlc | ",")* ")"; // or-expression:doc = "or" wsc* "(" (wsnlc | ",")* expression-list? (wsnlc | ",")* ")";
argument-list:alias = static-symbol (list-sep static-symbol)*; argument-list:alias = static-symbol (list-sep static-symbol)*;
collect-symbol = "..." wsnlc* static-symbol; collect-symbol = "..." wsnlc* static-symbol;
@ -373,8 +373,8 @@ primary-expression:alias = int
| struct | struct
| mutable-struct | mutable-struct
| channel | channel
| and-expression // only documentation // | and-expression // only documentation
| or-expression // only documentation // | or-expression // only documentation
| function | function
| effect | effect
| indexer | indexer

View File

@ -5,6 +5,7 @@ error reporting
- print the deepest non-alias node name - print the deepest non-alias node name
- print the documentation of the node name - print the documentation of the node name
read, with error reporting read, with error reporting
what was the bug with the large json from eskip?
[next] [next]
optimization optimization

View File

@ -8,6 +8,7 @@ type sequenceDefinition struct {
includedBy []int includedBy []int
ranges [][]int ranges [][]int
sbuilder *sequenceBuilder sbuilder *sequenceBuilder
allChars bool
} }
type sequenceParser struct { type sequenceParser struct {
@ -17,6 +18,7 @@ type sequenceParser struct {
items []parser items []parser
ranges [][]int ranges [][]int
includedBy []int includedBy []int
allChars bool
} }
type sequenceBuilder struct { type sequenceBuilder struct {
@ -82,6 +84,7 @@ func (d *sequenceDefinition) init(r *registry) error {
d.sbuilder.ranges = d.ranges d.sbuilder.ranges = d.ranges
d.sbuilder.allChars = allChars d.sbuilder.allChars = allChars
d.allChars = allChars
if !d.includeItems() { if !d.includeItems() {
return nil return nil
@ -134,6 +137,7 @@ func (d *sequenceDefinition) parser(r *registry, parsers *idSet) (parser, error)
id: d.id, id: d.id,
commit: d.commit, commit: d.commit,
includedBy: d.includedBy, includedBy: d.includedBy,
allChars: d.allChars,
} }
r.setParser(sp) r.setParser(sp)
@ -186,11 +190,11 @@ func (p *sequenceParser) parse(t Trace, c *context) {
// t = t.Extend(p.name) // t = t.Extend(p.name)
// t.Out1("parsing sequence", c.offset) // t.Out1("parsing sequence", c.offset)
if p.commit&Documentation != 0 { // if p.commit&Documentation != 0 {
// t.Out1("fail, doc") // // t.Out1("fail, doc")
c.fail(c.offset) // c.fail(c.offset)
return // return
} // }
if c.excluded(c.offset, p.id) { if c.excluded(c.offset, p.id) {
// t.Out1("fail, excluded") // t.Out1("fail, excluded")