From f20d24bbd2e5ab759cd695d8fbb84139b8997a0d Mon Sep 17 00:00:00 2001 From: zonesowhat Date: Mon, 21 Mar 2022 11:24:07 +0800 Subject: [PATCH] Update sync_pool.go Add returned values to be better use of the slice. For example, if the 3dr value returned is false , we should not call Free(). --- sync_pool.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sync_pool.go b/sync_pool.go index 458ee5e..8f8ce32 100644 --- a/sync_pool.go +++ b/sync_pool.go @@ -39,16 +39,19 @@ func NewSyncPool(minSize, maxSize, factor int) *SyncPool { } // Alloc try alloc a []byte from internal slab class if no free chunk in slab class Alloc will make one. -func (pool *SyncPool) Alloc(size int) []byte { +// 1st returned value indicate a slice caller can use +// 2nd returned value indicate the capacity of the slice +// 3rd returned value indicate whether the slice is allocated from this pool or not +func (pool *SyncPool) Alloc(size int) ([]byte, int, bool) { if size <= pool.maxSize { for i := 0; i < len(pool.classesSize); i++ { if pool.classesSize[i] >= size { mem := pool.classes[i].Get().(*[]byte) - return (*mem)[:size] + return (*mem)[:size], pool.classesSize[i], true } } } - return make([]byte, size) + return make([]byte, size), size, false } // Free release a []byte that alloc from Pool.Alloc.