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')
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

View File

@ -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)
}
}
}
}

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)
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) {

View File

@ -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

View File

@ -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

View File

@ -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")