diff --git a/patricia.go b/patricia.go index 0099e38..a38cae0 100644 --- a/patricia.go +++ b/patricia.go @@ -22,8 +22,8 @@ import ( // patriciaTree is a simple patricia tree that handles []byte instead of string // and cannot be changed after instantiation. type patriciaTree struct { - root *ptNode - buf []byte // preallocated buffer to read data while matching + root *ptNode + maxDepth int // max depth of the tree. } func newPatriciaTree(bs ...[]byte) *patriciaTree { @@ -34,8 +34,8 @@ func newPatriciaTree(bs ...[]byte) *patriciaTree { } } return &patriciaTree{ - root: newNode(bs), - buf: make([]byte, max+1), + root: newNode(bs), + maxDepth: max + 1, } } @@ -48,13 +48,15 @@ func newPatriciaTreeString(strs ...string) *patriciaTree { } func (t *patriciaTree) matchPrefix(r io.Reader) bool { - n, _ := io.ReadFull(r, t.buf) - return t.root.match(t.buf[:n], true) + buf := make([]byte, t.maxDepth) + n, _ := io.ReadFull(r, buf) + return t.root.match(buf[:n], true) } func (t *patriciaTree) match(r io.Reader) bool { - n, _ := io.ReadFull(r, t.buf) - return t.root.match(t.buf[:n], false) + buf := make([]byte, t.maxDepth) + n, _ := io.ReadFull(r, buf) + return t.root.match(buf[:n], false) } type ptNode struct {