github.com/dubbogo/gost@v1.14.0/container/gxbucketpool/bucketpool.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  /*
    18   * Licensed to the Apache Software Foundation (ASF) under one or more
    19   * contributor license agreements.  See the NOTICE file distributed with
    20   * this work for additional information regarding copyright ownership.
    21   * The ASF licenses this file to You under the Apache License, Version 2.0
    22   * (the "License"); you may not use this file except in compliance with
    23   * the License.  You may obtain a copy of the License at
    24   *
    25   *     http://www.apache.org/licenses/LICENSE-2.0
    26   *
    27   * Unless required by applicable law or agreed to in writing, software
    28   * distributed under the License is distributed on an "AS IS" BASIS,
    29   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    30   * See the License for the specific language governing permissions and
    31   * limitations under the License.
    32   */
    33  
    34  package gxbucketpool
    35  
    36  import (
    37  	"math"
    38  	"sync"
    39  )
    40  
    41  type sizedPool struct {
    42  	size int
    43  	pool sync.Pool
    44  }
    45  
    46  func newSizedPool(size int) *sizedPool {
    47  	return &sizedPool{
    48  		size: size,
    49  		pool: sync.Pool{
    50  			New: func() interface{} { return makeSlicePointer(size) },
    51  		},
    52  	}
    53  }
    54  
    55  // Pool is actually multiple pools which store buffers of specific size.
    56  // i.e. it can be three pools which return buffers 32K, 64K and 128K.
    57  type Pool struct {
    58  	minSize int
    59  	maxSize int
    60  	pools   []*sizedPool
    61  }
    62  
    63  // New returns Pool which has buckets from minSize to maxSize.
    64  // Buckets increase with the power of two, i.e with multiplier 2: [2b, 4b, 16b, ... , 1024b]
    65  // Last pool will always be capped to maxSize.
    66  func New(minSize, maxSize int) *Pool {
    67  	if maxSize < minSize {
    68  		panic("maxSize can't be less than minSize")
    69  	}
    70  	const multiplier = 2
    71  	var pools []*sizedPool
    72  	curSize := minSize
    73  	for curSize < maxSize {
    74  		pools = append(pools, newSizedPool(curSize))
    75  		curSize *= multiplier
    76  	}
    77  	pools = append(pools, newSizedPool(maxSize))
    78  	return &Pool{
    79  		minSize: minSize,
    80  		maxSize: maxSize,
    81  		pools:   pools,
    82  	}
    83  }
    84  
    85  func (p *Pool) findPool(size int) *sizedPool {
    86  	if size > p.maxSize {
    87  		return nil
    88  	}
    89  	idx := int(math.Ceil(math.Log2(float64(size) / float64(p.minSize))))
    90  	if idx < 0 {
    91  		idx = 0
    92  	}
    93  	if idx > len(p.pools)-1 {
    94  		return nil
    95  	}
    96  	return p.pools[idx]
    97  }
    98  
    99  // Get returns pointer to []byte which has len size.
   100  // If there is no bucket with buffers >= size, slice will be allocated.
   101  func (p *Pool) Get(size int) *[]byte {
   102  	sp := p.findPool(size)
   103  	if sp == nil {
   104  		return makeSlicePointer(size)
   105  	}
   106  	buf := sp.pool.Get().(*[]byte)
   107  	*buf = (*buf)[:size]
   108  	return buf
   109  }
   110  
   111  // Put returns pointer to slice to some bucket. Discards slice for which there is no bucket
   112  func (p *Pool) Put(b *[]byte) {
   113  	sp := p.findPool(cap(*b))
   114  	if sp == nil {
   115  		return
   116  	}
   117  	*b = (*b)[:cap(*b)]
   118  	sp.pool.Put(b)
   119  }
   120  
   121  func makeSlicePointer(size int) *[]byte {
   122  	data := make([]byte, size)
   123  	return &data
   124  }