microoptimizations in exclude/include, sequence and choice
This commit is contained in:
parent
b9f05bed7d
commit
86d244790a
2
char.go
2
char.go
@ -83,6 +83,6 @@ func (p *charParser) parse(t Trace, c *context) {
|
|||||||
|
|
||||||
c.success(c.offset + 1)
|
c.success(c.offset + 1)
|
||||||
for _, includedBy := range p.includedBy {
|
for _, includedBy := range p.includedBy {
|
||||||
includedBy.storeIncluded(c, c.offset, c.offset + 1)
|
includedBy.storeIncluded(c, c.offset, c.offset+1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,15 +117,17 @@ 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
|
||||||
|
|
||||||
for {
|
for {
|
||||||
elementIndex := 0
|
|
||||||
var foundMatch bool
|
var foundMatch bool
|
||||||
|
elementIndex = 0
|
||||||
|
|
||||||
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
|
nextTo = c.offset
|
||||||
c.offset = from
|
c.offset = from
|
||||||
|
|
||||||
if !c.match || match && nextTo <= to {
|
if !c.match || match && nextTo <= to {
|
||||||
|
46
context.go
46
context.go
@ -15,7 +15,7 @@ type context struct {
|
|||||||
tokens []rune
|
tokens []rune
|
||||||
match bool
|
match bool
|
||||||
node *Node
|
node *Node
|
||||||
isExcluded []*idSet
|
isExcluded [][]int
|
||||||
}
|
}
|
||||||
|
|
||||||
func newContext(r io.RuneReader) *context {
|
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 {
|
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 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) {
|
func (c *context) exclude(offset int, id int) {
|
||||||
if c.excluded(offset, id) {
|
if len(c.isExcluded) <= id {
|
||||||
return
|
if cap(c.isExcluded) > id {
|
||||||
}
|
c.isExcluded = c.isExcluded[:id+1]
|
||||||
|
|
||||||
if len(c.isExcluded) <= offset {
|
|
||||||
c.isExcluded = append(c.isExcluded, nil)
|
|
||||||
if cap(c.isExcluded) > offset {
|
|
||||||
c.isExcluded = c.isExcluded[:offset+1]
|
|
||||||
} else {
|
} else {
|
||||||
c.isExcluded = append(
|
c.isExcluded = c.isExcluded[:cap(c.isExcluded)]
|
||||||
c.isExcluded[:cap(c.isExcluded)],
|
for i := cap(c.isExcluded); i <= id; i++ {
|
||||||
make([]*idSet, offset+1-cap(c.isExcluded))...,
|
c.isExcluded = append(c.isExcluded, nil)
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.isExcluded[offset] == nil {
|
c.isExcluded[id] = append(c.isExcluded[id], offset)
|
||||||
c.isExcluded[offset] = &idSet{}
|
|
||||||
}
|
|
||||||
|
|
||||||
c.isExcluded[offset].set(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) include(offset int, id int) {
|
func (c *context) include(offset int, id int) {
|
||||||
if len(c.isExcluded) <= offset || c.isExcluded[offset] == nil {
|
for i := range c.isExcluded[id] {
|
||||||
return
|
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) {
|
func (c *context) fromStore(id int) (bool, bool) {
|
||||||
|
@ -129,9 +129,9 @@ func (p *sequenceParser) parse(t Trace, c *context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.store.hasNoMatch(c.offset, p.id) {
|
// if c.store.hasNoMatch(c.offset, p.id) {
|
||||||
c.fail(c.offset)
|
// c.fail(c.offset)
|
||||||
}
|
// }
|
||||||
|
|
||||||
c.exclude(c.offset, p.id)
|
c.exclude(c.offset, p.id)
|
||||||
|
|
||||||
@ -144,7 +144,7 @@ func (p *sequenceParser) parse(t Trace, c *context) {
|
|||||||
p.items[itemIndex].parse(t, c)
|
p.items[itemIndex].parse(t, c)
|
||||||
if !c.match {
|
if !c.match {
|
||||||
if currentCount < p.ranges[itemIndex][0] {
|
if currentCount < p.ranges[itemIndex][0] {
|
||||||
c.store.setNoMatch(from, p.id)
|
// c.store.setNoMatch(from, p.id)
|
||||||
c.fail(from)
|
c.fail(from)
|
||||||
c.include(from, p.id)
|
c.include(from, p.id)
|
||||||
return
|
return
|
||||||
|
10
store.go
10
store.go
@ -33,8 +33,8 @@ func (s *store) getMatch(offset, id int) (int, bool, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
found = true
|
found = true
|
||||||
if s.match[offset][i + 1] > length {
|
if s.match[offset][i+1] > length {
|
||||||
length = s.match[offset][i + 1]
|
length = s.match[offset][i+1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ func (s *store) ensureOffset(offset int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.match = s.match[:cap(s.match)]
|
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)
|
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) {
|
func (s *store) setNoMatch(offset, id int) {
|
||||||
if len(s.noMatch) <= offset {
|
if len(s.noMatch) <= offset {
|
||||||
if cap(s.noMatch) > offset {
|
if cap(s.noMatch) > offset {
|
||||||
s.noMatch = s.noMatch[:offset + 1]
|
s.noMatch = s.noMatch[:offset+1]
|
||||||
} else {
|
} else {
|
||||||
s.noMatch = s.noMatch[:cap(s.noMatch)]
|
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)
|
s.noMatch = append(s.noMatch, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user