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