use id based cache
This commit is contained in:
parent
50592ed83f
commit
baa2ceede8
8
char.go
8
char.go
@ -28,7 +28,7 @@ func newChar(
|
||||
|
||||
func (p *charParser) nodeName() string { return p.name }
|
||||
func (p *charParser) nodeID() int { return p.id }
|
||||
func (p *charParser) setID(id int) { p.id = p.id }
|
||||
func (p *charParser) setID(id int) { p.id = id }
|
||||
|
||||
func (p *charParser) parser(r *registry, parsers *idSet) (parser, error) {
|
||||
if parsers.has(p.id) {
|
||||
@ -85,7 +85,7 @@ func (p *charParser) parse(t Trace, c *context) {
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := c.fromStore(p.name); ok {
|
||||
if _, ok := c.fromStore(p.id); ok {
|
||||
// t.Out1("found in store, match:", m)
|
||||
return
|
||||
}
|
||||
@ -93,7 +93,7 @@ func (p *charParser) parse(t Trace, c *context) {
|
||||
if tok, ok := c.token(); ok && p.match(tok) {
|
||||
// t.Out1("success", string(tok))
|
||||
n := newNode(p.name, p.id, c.offset, c.offset+1, p.commit)
|
||||
c.store.set(c.offset, p.name, n)
|
||||
c.store.set(c.offset, p.id, n)
|
||||
for _, includedBy := range p.includedBy {
|
||||
includedBy.storeIncluded(c, n)
|
||||
}
|
||||
@ -102,7 +102,7 @@ func (p *charParser) parse(t Trace, c *context) {
|
||||
return
|
||||
} else {
|
||||
// t.Out1("fail", string(tok))
|
||||
c.store.set(c.offset, p.name, nil)
|
||||
c.store.set(c.offset, p.id, nil)
|
||||
c.fail(c.offset)
|
||||
return
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ func (p *choiceParser) storeIncluded(c *context, n *Node) {
|
||||
|
||||
nc := newNode(p.name, p.id, n.From, n.To, p.commit)
|
||||
nc.append(n)
|
||||
c.store.set(nc.From, p.name, nc)
|
||||
c.store.set(nc.From, p.id, nc)
|
||||
|
||||
for _, includedBy := range p.includedBy {
|
||||
includedBy.storeIncluded(c, nc)
|
||||
@ -109,7 +109,7 @@ func (p *choiceParser) parse(t Trace, c *context) {
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := c.fromStore(p.name); ok {
|
||||
if _, ok := c.fromStore(p.id); ok {
|
||||
// t.Out1("found in store, match:", m)
|
||||
return
|
||||
}
|
||||
@ -144,7 +144,7 @@ func (p *choiceParser) parse(t Trace, c *context) {
|
||||
node = newNode(p.name, p.id, c.offset, c.offset, p.commit)
|
||||
node.append(c.node)
|
||||
|
||||
c.store.set(node.From, p.name, node)
|
||||
c.store.set(node.From, p.id, node)
|
||||
for _, includedBy := range p.includedBy {
|
||||
includedBy.storeIncluded(c, node)
|
||||
}
|
||||
@ -163,7 +163,7 @@ func (p *choiceParser) parse(t Trace, c *context) {
|
||||
}
|
||||
|
||||
// t.Out1("fail")
|
||||
c.store.set(node.From, p.name, nil)
|
||||
c.store.set(node.From, p.id, nil)
|
||||
c.fail(node.From)
|
||||
c.include(initialOffset, p.id) // TODO: test if can be optimized
|
||||
}
|
||||
|
@ -104,8 +104,8 @@ func (c *context) include(offset int, id int) {
|
||||
c.isExcluded[offset].unset(id)
|
||||
}
|
||||
|
||||
func (c *context) fromStore(name string) (bool, bool) {
|
||||
n, m, ok := c.store.get(c.offset, name)
|
||||
func (c *context) fromStore(id int) (bool, bool) {
|
||||
n, m, ok := c.store.get(c.offset, id)
|
||||
if !ok {
|
||||
return false, false
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ func (p *sequenceParser) storeIncluded(c *context, n *Node) {
|
||||
|
||||
nc := newNode(p.name, p.id, n.From, n.To, p.commit)
|
||||
nc.append(n)
|
||||
c.store.set(nc.From, p.name, nc)
|
||||
c.store.set(nc.From, p.id, nc)
|
||||
|
||||
for _, includedBy := range p.includedBy {
|
||||
includedBy.storeIncluded(c, nc)
|
||||
@ -145,7 +145,7 @@ func (p *sequenceParser) parse(t Trace, c *context) {
|
||||
node := newNode(p.name, p.id, c.offset, c.offset, p.commit)
|
||||
|
||||
for len(items) > 0 {
|
||||
m, ok := c.fromStore(items[0].nodeName())
|
||||
m, ok := c.fromStore(items[0].nodeID())
|
||||
if ok {
|
||||
// t.Out1("sequence item found in store, match:", m, items[0].nodeName(), c.offset)
|
||||
} else {
|
||||
@ -156,7 +156,7 @@ func (p *sequenceParser) parse(t Trace, c *context) {
|
||||
if !m {
|
||||
if currentCount < ranges[0][0] {
|
||||
// t.Out1("fail, item failed")
|
||||
c.store.set(node.From, p.name, nil)
|
||||
c.store.set(node.From, p.id, nil)
|
||||
c.fail(node.From)
|
||||
c.include(initialOffset, p.id)
|
||||
return
|
||||
@ -182,7 +182,7 @@ func (p *sequenceParser) parse(t Trace, c *context) {
|
||||
|
||||
// t.Out1("success, items parsed")
|
||||
|
||||
c.store.set(node.From, p.name, node)
|
||||
c.store.set(node.From, p.id, node)
|
||||
for _, includedBy := range p.includedBy {
|
||||
includedBy.storeIncluded(c, node)
|
||||
}
|
||||
|
86
store.go
86
store.go
@ -1,91 +1,83 @@
|
||||
package treerack
|
||||
|
||||
type storedItem struct {
|
||||
name string
|
||||
node *Node
|
||||
}
|
||||
|
||||
type storeEntry struct {
|
||||
match []*storedItem
|
||||
noMatch []string
|
||||
match *idSet
|
||||
noMatch *idSet
|
||||
nodes []*Node
|
||||
}
|
||||
|
||||
type store struct {
|
||||
tokens []*storeEntry
|
||||
entries []*storeEntry
|
||||
}
|
||||
|
||||
func (c *store) get(offset int, name string) (*Node, bool, bool) {
|
||||
if len(c.tokens) <= offset {
|
||||
func (c *store) get(offset int, id int) (*Node, bool, bool) {
|
||||
if len(c.entries) <= offset {
|
||||
return nil, false, false
|
||||
}
|
||||
|
||||
tc := c.tokens[offset]
|
||||
tc := c.entries[offset]
|
||||
if tc == nil {
|
||||
return nil, false, false
|
||||
}
|
||||
|
||||
for _, i := range tc.noMatch {
|
||||
if i == name {
|
||||
return nil, false, true
|
||||
}
|
||||
if tc.noMatch.has(id) {
|
||||
return nil, false, true
|
||||
}
|
||||
|
||||
for _, i := range tc.match {
|
||||
if i.name == name {
|
||||
return i.node, true, true
|
||||
if !tc.match.has(id) {
|
||||
return nil, false, false
|
||||
}
|
||||
|
||||
for _, n := range tc.nodes {
|
||||
if n.id == id {
|
||||
return n, true, true
|
||||
}
|
||||
}
|
||||
|
||||
return nil, false, false
|
||||
}
|
||||
|
||||
func (c *store) set(offset int, name string, n *Node) {
|
||||
func (c *store) set(offset int, id int, n *Node) {
|
||||
var tc *storeEntry
|
||||
if len(c.tokens) > offset {
|
||||
tc = c.tokens[offset]
|
||||
if len(c.entries) > offset {
|
||||
tc = c.entries[offset]
|
||||
} else {
|
||||
if cap(c.tokens) > offset {
|
||||
c.tokens = c.tokens[:offset+1]
|
||||
if cap(c.entries) > offset {
|
||||
c.entries = c.entries[:offset+1]
|
||||
} else {
|
||||
c.tokens = c.tokens[:cap(c.tokens)]
|
||||
for len(c.tokens) <= offset {
|
||||
c.tokens = append(c.tokens, nil)
|
||||
c.entries = c.entries[:cap(c.entries)]
|
||||
for len(c.entries) <= offset {
|
||||
c.entries = append(c.entries, nil)
|
||||
}
|
||||
}
|
||||
|
||||
tc = &storeEntry{}
|
||||
c.tokens[offset] = tc
|
||||
tc = &storeEntry{
|
||||
match: &idSet{},
|
||||
noMatch: &idSet{},
|
||||
}
|
||||
|
||||
c.entries[offset] = tc
|
||||
}
|
||||
|
||||
if n == nil {
|
||||
for _, i := range tc.match {
|
||||
if i.name == name {
|
||||
return
|
||||
}
|
||||
if tc.match.has(id) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, i := range tc.noMatch {
|
||||
if i == name {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
tc.noMatch = append(tc.noMatch, name)
|
||||
tc.noMatch.set(id)
|
||||
return
|
||||
}
|
||||
|
||||
for _, i := range tc.match {
|
||||
if i.name == name {
|
||||
if n.tokenLength() > i.node.tokenLength() {
|
||||
i.node = n
|
||||
tc.match.set(id)
|
||||
for i, ni := range tc.nodes {
|
||||
if ni.id == id {
|
||||
if n.tokenLength() > ni.tokenLength() {
|
||||
tc.nodes[i] = n
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
tc.match = append(tc.match, &storedItem{
|
||||
name: name,
|
||||
node: n,
|
||||
})
|
||||
tc.nodes = append(tc.nodes, n)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user