microoptimizations in exclude/include, sequence and choice
This commit is contained in:
parent
b9f05bed7d
commit
86d244790a
@ -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 {
|
||||
|
46
context.go
46
context.go
@ -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)
|
||||
for i := range c.isExcluded[id] {
|
||||
if c.isExcluded[id][i] == offset {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
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]
|
||||
if len(c.isExcluded) <= id {
|
||||
if cap(c.isExcluded) > id {
|
||||
c.isExcluded = c.isExcluded[:id+1]
|
||||
} else {
|
||||
c.isExcluded = append(
|
||||
c.isExcluded[:cap(c.isExcluded)],
|
||||
make([]*idSet, offset+1-cap(c.isExcluded))...,
|
||||
)
|
||||
c.isExcluded = c.isExcluded[:cap(c.isExcluded)]
|
||||
for i := cap(c.isExcluded); i <= id; i++ {
|
||||
c.isExcluded = append(c.isExcluded, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if c.isExcluded[offset] == nil {
|
||||
c.isExcluded[offset] = &idSet{}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -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
|
||||
|
4
store.go
4
store.go
@ -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)
|
||||
}
|
||||
}
|
||||
@ -68,7 +68,7 @@ func (s *store) setNoMatch(offset, id int) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user