Skip to content

Commit

Permalink
chore: rename reuse and fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Apr 19, 2024
1 parent 33ae2c4 commit f157981
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
16 changes: 8 additions & 8 deletions nocopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)

Expand Down
6 changes: 3 additions & 3 deletions nocopy_linkbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
12 changes: 6 additions & 6 deletions nocopy_linkbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f157981

Please sign in to comment.