github.com/cloudwego/frugal@v0.1.15/internal/binary/decoder/pools.go (about) 1 /* 2 * Copyright 2022 ByteDance Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package decoder 18 19 import ( 20 `reflect` 21 `sync` 22 23 `github.com/cloudwego/frugal/internal/opts` 24 `github.com/cloudwego/frugal/internal/rt` 25 ) 26 27 var ( 28 programPool sync.Pool 29 compilerPool sync.Pool 30 basicBlockPool sync.Pool 31 graphBuilderPool sync.Pool 32 runtimeStatePool sync.Pool 33 optimizerStatePool sync.Pool 34 ) 35 36 func newProgram() Program { 37 if v := programPool.Get(); v != nil { 38 return v.(Program)[:0] 39 } else { 40 return make(Program, 0, 16) 41 } 42 } 43 44 func freeProgram(p Program) { 45 programPool.Put(p) 46 } 47 48 func newCompiler() *Compiler { 49 if v := compilerPool.Get(); v == nil { 50 return allocCompiler() 51 } else { 52 return resetCompiler(v.(*Compiler)) 53 } 54 } 55 56 func freeCompiler(p *Compiler) { 57 compilerPool.Put(p) 58 } 59 60 func allocCompiler() *Compiler { 61 return &Compiler { 62 o: opts.GetDefaultOptions(), 63 t: make(map[reflect.Type]bool), 64 d: make(map[reflect.Type]struct{}), 65 } 66 } 67 68 func resetCompiler(p *Compiler) *Compiler { 69 p.o = opts.GetDefaultOptions() 70 rt.MapClear(p.t) 71 rt.MapClear(p.d) 72 return p 73 } 74 75 func newBasicBlock() *BasicBlock { 76 if v := basicBlockPool.Get(); v != nil { 77 return v.(*BasicBlock) 78 } else { 79 return new(BasicBlock) 80 } 81 } 82 83 func freeBasicBlock(p *BasicBlock) { 84 basicBlockPool.Put(p) 85 } 86 87 func newGraphBuilder() *GraphBuilder { 88 if v := graphBuilderPool.Get(); v == nil { 89 return allocGraphBuilder() 90 } else { 91 return resetGraphBuilder(v.(*GraphBuilder)) 92 } 93 } 94 95 func freeGraphBuilder(p *GraphBuilder) { 96 graphBuilderPool.Put(p) 97 } 98 99 func allocGraphBuilder() *GraphBuilder { 100 return &GraphBuilder { 101 Pin : make(map[int]bool), 102 Graph : make(map[int]*BasicBlock), 103 } 104 } 105 106 func resetGraphBuilder(p *GraphBuilder) *GraphBuilder { 107 rt.MapClear(p.Pin) 108 rt.MapClear(p.Graph) 109 return p 110 } 111 112 func newRuntimeState() *RuntimeState { 113 if v := runtimeStatePool.Get(); v != nil { 114 return v.(*RuntimeState) 115 } else { 116 return new(RuntimeState) 117 } 118 } 119 120 func freeRuntimeState(p *RuntimeState) { 121 runtimeStatePool.Put(p) 122 } 123 124 func newOptimizerState() *_OptimizerState { 125 if v := optimizerStatePool.Get(); v == nil { 126 return allocOptimizerState() 127 } else { 128 return resetOptimizerState(v.(*_OptimizerState)) 129 } 130 } 131 132 func freeOptimizerState(p *_OptimizerState) { 133 optimizerStatePool.Put(p) 134 } 135 136 func allocOptimizerState() *_OptimizerState { 137 return &_OptimizerState { 138 buf : make([]*BasicBlock, 0, 16), 139 refs : make(map[int]int), 140 mask : make(map[*BasicBlock]bool), 141 } 142 } 143 144 func resetOptimizerState(p *_OptimizerState) *_OptimizerState { 145 p.buf = p.buf[:0] 146 rt.MapClear(p.refs) 147 rt.MapClear(p.mask) 148 return p 149 }