TODO: iter call 太多系统内存. 导致效率低下

This commit is contained in:
huangsimin 2019-04-02 18:28:41 +08:00
parent d93208f769
commit f0f16d7516
10 changed files with 169 additions and 136 deletions

View File

@ -28,10 +28,11 @@ type Tree struct {
root *Node root *Node
size int size int
Compare compare.Compare Compare compare.Compare
iter *Iterator
} }
func New(Compare compare.Compare) *Tree { func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare} return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
} }
func (tree *Tree) String() string { func (tree *Tree) String() string {
@ -285,11 +286,12 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
case 1: case 1:
n = n.children[1] n = n.children[1]
case 0: case 0:
iter := NewIterator(n) // iter := NewIterator(n)
iter.Prev() tree.iter.SetNode(n)
for iter.Prev() { tree.iter.Prev()
if tree.Compare(iter.cur.value, n.value) == 0 { for tree.iter.Prev() {
n = iter.cur if tree.Compare(tree.iter.cur.value, n.value) == 0 {
n = tree.iter.cur
} else { } else {
break break
} }
@ -310,25 +312,29 @@ func (tree *Tree) Put(value interface{}) {
return return
} }
cur := tree.root for cur, c := tree.root, 0; ; {
parent := cur.parent c = tree.Compare(value, cur.value)
child := -1 if c == -1 {
if cur.children[0] == nil {
for { cur.children[0] = node
node.parent = cur
if cur == nil { if cur.height == 0 {
parent.children[child] = node tree.fixPutHeight(cur)
node.parent = parent }
if node.parent.height == 0 { return
tree.fixPutHeight(node.parent)
} }
return cur = cur.children[0]
} else {
if cur.children[1] == nil {
cur.children[1] = node
node.parent = cur
if cur.height == 0 {
tree.fixPutHeight(cur)
}
return
}
cur = cur.children[1]
} }
parent = cur
c := tree.Compare(value, cur.value)
child = (c + 2) / 2
cur = cur.children[child]
} }
} }

View File

@ -247,11 +247,11 @@ func TestPutStable(t *testing.T) {
// log.SetOutput(f) // log.SetOutput(f)
// 0-1 3 | 2-3 7-8 | 4-7 12-16 | 8-15 20-32 | 16-31 33-58 l := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 18, 19, 20, 21, 22, 30, 41, 41, 41} // 0-1 3 | 2-3 7-8 | 4-7 12-16 | 8-15 20-32 | 16-31 33-58 l := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 18, 19, 20, 21, 22, 30, 41, 41, 41}
tree := New(compare.Int) // tree := New(compare.Int)
for i := 0; i < 10; i++ { // for i := 0; i < 10; i++ {
tree.Put(randomdata.Number(0, 100)) // tree.Put(randomdata.Number(0, 100))
} // }
t.Error(tree.debugString()) // t.Error(tree.debugString())
// t.Error(tree.debugString(), tree.TraversalBreadth(), "\n", "-----------") // t.Error(tree.debugString(), tree.TraversalBreadth(), "\n", "-----------")
} }
@ -485,11 +485,20 @@ func BenchmarkGet(b *testing.B) {
l := loadTestData() l := loadTestData()
b.N = len(l) b.N = len(l)
for i := 0; i < b.N; i++ {
tree.Put(l[i])
}
b.ResetTimer() b.ResetTimer()
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ {
tree.Get(l[i]) execCount := 50
b.N = len(l) * execCount
for i := 0; i < execCount; i++ {
for _, v := range l {
tree.Get(v)
}
} }
} }
@ -526,7 +535,7 @@ func BenchmarkPut(b *testing.B) {
b.ResetTimer() b.ResetTimer()
b.StartTimer() b.StartTimer()
execCount := 50 execCount := 1000
b.N = len(l) * execCount b.N = len(l) * execCount
for i := 0; i < execCount; i++ { for i := 0; i < execCount; i++ {
tree := New(compare.Int) tree := New(compare.Int)

View File

@ -24,6 +24,17 @@ func NewIterator(n *Node) *Iterator {
return iter return iter
} }
func NewIteratorWithCap(n *Node, cap int) *Iterator {
iter := &Iterator{tstack: lastack.NewWithCap(cap)}
iter.up = n
return iter
}
func (iter *Iterator) SetNode(n *Node) {
iter.up = n
iter.tstack.Clear()
}
func (iter *Iterator) Value() interface{} { func (iter *Iterator) Value() interface{} {
return iter.cur.value return iter.cur.value
} }

View File

@ -292,29 +292,32 @@ func (tree *Tree) Put(value interface{}) {
return return
} }
cur := tree.root for cur, c := tree.root, 0; ; {
parent := cur.parent c = tree.Compare(value, cur.value)
child := -1
for {
if cur == nil {
parent.children[child] = node
node.parent = parent
if node.parent.height == 0 {
tree.fixPutHeight(node.parent)
}
return
}
parent = cur
c := tree.Compare(value, cur.value)
if c == 0 { if c == 0 {
cur.value = value cur.value = value
return return
} else if c == -1 {
if cur.children[0] == nil {
cur.children[0] = node
node.parent = cur
if cur.height == 0 {
tree.fixPutHeight(cur)
}
return
}
cur = cur.children[0]
} else {
if cur.children[1] == nil {
cur.children[1] = node
node.parent = cur
if cur.height == 0 {
tree.fixPutHeight(cur)
}
return
}
cur = cur.children[1]
} }
child = (c + 2) / 2
cur = cur.children[child]
} }
} }

View File

@ -397,11 +397,20 @@ func BenchmarkGet(b *testing.B) {
l := loadTestData() l := loadTestData()
b.N = len(l) b.N = len(l)
for i := 0; i < b.N; i++ {
tree.Put(l[i])
}
b.ResetTimer() b.ResetTimer()
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ {
tree.Get(l[i]) execCount := 500
b.N = len(l) * execCount
b.Log(tree.size)
for i := 0; i < execCount; i++ {
for _, v := range l {
tree.Get(v)
}
} }
} }
@ -438,7 +447,7 @@ func BenchmarkPut(b *testing.B) {
b.ResetTimer() b.ResetTimer()
b.StartTimer() b.StartTimer()
execCount := 50 execCount := 1000
b.N = len(l) * execCount b.N = len(l) * execCount
for i := 0; i < execCount; i++ { for i := 0; i < execCount; i++ {
tree := New(compare.Int) tree := New(compare.Int)

View File

@ -438,7 +438,7 @@ func BenchmarkPut(b *testing.B) {
b.ResetTimer() b.ResetTimer()
b.StartTimer() b.StartTimer()
execCount := 50 execCount := 5
b.N = len(l) * execCount b.N = len(l) * execCount
for i := 0; i < execCount; i++ { for i := 0; i < execCount; i++ {
tree := New(compare.Int) tree := New(compare.Int)

View File

@ -60,10 +60,18 @@ func New() *Stack {
return s return s
} }
func NewWithCap(cap int) *Stack {
s := &Stack{}
s.size = 0
s.top = &Node{elements: make([]interface{}, cap, cap), cur: -1}
return s
}
func (as *Stack) Clear() { func (as *Stack) Clear() {
as.size = 0 as.size = 0
as.top = &Node{elements: make([]interface{}, 8, 8), cur: -1}
as.cache = nil as.top.down = nil
as.top.cur = -1
} }
func (as *Stack) Empty() bool { func (as *Stack) Empty() bool {

View File

@ -8,34 +8,34 @@ import (
"github.com/Pallinder/go-randomdata" "github.com/Pallinder/go-randomdata"
) )
// func TestPush(t *testing.T) { func TestPush(t *testing.T) {
// s := New() s := New()
// for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
// v := randomdata.Number(0, 10) v := randomdata.Number(0, 10)
// s.Push(v) s.Push(v)
// t.Error(v) t.Error(v)
// t.Error(s.String(), " size ", s.size) t.Error(s.String(), " size ", s.size)
// } }
// t.Error(s.Values()) t.Error(s.Values())
// for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
// v, ok := s.Pop() v, ok := s.Pop()
// t.Error(v, ok) t.Error(v, ok)
// t.Error(s.String(), " size ", s.size) t.Error(s.String(), " size ", s.size)
// } }
// for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
// v := randomdata.Number(0, 10) v := randomdata.Number(0, 10)
// s.Push(v) s.Push(v)
// } }
// for i := -1; i < 11; i++ { for i := -1; i < 11; i++ {
// v, ok := s.Get(i) v, ok := s.Get(i)
// t.Error(v, ok) t.Error(v, ok)
// } }
// } }
func BenchmarkGet(b *testing.B) { func BenchmarkGet(b *testing.B) {
s := New() s := New()

View File

@ -421,38 +421,48 @@ func (tree *Tree) Put(key interface{}) {
return return
} }
cur := tree.root for cur := tree.root; ; {
parent := cur.parent
child := -1
for {
if cur == nil {
parent.children[child] = node
node.parent = parent
fixed := parent.parent
fsize := getSize(fixed)
if fsize == 3 {
lefts, rigths := getChildrenSize(fixed)
tree.fix3Size(fixed, lefts, rigths)
}
return
}
if cur.size > 8 { if cur.size > 8 {
ls, rs := cur.children[0].size, cur.children[1].size
factor := cur.size / 10 // or factor = 1 factor := cur.size / 10 // or factor = 1
if rs >= ls*2+factor || ls >= rs*2+factor { if cur.children[1].size >= cur.children[0].size*2+factor || cur.children[0].size >= cur.children[1].size*2+factor {
tree.fixSize(cur, ls, rs) tree.fixSize(cur)
} }
} }
cur.size++ cur.size++
parent = cur
c := tree.Compare(key, cur.value) c := tree.Compare(key, cur.value)
child = (c + 2) / 2 if c < 0 {
cur = cur.children[child] if cur.children[0] == nil {
cur.children[0] = node
node.parent = cur
if cur.parent != nil && cur.parent.size == 3 {
if cur.parent.children[0] == nil {
tree.lrrotate3(cur.parent)
} else {
tree.rrotate3(cur.parent)
}
}
return
}
cur = cur.children[0]
} else {
if cur.children[1] == nil {
cur.children[1] = node
node.parent = cur
if cur.parent != nil && cur.parent.size == 3 {
if cur.parent.children[1] == nil {
tree.rlrotate3(cur.parent)
} else {
tree.lrotate3(cur.parent)
}
}
return
}
cur = cur.children[1]
}
} }
} }
@ -850,48 +860,25 @@ func (tree *Tree) fixSizeWithRemove(cur *Node) {
for cur != nil { for cur != nil {
cur.size-- cur.size--
if cur.size > 8 { if cur.size > 8 {
ls, rs := getChildrenSize(cur)
factor := cur.size / 10 // or factor = 1 factor := cur.size / 10 // or factor = 1
if rs >= ls*2+factor || ls >= rs*2+factor { if cur.children[1].size >= cur.children[0].size*2+factor || cur.children[0].size >= cur.children[1].size*2+factor {
tree.fixSize(cur, ls, rs) tree.fixSize(cur)
} }
} }
cur = cur.parent cur = cur.parent
} }
} }
func (tree *Tree) fix3Size(cur *Node, lefts, rigths int) { func (tree *Tree) fixSize(cur *Node) {
if lefts > rigths { if cur.children[0].size > cur.children[1].size {
l := cur.children[0] llsize, lrsize := getChildrenSize(cur.children[0])
llsize, lrsize := getChildrenSize(l)
if lrsize > llsize {
tree.rlrotate3(cur)
} else {
tree.rrotate3(cur)
}
} else {
r := cur.children[1]
rlsize, rrsize := getChildrenSize(r)
if rlsize > rrsize {
tree.lrrotate3(cur)
} else {
tree.lrotate3(cur)
}
}
}
func (tree *Tree) fixSize(cur *Node, lefts, rigths int) {
if lefts > rigths {
l := cur.children[0]
llsize, lrsize := getChildrenSize(l)
if lrsize > llsize { if lrsize > llsize {
tree.rlrotate(cur) tree.rlrotate(cur)
} else { } else {
tree.rrotate(cur) tree.rrotate(cur)
} }
} else { } else {
r := cur.children[1] rlsize, rrsize := getChildrenSize(cur.children[1])
rlsize, rrsize := getChildrenSize(r)
if rlsize > rrsize { if rlsize > rrsize {
tree.lrrotate(cur) tree.lrrotate(cur)
} else { } else {

View File

@ -17,10 +17,10 @@ import (
"github.com/emirpasic/gods/trees/redblacktree" "github.com/emirpasic/gods/trees/redblacktree"
) )
const CompareSize = 1000000 const CompareSize = 5000
const NumberMax = 50000000 const NumberMax = 50000000
func Save(t *testing.T) { func TestSave(t *testing.T) {
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil { if err != nil {
@ -552,7 +552,7 @@ func BenchmarkGet(b *testing.B) {
b.ResetTimer() b.ResetTimer()
b.StartTimer() b.StartTimer()
execCount := 10 execCount := 50
b.N = len(l) * execCount b.N = len(l) * execCount
for i := 0; i < execCount; i++ { for i := 0; i < execCount; i++ {
@ -612,7 +612,7 @@ func BenchmarkPut(b *testing.B) {
b.ResetTimer() b.ResetTimer()
b.StartTimer() b.StartTimer()
execCount := 10 execCount := 1000
b.N = len(l) * execCount b.N = len(l) * execCount
for i := 0; i < execCount; i++ { for i := 0; i < execCount; i++ {
tree := New(compare.Int) tree := New(compare.Int)