diff --git a/nocopy_linkbuffer.go b/nocopy_linkbuffer.go index 879547de..f399765d 100644 --- a/nocopy_linkbuffer.go +++ b/nocopy_linkbuffer.go @@ -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) } diff --git a/nocopy_linkbuffer_test.go b/nocopy_linkbuffer_test.go index 2225f623..504ff2ab 100644 --- a/nocopy_linkbuffer_test.go +++ b/nocopy_linkbuffer_test.go @@ -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 } @@ -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() } }) }