github.com/m3db/m3@v1.5.0/src/x/generics/arraypool/pool.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package arraypool
    22  
    23  import (
    24  	"github.com/m3db/m3/src/x/pool"
    25  
    26  	"github.com/mauricelam/genny/generic"
    27  )
    28  
    29  // elemType is the generic type for use with the specialized array pool.
    30  type elemType generic.Type
    31  
    32  // elemArrayPool provides a pool for elemType slices.
    33  type elemArrayPool interface {
    34  	// Init initializes the array pool, it needs to be called
    35  	// before Get/Put use.
    36  	Init()
    37  
    38  	// Get returns the a slice from the pool.
    39  	Get() []elemType
    40  
    41  	// Put returns the provided slice to the pool.
    42  	Put(elems []elemType)
    43  }
    44  
    45  type elemFinalizeFn func([]elemType) []elemType
    46  
    47  type elemArrayPoolOpts struct {
    48  	Options     pool.ObjectPoolOptions
    49  	Capacity    int
    50  	MaxCapacity int
    51  	FinalizeFn  elemFinalizeFn
    52  }
    53  
    54  type elemArrPool struct {
    55  	opts elemArrayPoolOpts
    56  	pool pool.ObjectPool
    57  }
    58  
    59  // nolint
    60  func newElemArrayPool(opts elemArrayPoolOpts) elemArrayPool {
    61  	if opts.FinalizeFn == nil {
    62  		opts.FinalizeFn = defaultElemFinalizerFn
    63  	}
    64  	p := pool.NewObjectPool(opts.Options)
    65  	return &elemArrPool{opts, p}
    66  }
    67  
    68  func (p *elemArrPool) Init() {
    69  	p.pool.Init(func() interface{} {
    70  		return make([]elemType, 0, p.opts.Capacity)
    71  	})
    72  }
    73  
    74  func (p *elemArrPool) Get() []elemType {
    75  	return p.pool.Get().([]elemType)
    76  }
    77  
    78  func (p *elemArrPool) Put(arr []elemType) {
    79  	arr = p.opts.FinalizeFn(arr)
    80  	if max := p.opts.MaxCapacity; max > 0 && cap(arr) > max {
    81  		return
    82  	}
    83  	p.pool.Put(arr)
    84  }
    85  
    86  func defaultElemFinalizerFn(elems []elemType) []elemType {
    87  	var empty elemType
    88  	for i := range elems {
    89  		elems[i] = empty
    90  	}
    91  	elems = elems[:0]
    92  	return elems
    93  }
    94  
    95  // nolint
    96  type elemArr []elemType
    97  
    98  // nolint
    99  func (elems elemArr) grow(n int) []elemType {
   100  	if cap(elems) < n {
   101  		elems = make([]elemType, n)
   102  	}
   103  	elems = elems[:n]
   104  	// following compiler optimized memcpy impl
   105  	// https://github.com/golang/go/wiki/CompilerOptimizations#optimized-memclr
   106  	var empty elemType
   107  	for i := range elems {
   108  		elems[i] = empty
   109  	}
   110  	return elems
   111  }