Skip to content

Commit

Permalink
cache allocated buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Oct 9, 2023
1 parent 7019692 commit aecdebb
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const defaultBufSize = 4096 // must be 2^n
// const maxCachedBufSize = 256 * 1024
const maxCachedBufSize = 256 * 1024

type writeBuffer struct {
buf []byte
Expand Down Expand Up @@ -39,9 +39,10 @@ func (wb *writeBuffer) store(buf []byte) {
}

type readBuffer struct {
buf []byte
idx int
nc net.Conn
buf []byte
idx int
nc net.Conn
cached []byte
}

func newReadBuffer(nc net.Conn) readBuffer {
Expand All @@ -54,12 +55,16 @@ func newReadBuffer(nc net.Conn) readBuffer {
// fill reads into the buffer until at least _need_ bytes are in it.
func (rb *readBuffer) fill(need int) error {
var buf []byte
if need <= cap(rb.buf) {
buf = rb.buf[:0]
if need <= cap(rb.cached) {
buf = rb.cached[:0]
} else {
// Round up to the next multiple of the default size
size := (need + defaultBufSize - 1) &^ (defaultBufSize - 1)

buf = make([]byte, 0, size)
if size <= maxCachedBufSize {
rb.cached = buf
}
}

// move the existing data to the start of it.
Expand Down

0 comments on commit aecdebb

Please sign in to comment.