mirror of
https://github.com/soheilhy/cmux.git
synced 2024-11-10 03:31:52 +08:00
Fix race in patricia tree
This commit fixes a major issue added in 703b087a
.
There are still wins on allocation though.
benchmark old ns/op new ns/op delta
BenchmarkCMuxConnHTTP1-4 783 836 +6.77%
BenchmarkCMuxConnHTTP2-4 895 806 -9.94%
BenchmarkCMuxConnHTTP1n2-4 1000 1026 +2.60%
BenchmarkCMuxConnHTTP2n1-4 916 961 +4.91%
benchmark old allocs new allocs delta
BenchmarkCMuxConnHTTP1-4 3 4 +33.33%
BenchmarkCMuxConnHTTP2-4 4 4 +0.00%
BenchmarkCMuxConnHTTP1n2-4 4 5 +25.00%
BenchmarkCMuxConnHTTP2n1-4 4 5 +25.00%
benchmark old bytes new bytes delta
BenchmarkCMuxConnHTTP1-4 272 280 +2.94%
BenchmarkCMuxConnHTTP2-4 304 304 +0.00%
BenchmarkCMuxConnHTTP1n2-4 304 312 +2.63%
BenchmarkCMuxConnHTTP2n1-4 304 312 +2.63%
This commit is contained in:
parent
f952454ed9
commit
fd01d3cc6c
18
patricia.go
18
patricia.go
@ -22,8 +22,8 @@ import (
|
|||||||
// patriciaTree is a simple patricia tree that handles []byte instead of string
|
// patriciaTree is a simple patricia tree that handles []byte instead of string
|
||||||
// and cannot be changed after instantiation.
|
// and cannot be changed after instantiation.
|
||||||
type patriciaTree struct {
|
type patriciaTree struct {
|
||||||
root *ptNode
|
root *ptNode
|
||||||
buf []byte // preallocated buffer to read data while matching
|
maxDepth int // max depth of the tree.
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPatriciaTree(bs ...[]byte) *patriciaTree {
|
func newPatriciaTree(bs ...[]byte) *patriciaTree {
|
||||||
@ -34,8 +34,8 @@ func newPatriciaTree(bs ...[]byte) *patriciaTree {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &patriciaTree{
|
return &patriciaTree{
|
||||||
root: newNode(bs),
|
root: newNode(bs),
|
||||||
buf: make([]byte, max+1),
|
maxDepth: max + 1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,13 +48,15 @@ func newPatriciaTreeString(strs ...string) *patriciaTree {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *patriciaTree) matchPrefix(r io.Reader) bool {
|
func (t *patriciaTree) matchPrefix(r io.Reader) bool {
|
||||||
n, _ := io.ReadFull(r, t.buf)
|
buf := make([]byte, t.maxDepth)
|
||||||
return t.root.match(t.buf[:n], true)
|
n, _ := io.ReadFull(r, buf)
|
||||||
|
return t.root.match(buf[:n], true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *patriciaTree) match(r io.Reader) bool {
|
func (t *patriciaTree) match(r io.Reader) bool {
|
||||||
n, _ := io.ReadFull(r, t.buf)
|
buf := make([]byte, t.maxDepth)
|
||||||
return t.root.match(t.buf[:n], false)
|
n, _ := io.ReadFull(r, buf)
|
||||||
|
return t.root.match(buf[:n], false)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ptNode struct {
|
type ptNode struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user