github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/pool/bucketized.go (about) 1 // Copyright (c) 2016 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 package pool 22 23 import ( 24 "sort" 25 "strconv" 26 27 "github.com/uber-go/tally" 28 ) 29 30 type bucketPool struct { 31 capacity int 32 pool ObjectPool 33 } 34 35 type bucketizedObjectPool struct { 36 sizesAsc []Bucket 37 buckets []bucketPool 38 maxBucketCapacity int 39 opts ObjectPoolOptions 40 alloc BucketizedAllocator 41 maxAlloc tally.Counter 42 } 43 44 // NewBucketizedObjectPool creates a bucketized object pool 45 func NewBucketizedObjectPool(sizes []Bucket, opts ObjectPoolOptions) BucketizedObjectPool { 46 if opts == nil { 47 opts = NewObjectPoolOptions() 48 } 49 50 sizesAsc := make([]Bucket, len(sizes)) 51 copy(sizesAsc, sizes) 52 sort.Sort(BucketByCapacity(sizesAsc)) 53 54 var maxBucketCapacity int 55 if len(sizesAsc) != 0 { 56 maxBucketCapacity = sizesAsc[len(sizesAsc)-1].Capacity 57 } 58 59 iopts := opts.InstrumentOptions() 60 61 return &bucketizedObjectPool{ 62 opts: opts, 63 sizesAsc: sizesAsc, 64 maxBucketCapacity: maxBucketCapacity, 65 maxAlloc: iopts.MetricsScope().Counter("alloc-max"), 66 } 67 } 68 69 func (p *bucketizedObjectPool) Init(alloc BucketizedAllocator) { 70 buckets := make([]bucketPool, len(p.sizesAsc)) 71 for i := range p.sizesAsc { 72 size := p.sizesAsc[i].Count 73 capacity := p.sizesAsc[i].Capacity 74 75 opts := p.opts 76 if perBucketOpts := p.sizesAsc[i].Options; perBucketOpts != nil { 77 opts = perBucketOpts 78 } 79 80 if size > 0 { 81 opts = opts.SetSize(int(size)) 82 } 83 opts = opts.SetDynamic(size.IsDynamic()) 84 85 iopts := opts.InstrumentOptions() 86 87 if iopts.MetricsScope() != nil { 88 sz := strconv.Itoa(capacity) 89 if capacity <= 0 { 90 sz = "dynamic" 91 } 92 opts = opts.SetInstrumentOptions(iopts.SetMetricsScope( 93 iopts.MetricsScope().Tagged(map[string]string{ 94 "bucket-capacity": sz, 95 }))) 96 } 97 98 buckets[i].capacity = capacity 99 buckets[i].pool = NewObjectPool(opts) 100 buckets[i].pool.Init(func() interface{} { 101 return alloc(capacity) 102 }) 103 } 104 p.buckets = buckets 105 p.alloc = alloc 106 } 107 108 func (p *bucketizedObjectPool) Get(capacity int) interface{} { 109 if capacity > p.maxBucketCapacity { 110 p.maxAlloc.Inc(1) 111 return p.alloc(capacity) 112 } 113 for i := range p.buckets { 114 if p.buckets[i].capacity >= capacity { 115 return p.buckets[i].pool.Get() 116 } 117 } 118 return p.alloc(capacity) 119 } 120 121 func (p *bucketizedObjectPool) Put(obj interface{}, capacity int) { 122 if capacity > p.maxBucketCapacity { 123 return 124 } 125 126 for i := len(p.buckets) - 1; i >= 0; i-- { 127 if capacity >= p.buckets[i].capacity { 128 p.buckets[i].pool.Put(obj) 129 return 130 } 131 } 132 }