aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchrislu <chris.lu@gmail.com>2022-03-02 13:50:28 -0800
committerchrislu <chris.lu@gmail.com>2022-03-02 13:50:28 -0800
commit784583afc6513f2083190a1e0765cf181b88cba6 (patch)
tree62b2bb614a8f67100e6911a69c195fc503e2e73b
parentba14307319a9813264154b19ff71f2bfb041fbac (diff)
downloadseaweedfs-784583afc6513f2083190a1e0765cf181b88cba6.tar.xz
seaweedfs-784583afc6513f2083190a1e0765cf181b88cba6.zip
avoid pool memory allocation if too large
-rw-r--r--weed/util/mem/slot_pool.go14
1 files changed, 7 insertions, 7 deletions
diff --git a/weed/util/mem/slot_pool.go b/weed/util/mem/slot_pool.go
index f7aa86e05..b3493febf 100644
--- a/weed/util/mem/slot_pool.go
+++ b/weed/util/mem/slot_pool.go
@@ -33,17 +33,18 @@ func init() {
}
}
-func getSlotPool(size int) *sync.Pool {
+func getSlotPool(size int) (*sync.Pool, bool) {
index := bitCount(size)
- return pools[index]
+ if index >= len(pools) {
+ return nil, false
+ }
+ return pools[index], true
}
var total int64
func Allocate(size int) []byte {
- pool := getSlotPool(size)
- if pool != nil {
-
+ if pool, found := getSlotPool(size); found {
newVal := atomic.AddInt64(&total, 1)
glog.V(4).Infof("++> %d", newVal)
@@ -54,8 +55,7 @@ func Allocate(size int) []byte {
}
func Free(buf []byte) {
- pool := getSlotPool(cap(buf))
- if pool != nil {
+ if pool, found := getSlotPool(cap(buf)); found {
newVal := atomic.AddInt64(&total, -1)
glog.V(4).Infof("--> %d", newVal)
pool.Put(&buf)