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