github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/chunk/pool.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package chunk 15 16 import ( 17 "sync" 18 19 "github.com/whtcorpsinc/milevadb/types" 20 ) 21 22 // Pool is the DeferredCauset pool. 23 // NOTE: Pool is non-copyable. 24 type Pool struct { 25 initCap int 26 27 varLenDefCausPool *sync.Pool 28 fixLenDefCausPool4 *sync.Pool 29 fixLenDefCausPool8 *sync.Pool 30 fixLenDefCausPool16 *sync.Pool 31 fixLenDefCausPool40 *sync.Pool 32 } 33 34 // NewPool creates a new Pool. 35 func NewPool(initCap int) *Pool { 36 return &Pool{ 37 initCap: initCap, 38 varLenDefCausPool: &sync.Pool{New: func() interface{} { return newVarLenDeferredCauset(initCap, nil) }}, 39 fixLenDefCausPool4: &sync.Pool{New: func() interface{} { return newFixedLenDeferredCauset(4, initCap) }}, 40 fixLenDefCausPool8: &sync.Pool{New: func() interface{} { return newFixedLenDeferredCauset(8, initCap) }}, 41 fixLenDefCausPool16: &sync.Pool{New: func() interface{} { return newFixedLenDeferredCauset(16, initCap) }}, 42 fixLenDefCausPool40: &sync.Pool{New: func() interface{} { return newFixedLenDeferredCauset(40, initCap) }}, 43 } 44 } 45 46 // GetChunk gets a Chunk from the Pool. 47 func (p *Pool) GetChunk(fields []*types.FieldType) *Chunk { 48 chk := new(Chunk) 49 chk.capacity = p.initCap 50 chk.defCausumns = make([]*DeferredCauset, len(fields)) 51 for i, f := range fields { 52 switch elemLen := getFixedLen(f); elemLen { 53 case varElemLen: 54 chk.defCausumns[i] = p.varLenDefCausPool.Get().(*DeferredCauset) 55 case 4: 56 chk.defCausumns[i] = p.fixLenDefCausPool4.Get().(*DeferredCauset) 57 case 8: 58 chk.defCausumns[i] = p.fixLenDefCausPool8.Get().(*DeferredCauset) 59 case 16: 60 chk.defCausumns[i] = p.fixLenDefCausPool16.Get().(*DeferredCauset) 61 case 40: 62 chk.defCausumns[i] = p.fixLenDefCausPool40.Get().(*DeferredCauset) 63 } 64 } 65 return chk 66 } 67 68 // PutChunk puts a Chunk back to the Pool. 69 func (p *Pool) PutChunk(fields []*types.FieldType, chk *Chunk) { 70 for i, f := range fields { 71 switch elemLen := getFixedLen(f); elemLen { 72 case varElemLen: 73 p.varLenDefCausPool.Put(chk.defCausumns[i]) 74 case 4: 75 p.fixLenDefCausPool4.Put(chk.defCausumns[i]) 76 case 8: 77 p.fixLenDefCausPool8.Put(chk.defCausumns[i]) 78 case 16: 79 p.fixLenDefCausPool16.Put(chk.defCausumns[i]) 80 case 40: 81 p.fixLenDefCausPool40.Put(chk.defCausumns[i]) 82 } 83 } 84 chk.defCausumns = nil // release the DeferredCauset references. 85 }