github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/cloud/metainfo/kvstore.go (about) 1 // Copyright 2023 ByteDance 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package metainfo 16 17 import "sync" 18 19 type kvstore map[string]string 20 21 var kvpool sync.Pool 22 23 func newKVStore(size ...int) kvstore { 24 kvs := kvpool.Get() 25 if kvs == nil { 26 if len(size) > 0 { 27 return make(kvstore, size[0]) 28 } 29 return make(kvstore) 30 } 31 return kvs.(kvstore) 32 } 33 34 func (store kvstore) size() int { 35 return len(store) 36 } 37 38 func (store kvstore) recycle() { 39 /* 40 for k := range m { 41 delete(m, k) 42 } 43 ==> 44 LEAQ type.map[string]int(SB), AX 45 MOVQ AX, (SP) 46 MOVQ "".m(SB), AX 47 MOVQ AX, 8(SP) 48 PCDATA $1, $0 49 CALL runtime.mapclear(SB) 50 */ 51 for key := range store { 52 delete(store, key) 53 } 54 kvpool.Put(store) 55 }