mirror of
https://github.com/valyala/bytebufferpool.git
synced 2026-06-14 13:26:35 +03:00
renaming: AcquireByteBuffer -> Acquire, ReleaseByteBuffer -> Release
This commit is contained in:
+6
-6
@@ -6,7 +6,7 @@ package bytebufferpool
|
|||||||
// ByteBuffer may be used with functions appending data to the given []byte
|
// ByteBuffer may be used with functions appending data to the given []byte
|
||||||
// slice. See example code for details.
|
// slice. See example code for details.
|
||||||
//
|
//
|
||||||
// Use AcquireByteBuffer for obtaining an empty byte buffer.
|
// Use Acquire for obtaining an empty byte buffer.
|
||||||
type ByteBuffer struct {
|
type ByteBuffer struct {
|
||||||
|
|
||||||
// B is a byte buffer to use in append-like workloads.
|
// B is a byte buffer to use in append-like workloads.
|
||||||
@@ -41,20 +41,20 @@ func (b *ByteBuffer) Reset() {
|
|||||||
b.B = b.B[:0]
|
b.B = b.B[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// AcquireByteBuffer returns an empty byte buffer from the pool.
|
// Acquire returns an empty byte buffer from the pool.
|
||||||
//
|
//
|
||||||
// Acquired byte buffer may be returned to the pool via ReleaseByteBuffer call.
|
// Acquired byte buffer may be returned to the pool via Release call.
|
||||||
// This reduces the number of memory allocations required for byte buffer
|
// This reduces the number of memory allocations required for byte buffer
|
||||||
// management.
|
// management.
|
||||||
func AcquireByteBuffer() *ByteBuffer {
|
func Acquire() *ByteBuffer {
|
||||||
return defaultByteBufferPool.Acquire()
|
return defaultByteBufferPool.Acquire()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReleaseByteBuffer returns byte buffer to the pool.
|
// Release returns byte buffer to the pool.
|
||||||
//
|
//
|
||||||
// ByteBuffer.B mustn't be touched after returning it to the pool.
|
// ByteBuffer.B mustn't be touched after returning it to the pool.
|
||||||
// Otherwise data races will occur.
|
// Otherwise data races will occur.
|
||||||
func ReleaseByteBuffer(b *ByteBuffer) {
|
func Release(b *ByteBuffer) {
|
||||||
defaultByteBufferPool.Release(b)
|
defaultByteBufferPool.Release(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func ExampleByteBuffer() {
|
func ExampleByteBuffer() {
|
||||||
bb := bytebufferpool.AcquireByteBuffer()
|
bb := bytebufferpool.Acquire()
|
||||||
|
|
||||||
bb.WriteString("first line\n")
|
bb.WriteString("first line\n")
|
||||||
bb.Write([]byte("second line\n"))
|
bb.Write([]byte("second line\n"))
|
||||||
@@ -17,5 +17,5 @@ func ExampleByteBuffer() {
|
|||||||
|
|
||||||
// It is safe to release byte buffer now, since it is
|
// It is safe to release byte buffer now, since it is
|
||||||
// no longer used.
|
// no longer used.
|
||||||
bytebufferpool.ReleaseByteBuffer(bb)
|
bytebufferpool.Release(bb)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -32,12 +32,12 @@ func TestByteBufferAcquireReleaseConcurrent(t *testing.T) {
|
|||||||
func testByteBufferAcquireRelease(t *testing.T) {
|
func testByteBufferAcquireRelease(t *testing.T) {
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
expectedS := fmt.Sprintf("num %d", i)
|
expectedS := fmt.Sprintf("num %d", i)
|
||||||
b := AcquireByteBuffer()
|
b := Acquire()
|
||||||
b.B = append(b.B, "num "...)
|
b.B = append(b.B, "num "...)
|
||||||
b.B = append(b.B, fmt.Sprintf("%d", i)...)
|
b.B = append(b.B, fmt.Sprintf("%d", i)...)
|
||||||
if string(b.B) != expectedS {
|
if string(b.B) != expectedS {
|
||||||
t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
|
t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
|
||||||
}
|
}
|
||||||
ReleaseByteBuffer(b)
|
Release(b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -42,12 +42,12 @@ func testPoolVariousSizes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testAcquireRelease(t *testing.T, n int) {
|
func testAcquireRelease(t *testing.T, n int) {
|
||||||
bb := AcquireByteBuffer()
|
bb := Acquire()
|
||||||
if len(bb.B) > 0 {
|
if len(bb.B) > 0 {
|
||||||
t.Fatalf("non-empty byte buffer returned from acquire")
|
t.Fatalf("non-empty byte buffer returned from acquire")
|
||||||
}
|
}
|
||||||
bb.B = allocNBytes(bb.B, n)
|
bb.B = allocNBytes(bb.B, n)
|
||||||
ReleaseByteBuffer(bb)
|
Release(bb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func allocNBytes(dst []byte, n int) []byte {
|
func allocNBytes(dst []byte, n int) []byte {
|
||||||
|
|||||||
Reference in New Issue
Block a user