-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use own buffer instead of bytes.Buffer
- Loading branch information
Showing
2 changed files
with
38 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,32 @@ | ||
package vimebu | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
) | ||
|
||
type buffer struct { | ||
f []byte | ||
} | ||
|
||
// bytesBufferPool is a simple pool to create or retrieve a [bytes.Buffer]. | ||
var bytesBufferPool = sync.Pool{ | ||
New: func() any { | ||
return new(bytes.Buffer) | ||
return &buffer{ | ||
f: make([]byte, 0, 64), | ||
} | ||
}, | ||
} | ||
|
||
// getBuffer acquires a [bytes.Buffer] from the pool. | ||
func getBuffer() *bytes.Buffer { | ||
return bytesBufferPool.Get().(*bytes.Buffer) | ||
func getBuffer() *buffer { | ||
return bytesBufferPool.Get().(*buffer) | ||
} | ||
|
||
// putBuffer resets and returns a [bytes.Buffer] to the pool. | ||
func putBuffer(b *bytes.Buffer) { | ||
func putBuffer(b *buffer) { | ||
if b == nil { | ||
return | ||
} | ||
b.Reset() | ||
b.f = b.f[:0] | ||
bytesBufferPool.Put(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters