Skip to content

Commit

Permalink
opt: replace the buffer pool with byte slice pool in linked-list buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jan 18, 2022
1 parent 90ac45d commit 3631fa2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 134 deletions.
14 changes: 7 additions & 7 deletions pkg/buffer/elastic/elastic_ring_list_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (mb *Buffer) Read(p []byte) (n int, err error) {
// Peek returns n bytes as [][]byte, these bytes won't be discarded until Buffer.Discard() is called.
func (mb *Buffer) Peek(n int) [][]byte {
if mb.ringBuffer == nil {
return mb.listBuffer.PeekBytesList(n)
return mb.listBuffer.Peek(n)
}

if n <= 0 {
Expand All @@ -74,7 +74,7 @@ func (mb *Buffer) Peek(n int) [][]byte {
if mb.ringBuffer.Buffered() >= n {
return [][]byte{head, tail}
}
return mb.listBuffer.PeekBytesListWithBytes(n, head, tail)
return mb.listBuffer.PeekWithBytes(n, head, tail)
}

// Discard discards n bytes in this buffer.
Expand Down Expand Up @@ -108,14 +108,14 @@ func (mb *Buffer) Write(p []byte) (n int, err error) {
}

if !mb.listBuffer.IsEmpty() || mb.ringBuffer.Buffered() >= mb.maxStaticBytes {
mb.listBuffer.PushBytesBack(p)
mb.listBuffer.PushBack(p)
return len(p), nil
}
if mb.ringBuffer.Len() >= mb.maxStaticBytes {
writable := mb.ringBuffer.Available()
if n = len(p); n > writable {
_, _ = mb.ringBuffer.Write(p[:writable])
mb.listBuffer.PushBytesBack(p[writable:])
mb.listBuffer.PushBack(p[writable:])
return
}
}
Expand All @@ -131,7 +131,7 @@ func (mb *Buffer) Writev(bs [][]byte) (int, error) {
if !mb.listBuffer.IsEmpty() || mb.ringBuffer.Buffered() >= mb.maxStaticBytes {
var n int
for _, b := range bs {
mb.listBuffer.PushBytesBack(b)
mb.listBuffer.PushBack(b)
n += len(b)
}
return n, nil
Expand All @@ -147,15 +147,15 @@ func (mb *Buffer) Writev(bs [][]byte) (int, error) {
cum += len(b)
if len(b) > writable {
_, _ = mb.ringBuffer.Write(b[:writable])
mb.listBuffer.PushBytesBack(b[writable:])
mb.listBuffer.PushBack(b[writable:])
break
}
n, _ := mb.ringBuffer.Write(b)
writable -= n
}
for pos++; pos < len(bs); pos++ {
cum += len(bs[pos])
mb.listBuffer.PushBytesBack(bs[pos])
mb.listBuffer.PushBack(bs[pos])
}
return cum, nil
}
Expand Down
Loading

0 comments on commit 3631fa2

Please sign in to comment.