microoptimizations in exclude/include, sequence and choice

This commit is contained in:
Arpad Ryszka 2017-07-18 00:38:44 +02:00
parent b9f05bed7d
commit 86d244790a
5 changed files with 40 additions and 40 deletions

View File

@ -83,6 +83,6 @@ func (p *charParser) parse(t Trace, c *context) {
c.success(c.offset + 1)
for _, includedBy := range p.includedBy {
includedBy.storeIncluded(c, c.offset, c.offset + 1)
includedBy.storeIncluded(c, c.offset, c.offset+1)
}
}

View File

@ -117,15 +117,17 @@ func (p *choiceParser) parse(t Trace, c *context) {
to := c.offset
var match bool
var nextTo int
var elementIndex int
for {
elementIndex := 0
var foundMatch bool
elementIndex = 0
for elementIndex < len(p.elements) {
p.elements[elementIndex].parse(t, c)
elementIndex++
nextTo := c.offset
nextTo = c.offset
c.offset = from
if !c.match || match && nextTo <= to {

View File

@ -15,7 +15,7 @@ type context struct {
tokens []rune
match bool
node *Node
isExcluded []*idSet
isExcluded [][]int
}
func newContext(r io.RuneReader) *context {
@ -65,43 +65,41 @@ func (c *context) token() (rune, bool) {
}
func (c *context) excluded(offset int, id int) bool {
if len(c.isExcluded) <= offset || c.isExcluded[offset] == nil {
if len(c.isExcluded) <= id {
return false
}
return c.isExcluded[offset].has(id)
}
func (c *context) exclude(offset int, id int) {
if c.excluded(offset, id) {
return
}
if len(c.isExcluded) <= offset {
c.isExcluded = append(c.isExcluded, nil)
if cap(c.isExcluded) > offset {
c.isExcluded = c.isExcluded[:offset+1]
} else {
c.isExcluded = append(
c.isExcluded[:cap(c.isExcluded)],
make([]*idSet, offset+1-cap(c.isExcluded))...,
)
for i := range c.isExcluded[id] {
if c.isExcluded[id][i] == offset {
return true
}
}
if c.isExcluded[offset] == nil {
c.isExcluded[offset] = &idSet{}
return false
}
func (c *context) exclude(offset int, id int) {
if len(c.isExcluded) <= id {
if cap(c.isExcluded) > id {
c.isExcluded = c.isExcluded[:id+1]
} else {
c.isExcluded = c.isExcluded[:cap(c.isExcluded)]
for i := cap(c.isExcluded); i <= id; i++ {
c.isExcluded = append(c.isExcluded, nil)
}
}
}
c.isExcluded[offset].set(id)
c.isExcluded[id] = append(c.isExcluded[id], offset)
}
func (c *context) include(offset int, id int) {
if len(c.isExcluded) <= offset || c.isExcluded[offset] == nil {
return
for i := range c.isExcluded[id] {
if c.isExcluded[id][i] == offset {
c.isExcluded[id] = append(c.isExcluded[id][:i], c.isExcluded[id][i+1:]...)
break
}
}
c.isExcluded[offset].unset(id)
}
func (c *context) fromStore(id int) (bool, bool) {

View File

@ -129,9 +129,9 @@ func (p *sequenceParser) parse(t Trace, c *context) {
return
}
if c.store.hasNoMatch(c.offset, p.id) {
c.fail(c.offset)
}
// if c.store.hasNoMatch(c.offset, p.id) {
// c.fail(c.offset)
// }
c.exclude(c.offset, p.id)
@ -144,7 +144,7 @@ func (p *sequenceParser) parse(t Trace, c *context) {
p.items[itemIndex].parse(t, c)
if !c.match {
if currentCount < p.ranges[itemIndex][0] {
c.store.setNoMatch(from, p.id)
// c.store.setNoMatch(from, p.id)
c.fail(from)
c.include(from, p.id)
return

View File

@ -2,7 +2,7 @@ package treerack
type store struct {
noMatch []*idSet
match [][]int
match [][]int
}
func (s *store) hasNoMatch(offset, id int) bool {
@ -23,7 +23,7 @@ func (s *store) getMatch(offset, id int) (int, bool, bool) {
}
var (
found bool
found bool
length int
)
@ -33,8 +33,8 @@ func (s *store) getMatch(offset, id int) (int, bool, bool) {
}
found = true
if s.match[offset][i + 1] > length {
length = s.match[offset][i + 1]
if s.match[offset][i+1] > length {
length = s.match[offset][i+1]
}
}
@ -52,7 +52,7 @@ func (s *store) ensureOffset(offset int) {
}
s.match = s.match[:cap(s.match)]
for i := len(s.match); i <= offset; i++ {
for i := cap(s.match); i <= offset; i++ {
s.match = append(s.match, nil)
}
}
@ -65,10 +65,10 @@ func (s *store) setMatch(offset, id, to int) {
func (s *store) setNoMatch(offset, id int) {
if len(s.noMatch) <= offset {
if cap(s.noMatch) > offset {
s.noMatch = s.noMatch[:offset + 1]
s.noMatch = s.noMatch[:offset+1]
} else {
s.noMatch = s.noMatch[:cap(s.noMatch)]
for i := len(s.noMatch); i <= offset; i++ {
for i := cap(s.noMatch); i <= offset; i++ {
s.noMatch = append(s.noMatch, nil)
}
}