github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/client/op_array_pool.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 client
    22  
    23  import (
    24  	"github.com/m3db/m3/src/x/pool"
    25  )
    26  
    27  type opArrayPool struct {
    28  	pool     pool.ObjectPool
    29  	capacity int
    30  }
    31  
    32  func newOpArrayPool(opts pool.ObjectPoolOptions, capacity int) *opArrayPool {
    33  	p := pool.NewObjectPool(opts)
    34  	return &opArrayPool{p, capacity}
    35  }
    36  
    37  func (p *opArrayPool) Init() {
    38  	p.pool.Init(func() interface{} {
    39  		return make([]op, 0, p.capacity)
    40  	})
    41  }
    42  
    43  func (p *opArrayPool) Get() []op {
    44  	return p.pool.Get().([]op)
    45  }
    46  
    47  func (p *opArrayPool) Put(ops []op) {
    48  	for i := range ops {
    49  		ops[i] = nil
    50  	}
    51  	ops = ops[:0]
    52  	p.pool.Put(ops)
    53  }
    54  
    55  type fetchBatchOpArrayArrayPool struct {
    56  	pool     pool.ObjectPool
    57  	entries  int
    58  	capacity int
    59  }
    60  
    61  func newFetchBatchOpArrayArrayPool(
    62  	opts pool.ObjectPoolOptions,
    63  	entries, capacity int,
    64  ) *fetchBatchOpArrayArrayPool {
    65  	p := pool.NewObjectPool(opts)
    66  	return &fetchBatchOpArrayArrayPool{
    67  		pool:     p,
    68  		entries:  entries,
    69  		capacity: capacity,
    70  	}
    71  }
    72  
    73  func (p *fetchBatchOpArrayArrayPool) Init() {
    74  	p.pool.Init(func() interface{} {
    75  		arr := make([][]*fetchBatchOp, p.entries)
    76  		for i := range arr {
    77  			arr[i] = make([]*fetchBatchOp, 0, p.capacity)
    78  		}
    79  		return arr
    80  	})
    81  }
    82  
    83  func (p *fetchBatchOpArrayArrayPool) Get() [][]*fetchBatchOp {
    84  	return p.pool.Get().([][]*fetchBatchOp)
    85  }
    86  
    87  func (p *fetchBatchOpArrayArrayPool) Put(arr [][]*fetchBatchOp) {
    88  	for i := range arr {
    89  		for j := range arr[i] {
    90  			arr[i][j] = nil
    91  		}
    92  		arr[i] = arr[i][:0]
    93  	}
    94  	p.pool.Put(arr)
    95  }
    96  
    97  func (p *fetchBatchOpArrayArrayPool) Entries() int {
    98  	return p.entries
    99  }
   100  
   101  func (p *fetchBatchOpArrayArrayPool) Capacity() int {
   102  	return p.capacity
   103  }