diff --git a/internal/caches/memory_fragment_pool.go b/internal/caches/memory_fragment_pool.go index 7de47f3..b556e8e 100644 --- a/internal/caches/memory_fragment_pool.go +++ b/internal/caches/memory_fragment_pool.go @@ -126,9 +126,7 @@ func (this *MemoryFragmentPool) Get(expectSize int64) (resultBytes []byte, ok bo return } - if expectSize < minMemoryFragmentPoolItemSize { - return - } + // DO NOT check min segment size this.mu.RLock() diff --git a/internal/caches/writer_memory.go b/internal/caches/writer_memory.go index 93e5795..a9bf54f 100644 --- a/internal/caches/writer_memory.go +++ b/internal/caches/writer_memory.go @@ -32,23 +32,20 @@ func NewMemoryWriter(memoryStorage *MemoryStorage, key string, expiredAt int64, ModifiedAt: fasttime.Now().Unix(), Status: status, } - if expectedBodySize > 0 { + if expectedBodySize > 0 && expectedBodySize <= maxMemoryFragmentPoolItemSize { bodyBytes, ok := SharedFragmentMemoryPool.Get(expectedBodySize) // try to reuse memory if ok { valueItem.BodyValue = bodyBytes + valueItem.IsPrepared = true } else { - if isDirty { - if expectedBodySize >= minMemoryFragmentPoolItemSize { - SharedFragmentMemoryPool.IncreaseNew() - } + if expectedBodySize <= (16 << 20) { var allocSize = (expectedBodySize/16384 + 1) * 16384 valueItem.BodyValue = make([]byte, allocSize)[:expectedBodySize] - } else { - valueItem.BodyValue = make([]byte, expectedBodySize) + valueItem.IsPrepared = true + + SharedFragmentMemoryPool.IncreaseNew() } } - - valueItem.IsPrepared = true } var w = &MemoryWriter{ storage: memoryStorage, @@ -170,6 +167,13 @@ func (this *MemoryWriter) Discard() error { this.storage.locker.Lock() delete(this.storage.valuesMap, this.hash) + + if this.item != nil && + !this.item.isReferring && + cap(this.item.BodyValue) >= minMemoryFragmentPoolItemSize { + SharedFragmentMemoryPool.Put(this.item.BodyValue) + } + this.storage.locker.Unlock() return nil }