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