diff --git a/internal/list/bench_test.go b/internal/list/bench_test.go index 79c426f..68e388c 100644 --- a/internal/list/bench_test.go +++ b/internal/list/bench_test.go @@ -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() + } + }) +} diff --git a/internal/list/listpack.go b/internal/list/listpack.go index b3d2368..440fd24 100644 --- a/internal/list/listpack.go +++ b/internal/list/listpack.go @@ -35,7 +35,7 @@ var ( */ type ListPack struct { compress bool - size uint16 + size uint32 data []byte } @@ -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 } } @@ -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++ @@ -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...)