Skip to content

Commit

Permalink
refactor: refact listpack
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Jul 10, 2024
1 parent 9751435 commit b76f75a
Show file tree
Hide file tree
Showing 6 changed files with 792 additions and 958 deletions.
24 changes: 0 additions & 24 deletions internal/dict/utils.go

This file was deleted.

85 changes: 11 additions & 74 deletions internal/list/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,30 @@
package list

import (
"fmt"
"testing"
)

func BenchmarkList(b *testing.B) {
const N = 10000
b.Run("lpush", func(b *testing.B) {
ls := New()
for i := 0; i < b.N; i++ {
ls.LPush(genKey(i))
}
})
b.Run("rpush", func(b *testing.B) {
ls := New()
for i := 0; i < b.N; i++ {
ls.RPush(genKey(i))
}
})
b.Run("lpop", func(b *testing.B) {
ls := genList(0, b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls.LPop()
}
})
b.Run("rpop", func(b *testing.B) {
ls := genList(0, b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls.RPop()
}
})
b.Run("index", func(b *testing.B) {
ls := genList(0, N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls.Index(i % N)
}
})
b.Run("set", func(b *testing.B) {
ls := genList(0, N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls.Set(i%N, genKey(N-i))
}
})
b.Run("range", func(b *testing.B) {
ls := genList(0, N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls.Range(0, -1, func(s []byte) (stop bool) {
return false
})
}
})
b.Run("revrange", func(b *testing.B) {
ls := genList(0, N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls.RevRange(0, -1, func(s []byte) (stop bool) {
return false
})
}
})
}

func BenchmarkListPack(b *testing.B) {
const N = 1000
b.Run("set/same-len", func(b *testing.B) {
ls := genListPack(0, N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls.Set(i%N, fmt.Sprintf("%08x", i))
}
})
b.Run("set/less-len", func(b *testing.B) {
b.Run("next", func(b *testing.B) {
ls := genListPack(0, N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls.Set(i%N, fmt.Sprintf("%07x", i))
it := ls.NewIterator()
for i := 0; i < N; i++ {
it.Next()
}
}
})
b.Run("set/great-len", func(b *testing.B) {
b.Run("prev", func(b *testing.B) {
ls := genListPack(0, N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ls.Set(i%N, fmt.Sprintf("%09x", i))
it := ls.NewIterator()
it.SeekEnd()
for i := 0; i < N; i++ {
it.Prev()
}
}
})
}
148 changes: 72 additions & 76 deletions internal/list/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package list

import (
"math"
)

// +------------------------------ QuickList -----------------------------+
// | +-----------+ +-----------+ +-----------+ |
// head --- | listpack0 | <-> | listpack1 | <-> ... <-> | listpackN | --- tail
Expand Down Expand Up @@ -42,7 +38,7 @@ func (ls *QuickList) LPush(key string) {
ls.head.prev = n
ls.head = n
}
ls.head.Insert(0, key)
// ls.head.Insert(0, key)
}

// RPush
Expand All @@ -53,7 +49,7 @@ func (ls *QuickList) RPush(key string) {
n.prev = ls.tail
ls.tail = n
}
ls.tail.Insert(-1, key)
// ls.tail.Insert(-1, key)
}

// Index
Expand All @@ -74,7 +70,7 @@ func (ls *QuickList) LPop() (string, bool) {
func (ls *QuickList) RPop() (key string, ok bool) {
for lp := ls.tail; lp != nil; lp = lp.prev {
if lp.size > 0 {
return lp.Remove(-1)
// return lp.Remove(-1)
}
ls.free(lp)
}
Expand Down Expand Up @@ -102,38 +98,38 @@ func (ls *QuickList) find(index int) (*Node, int) {

// Set
func (ls *QuickList) Set(index int, key string) bool {
lp, indexInternal := ls.find(index)
if lp != nil {
return lp.Set(indexInternal, key)
}
// lp, indexInternal := ls.find(index)
// if lp != nil {
// return lp.Set(indexInternal, key)
// }
return false
}

// Remove
func (ls *QuickList) Remove(index int) (val string, ok bool) {
lp, indexInternal := ls.find(index)
if lp != nil {
val, ok = lp.Remove(indexInternal)
ls.free(lp)
}
// lp, indexInternal := ls.find(index)
// if lp != nil {
// val, ok = lp.Remove(indexInternal)
// ls.free(lp)
// }
return
}

// RemoveFirst
func (ls *QuickList) RemoveFirst(key string) (res int, ok bool) {
for lp := ls.head; lp != nil; lp = lp.next {
if lp.size == 0 {
ls.free(lp)

} else {
n, ok := lp.RemoveFirst(key)
if ok {
return res + n, true
} else {
res += lp.Size()
}
}
}
// for lp := ls.head; lp != nil; lp = lp.next {
// if lp.size == 0 {
// ls.free(lp)

// } else {
// n, ok := lp.RemoveFirst(key)
// if ok {
// return res + n, true
// } else {
// res += lp.Size()
// }
// }
// }
return 0, false
}

Expand All @@ -148,56 +144,56 @@ func (ls *QuickList) Size() (n int) {
type lsIterator func(data []byte) (stop bool)

func (ls *QuickList) iterFront(start, end int, f lsIterator) {
count := end - start
if end == -1 {
count = math.MaxInt
}
if start < 0 || count < 0 {
return
}

lp, indexInternal := ls.find(start)

var stop bool
for !stop && count > 0 && lp != nil {
lp.Range(indexInternal, -1, func(data []byte, _ int) bool {
stop = f(data)
count--
return stop || count == 0
})
lp = lp.next
indexInternal = 0
}
// count := end - start
// if end == -1 {
// count = math.MaxInt
// }
// if start < 0 || count < 0 {
// return
// }

// lp, indexInternal := ls.find(start)

// var stop bool
// for !stop && count > 0 && lp != nil {
// lp.Range(indexInternal, -1, func(data []byte, _ int) bool {
// stop = f(data)
// count--
// return stop || count == 0
// })
// lp = lp.next
// indexInternal = 0
// }
}

func (ls *QuickList) iterBack(start, end int, f lsIterator) {
count := end - start
if end == -1 {
count = math.MaxInt
}
if start < 0 || count < 0 {
return
}

lp := ls.tail
for start > lp.Size() {
start -= lp.Size()
lp = lp.prev
if lp == nil {
return
}
}

var stop bool
for !stop && count > 0 && lp != nil {
lp.RevRange(start, -1, func(data []byte, _ int) bool {
stop = f(data)
count--
return stop || count == 0
})
lp = lp.prev
start = 0
}
// count := end - start
// if end == -1 {
// count = math.MaxInt
// }
// if start < 0 || count < 0 {
// return
// }

// lp := ls.tail
// for start > lp.Size() {
// start -= lp.Size()
// lp = lp.prev
// if lp == nil {
// return
// }
// }

// var stop bool
// for !stop && count > 0 && lp != nil {
// lp.RevRange(start, -1, func(data []byte, _ int) bool {
// stop = f(data)
// count--
// return stop || count == 0
// })
// lp = lp.prev
// start = 0
// }
}

// Range
Expand Down
Loading

0 comments on commit b76f75a

Please sign in to comment.