From e4bf620795cad98862467f7ed74d76440d6c6e1e Mon Sep 17 00:00:00 2001 From: hmol233 <82594500+hmol233@users.noreply.github.com> Date: Sat, 10 Jul 2021 16:41:23 +0800 Subject: [PATCH] minor tweak --- common/buf/buffer.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/common/buf/buffer.go b/common/buf/buffer.go index baca8238..4878b0b6 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -24,7 +24,7 @@ type Buffer struct { UDP *net.Destination } -// New creates a Buffer with 0 length and 2K capacity. +// New creates a Buffer with 0 length and 8K capacity. func New() *Buffer { buf := pool.Get().([]byte) if cap(buf) >= Size { @@ -57,8 +57,15 @@ func NewExisted(b []byte) *Buffer { // StackNew creates a new Buffer object on stack. // This method is for buffers that is released in the same function. func StackNew() Buffer { + buf := pool.Get().([]byte) + if cap(buf) >= Size { + buf = buf[:Size] + } else { + buf = make([]byte, Size) + } + return Buffer{ - v: pool.Get().([]byte), + v: buf, } } @@ -72,7 +79,7 @@ func (b *Buffer) Release() { b.v = nil b.Clear() - if cap(p) >= Size { + if cap(p) == Size { pool.Put(p) } b.UDP = nil