Skip to content

Commit

Permalink
fix: link buffer reuse mode check
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Mar 4, 2024
1 parent b62943e commit 33ae2c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions nocopy_linkbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ func (b *UnsafeLinkBuffer) readBinary(n int) (p []byte) {
// if readBinary use no-copy mode, it will cause more memory used but get higher memory access efficiently
// for example, if user's codec need to decode 10 strings and each have 100 bytes, here could help the codec
// no need to malloc 10 times and the string slice could have the compact memory allocation.
if b.read.getMode(reuseMask) {
if !b.read.getMode(reuseMask) {
return b.read.Next(n)
}
if n >= minReuseBytes && cap(b.read.buf) < block32k {
if n >= minReuseBytes && cap(b.read.buf) <= block32k {
b.read.setMode(reuseMask, false)
return b.read.Next(n)
}
Expand Down
28 changes: 22 additions & 6 deletions nocopy_linkbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ func BenchmarkCopyString(b *testing.B) {
func BenchmarkNoCopyRead(b *testing.B) {
totalSize := 0
minSize := 32
maxSize := minSize << 10
maxSize := minSize << 9
for size := minSize; size <= maxSize; size = size << 1 {
totalSize += size
}
Expand All @@ -655,14 +655,30 @@ func BenchmarkNoCopyRead(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var buffer = NewLinkBuffer(pagesize)
for pb.Next() {
_, _ = buffer.Malloc(totalSize)
_ = buffer.MallocAck(totalSize)
_ = buffer.Flush()
buf, err := buffer.Malloc(totalSize)
if len(buf) != totalSize || err != nil {
b.Fatal(err)
}
err = buffer.MallocAck(totalSize)
if err != nil {
b.Fatal(err)
}
err = buffer.Flush()
if err != nil {
b.Fatal(err)
}

for size := minSize; size <= maxSize; size = size << 1 {
_, _ = buffer.ReadBinary(size)
buf, err = buffer.ReadBinary(size)
if len(buf) != size || err != nil {
b.Fatal(err)
}
}
// buffer.Release will not reuse memory since we use no copy mode here
err = buffer.Release()
if err != nil {
b.Fatal(err)
}
_ = buffer.Release()
}
})
}

0 comments on commit 33ae2c4

Please sign in to comment.