github.com/m3db/m3@v1.5.0/src/dbnode/storage/index/aggregate_results_entry_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 index
    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  // AggregateResultsEntryArrayPool provides a pool for aggregateResultsEntry slices.
    52  type AggregateResultsEntryArrayPool 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() []AggregateResultsEntry
    59  
    60  	// Put returns the provided slice to the pool.
    61  	Put(elems []AggregateResultsEntry)
    62  }
    63  
    64  type AggregateResultsEntryFinalizeFn func([]AggregateResultsEntry) []AggregateResultsEntry
    65  
    66  type AggregateResultsEntryArrayPoolOpts struct {
    67  	Options     pool.ObjectPoolOptions
    68  	Capacity    int
    69  	MaxCapacity int
    70  	FinalizeFn  AggregateResultsEntryFinalizeFn
    71  }
    72  
    73  type AggregateResultsEntryArrPool struct {
    74  	opts AggregateResultsEntryArrayPoolOpts
    75  	pool pool.ObjectPool
    76  }
    77  
    78  func NewAggregateResultsEntryArrayPool(opts AggregateResultsEntryArrayPoolOpts) AggregateResultsEntryArrayPool {
    79  	if opts.FinalizeFn == nil {
    80  		opts.FinalizeFn = defaultAggregateResultsEntryFinalizerFn
    81  	}
    82  	p := pool.NewObjectPool(opts.Options)
    83  	return &AggregateResultsEntryArrPool{opts, p}
    84  }
    85  
    86  func (p *AggregateResultsEntryArrPool) Init() {
    87  	p.pool.Init(func() interface{} {
    88  		return make([]AggregateResultsEntry, 0, p.opts.Capacity)
    89  	})
    90  }
    91  
    92  func (p *AggregateResultsEntryArrPool) Get() []AggregateResultsEntry {
    93  	return p.pool.Get().([]AggregateResultsEntry)
    94  }
    95  
    96  func (p *AggregateResultsEntryArrPool) Put(arr []AggregateResultsEntry) {
    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 defaultAggregateResultsEntryFinalizerFn(elems []AggregateResultsEntry) []AggregateResultsEntry {
   105  	var empty AggregateResultsEntry
   106  	for i := range elems {
   107  		elems[i] = empty
   108  	}
   109  	elems = elems[:0]
   110  	return elems
   111  }
   112  
   113  type AggregateResultsEntryArr []AggregateResultsEntry
   114  
   115  func (elems AggregateResultsEntryArr) grow(n int) []AggregateResultsEntry {
   116  	if cap(elems) < n {
   117  		elems = make([]AggregateResultsEntry, 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 AggregateResultsEntry
   123  	for i := range elems {
   124  		elems[i] = empty
   125  	}
   126  	return elems
   127  }