From f157981802690f8f2072d9e6eb5707fe117ea19c Mon Sep 17 00:00:00 2001 From: wangzhuowei Date: Fri, 19 Apr 2024 14:27:44 +0800 Subject: [PATCH] chore: rename reuse and fix typo --- nocopy.go | 16 ++++++++-------- nocopy_linkbuffer.go | 6 +++--- nocopy_linkbuffer_test.go | 12 ++++++------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/nocopy.go b/nocopy.go index a87f1303..9bc351fe 100644 --- a/nocopy.go +++ b/nocopy.go @@ -112,9 +112,9 @@ type Reader interface { // The usage of the design is a two-step operation, first apply for a section of memory, // fill it and then submit. E.g: // -// var buf, _ = Malloc(n) -// buf = append(buf[:0], ...) -// Flush() +// var buf, _ = Malloc(n) +// buf = append(buf[:0], ...) +// Flush() // // Note that it is not recommended to submit self-managed buffers to Writer. // Since the writer is processed asynchronously, if the self-managed buffer is used and recycled after submission, @@ -257,11 +257,11 @@ const ( pagesize = block8k mallocMax = block8k * block1k // mallocMax is 8MB - defaultLinkBufferMode = 1 << 0 - minReuseBytes = 64 // only reuse bytes if n >= minReuseBytes - // reuse mode, indicate weather reuse buffer node data, default true - reuseMask uint8 = 1 << 0 // 0000 0001 - // read-only mode, introduced by Refer, WriteString, WriteBinary, etc., default false + minReuseBytes = 64 // only reuse bytes if n >= minReuseBytes + defaultLinkBufferMode = reusableMask // default buffer mode is reusable but not readonly + // reusable mode indicate to whether reuse buffer node data, default value is true + reusableMask uint8 = 1 << 0 // 0000 0001 + // read-only mode enable by Refer/WriteString/WriteBinary/etc. API, default value is false readonlyMask uint8 = 1 << 1 // 0000 0010 ) diff --git a/nocopy_linkbuffer.go b/nocopy_linkbuffer.go index f399765d..ab450507 100644 --- a/nocopy_linkbuffer.go +++ b/nocopy_linkbuffer.go @@ -221,11 +221,11 @@ 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(reusableMask) { return b.read.Next(n) } if n >= minReuseBytes && cap(b.read.buf) <= block32k { - b.read.setMode(reuseMask, false) + b.read.setMode(reusableMask, false) return b.read.Next(n) } // if the underlying buffer too large, we shouldn't use no-copy mode @@ -822,5 +822,5 @@ func (node *linkBufferNode) setMode(mask uint8, enable bool) { } func (node *linkBufferNode) reusable() bool { - return !(node.mode&reuseMask > 0 || node.mode&readonlyMask > 0) + return !(node.mode&reusableMask > 0 || node.mode&readonlyMask > 0) } diff --git a/nocopy_linkbuffer_test.go b/nocopy_linkbuffer_test.go index 504ff2ab..46e1d09b 100644 --- a/nocopy_linkbuffer_test.go +++ b/nocopy_linkbuffer_test.go @@ -468,16 +468,16 @@ func TestWriteDirect(t *testing.T) { func TestBufferMode(t *testing.T) { bufnode := newLinkBufferNode(0) - MustTrue(t, bufnode.getMode(reuseMask)) + MustTrue(t, bufnode.getMode(reusableMask)) MustTrue(t, bufnode.getMode(readonlyMask)) bufnode = newLinkBufferNode(1) - MustTrue(t, bufnode.getMode(reuseMask)) + MustTrue(t, bufnode.getMode(reusableMask)) MustTrue(t, !bufnode.getMode(readonlyMask)) - bufnode.setMode(reuseMask, false) - MustTrue(t, !bufnode.getMode(reuseMask)) - bufnode.setMode(reuseMask, true) - MustTrue(t, bufnode.getMode(reuseMask)) + bufnode.setMode(reusableMask, false) + MustTrue(t, !bufnode.getMode(reusableMask)) + bufnode.setMode(reusableMask, true) + MustTrue(t, bufnode.getMode(reusableMask)) } func BenchmarkLinkBufferConcurrentReadWrite(b *testing.B) {