Skip to content

Commit

Permalink
use array to decode instead of map
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Aug 23, 2020
1 parent da63a0d commit 4342ef6
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type decodeLayer struct {
nodes map[uint8]*decodeNode
nodes []*decodeNode
any bool
anyNode *decodeNode
}
Expand All @@ -26,7 +26,7 @@ func defaultDecodeLayer() *decodeLayer {

func newDecodeLayer(level int, opcodes ...[]*OPCode) *decodeLayer {
l := &decodeLayer{
nodes: map[uint8]*decodeNode{},
nodes: make([]*decodeNode, 256),
any: true,
}
for _, cc := range opcodes {
Expand Down Expand Up @@ -72,8 +72,8 @@ func (l *decodeLayer) put(level int, opcode *OPCode) {
if !c.match(d) {
continue
}
n, ok := l.nodes[d]
if !ok || s > n.score {
n := l.nodes[d]
if n == nil || s > n.score {
n = &decodeNode{score: s}
l.nodes[d] = n
}
Expand All @@ -85,11 +85,7 @@ func (l *decodeLayer) get(d uint8) *decodeNode {
if l.anyNode != nil {
return l.anyNode
}
n, ok := l.nodes[d]
if !ok {
return nil
}
return n
return l.nodes[d]
}

func (l *decodeLayer) mapTo() map[string]interface{} {
Expand Down Expand Up @@ -117,6 +113,9 @@ func (l *decodeLayer) forNodes(f func(*decodeNode)) {
}
if l.nodes != nil {
for _, n := range l.nodes {
if n == nil {
continue
}
f(n)
if n.next != nil {
n.next.forNodes(f)
Expand Down

0 comments on commit 4342ef6

Please sign in to comment.