github.com/cloudwego/frugal@v0.1.15/internal/atm/hir/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 hir 18 19 import ( 20 `sync` 21 22 `github.com/cloudwego/frugal/internal/rt` 23 ) 24 25 var ( 26 instrPool sync.Pool 27 builderPool sync.Pool 28 ) 29 30 func newInstr(op OpCode) *Ir { 31 if v := instrPool.Get(); v == nil { 32 return allocInstr(op) 33 } else { 34 return resetInstr(op, v.(*Ir)) 35 } 36 } 37 38 func freeInstr(p *Ir) { 39 instrPool.Put(p) 40 } 41 42 func allocInstr(op OpCode) (p *Ir) { 43 p = new(Ir) 44 p.Op = op 45 return 46 } 47 48 func resetInstr(op OpCode, p *Ir) *Ir { 49 *p = Ir{Op: op} 50 return p 51 } 52 53 func newBuilder() *Builder { 54 if v := builderPool.Get(); v == nil { 55 return allocBuilder() 56 } else { 57 return resetBuilder(v.(*Builder)) 58 } 59 } 60 61 func freeBuilder(p *Builder) { 62 builderPool.Put(p) 63 } 64 65 func allocBuilder() (p *Builder) { 66 p = new(Builder) 67 p.refs = make(map[string]*Ir, 64) 68 p.pends = make(map[string][]**Ir, 64) 69 return 70 } 71 72 func resetBuilder(p *Builder) *Builder { 73 p.i = 0 74 p.head = nil 75 p.tail = nil 76 rt.MapClear(p.refs) 77 rt.MapClear(p.pends) 78 return p 79 }