Skip to content

Commit

Permalink
perf: optimize listpack compress memalloc
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Jul 12, 2024
1 parent 29ce167 commit 5f86952
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 14 additions & 0 deletions internal/list/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ func BenchmarkList(b *testing.B) {
}
})
}

func BenchmarkListPack(b *testing.B) {
b.Run("compress", func(b *testing.B) {
lp := NewListPack()
for i := 0; i < 1000; i++ {
lp.RPush("rotom")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
lp.Compress()
lp.Decompress()
}
})
}
13 changes: 6 additions & 7 deletions internal/list/listpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
*/
type ListPack struct {
compress bool
size uint16
size uint32
data []byte
}

Expand Down Expand Up @@ -70,14 +70,16 @@ func (lp *ListPack) RPop() (string, bool) {

func (lp *ListPack) Compress() {
if !lp.compress {
lp.data = encoder.EncodeAll(lp.data, make([]byte, 0, len(lp.data)/4))
dst := encoder.EncodeAll(lp.data, bpool.Get(len(lp.data))[:0])
bpool.Put(lp.data)
lp.data = slices.Clip(dst)
lp.compress = true
}
}

func (lp *ListPack) Decompress() {
if lp.compress {
lp.data, _ = decoder.DecodeAll(lp.data, nil)
lp.data, _ = decoder.DecodeAll(lp.data, bpool.Get(maxListPackSize)[:0])
lp.compress = false
}
}
Expand Down Expand Up @@ -157,7 +159,7 @@ func (it *lpIterator) Insert(datas ...string) {
}
return
}
var alloc []byte
alloc := bpool.Get(maxListPackSize)[:0]
for _, data := range datas {
alloc = appendEntry(alloc, data)
it.size++
Expand All @@ -179,9 +181,6 @@ func (it *lpIterator) RemoveNext() (string, bool) {
}

func appendEntry(dst []byte, data string) []byte {
if dst == nil {
dst = bpool.Get(maxListPackSize)[:0]
}
before := len(dst)
dst = appendUvarint(dst, len(data), false)
dst = append(dst, data...)
Expand Down

0 comments on commit 5f86952

Please sign in to comment.