github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/x/bytes/slice_arraypool_gen.go (about)

     1  // Copyright (c) 2021 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  // This file was automatically generated by genny.
    22  // Any changes will be lost if this file is regenerated.
    23  // see https://github.com/mauricelam/genny
    24  
    25  package bytes
    26  
    27  import (
    28  	"github.com/m3db/m3/src/x/pool"
    29  )
    30  
    31  // Copyright (c) 2018 Uber Technologies, Inc.
    32  //
    33  // Permission is hereby granted, free of charge, to any person obtaining a copy
    34  // of this software and associated documentation files (the "Software"), to deal
    35  // in the Software without restriction, including without limitation the rights
    36  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    37  // copies of the Software, and to permit persons to whom the Software is
    38  // furnished to do so, subject to the following conditions:
    39  //
    40  // The above copyright notice and this permission notice shall be included in
    41  // all copies or substantial portions of the Software.
    42  //
    43  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    44  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    45  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    46  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    47  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    48  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    49  // THE SOFTWARE.
    50  
    51  // SliceArrayPool provides a pool for []byte slices.
    52  type SliceArrayPool interface {
    53  	// Init initializes the array pool, it needs to be called
    54  	// before Get/Put use.
    55  	Init()
    56  
    57  	// Get returns the a slice from the pool.
    58  	Get() [][]byte
    59  
    60  	// Put returns the provided slice to the pool.
    61  	Put(elems [][]byte)
    62  }
    63  
    64  type SliceFinalizeFn func([][]byte) [][]byte
    65  
    66  type SliceArrayPoolOpts struct {
    67  	Options     pool.ObjectPoolOptions
    68  	Capacity    int
    69  	MaxCapacity int
    70  	FinalizeFn  SliceFinalizeFn
    71  }
    72  
    73  type SliceArrPool struct {
    74  	opts SliceArrayPoolOpts
    75  	pool pool.ObjectPool
    76  }
    77  
    78  func NewSliceArrayPool(opts SliceArrayPoolOpts) SliceArrayPool {
    79  	if opts.FinalizeFn == nil {
    80  		opts.FinalizeFn = defaultSliceFinalizerFn
    81  	}
    82  	p := pool.NewObjectPool(opts.Options)
    83  	return &SliceArrPool{opts, p}
    84  }
    85  
    86  func (p *SliceArrPool) Init() {
    87  	p.pool.Init(func() interface{} {
    88  		return make([][]byte, 0, p.opts.Capacity)
    89  	})
    90  }
    91  
    92  func (p *SliceArrPool) Get() [][]byte {
    93  	return p.pool.Get().([][]byte)
    94  }
    95  
    96  func (p *SliceArrPool) Put(arr [][]byte) {
    97  	arr = p.opts.FinalizeFn(arr)
    98  	if max := p.opts.MaxCapacity; max > 0 && cap(arr) > max {
    99  		return
   100  	}
   101  	p.pool.Put(arr)
   102  }
   103  
   104  func defaultSliceFinalizerFn(elems [][]byte) [][]byte {
   105  	var empty []byte
   106  	for i := range elems {
   107  		elems[i] = empty
   108  	}
   109  	elems = elems[:0]
   110  	return elems
   111  }
   112  
   113  type SliceArr [][]byte
   114  
   115  func (elems SliceArr) grow(n int) [][]byte {
   116  	if cap(elems) < n {
   117  		elems = make([][]byte, n)
   118  	}
   119  	elems = elems[:n]
   120  	// following compiler optimized memcpy impl
   121  	// https://github.com/golang/go/wiki/CompilerOptimizations#optimized-memclr
   122  	var empty []byte
   123  	for i := range elems {
   124  		elems[i] = empty
   125  	}
   126  	return elems
   127  }