github.com/cloudwego/frugal@v0.1.15/internal/binary/encoder/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 encoder
    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      }
    65  }
    66  
    67  func resetCompiler(p *Compiler) *Compiler {
    68      p.o = opts.GetDefaultOptions()
    69      rt.MapClear(p.t)
    70      return p
    71  }
    72  
    73  func newBasicBlock() *BasicBlock {
    74      if v := basicBlockPool.Get(); v != nil {
    75          return v.(*BasicBlock)
    76      } else {
    77          return new(BasicBlock)
    78      }
    79  }
    80  
    81  func freeBasicBlock(p *BasicBlock) {
    82      basicBlockPool.Put(p)
    83  }
    84  
    85  func newGraphBuilder() *GraphBuilder {
    86      if v := graphBuilderPool.Get(); v == nil {
    87          return allocGraphBuilder()
    88      } else {
    89          return resetGraphBuilder(v.(*GraphBuilder))
    90      }
    91  }
    92  
    93  func freeGraphBuilder(p *GraphBuilder) {
    94      graphBuilderPool.Put(p)
    95  }
    96  
    97  func allocGraphBuilder() *GraphBuilder {
    98      return &GraphBuilder {
    99          Pin   : make(map[int]bool),
   100          Graph : make(map[int]*BasicBlock),
   101      }
   102  }
   103  
   104  func resetGraphBuilder(p *GraphBuilder) *GraphBuilder {
   105      rt.MapClear(p.Pin)
   106      rt.MapClear(p.Graph)
   107      return p
   108  }
   109  
   110  func newRuntimeState() *RuntimeState {
   111      if v := runtimeStatePool.Get(); v != nil {
   112          return v.(*RuntimeState)
   113      } else {
   114          return new(RuntimeState)
   115      }
   116  }
   117  
   118  func freeRuntimeState(p *RuntimeState) {
   119      runtimeStatePool.Put(p)
   120  }
   121  
   122  func newOptimizerState() *_OptimizerState {
   123      if v := optimizerStatePool.Get(); v == nil {
   124          return allocOptimizerState()
   125      } else {
   126          return resetOptimizerState(v.(*_OptimizerState))
   127      }
   128  }
   129  
   130  func freeOptimizerState(p *_OptimizerState) {
   131      optimizerStatePool.Put(p)
   132  }
   133  
   134  func allocOptimizerState() *_OptimizerState {
   135      return &_OptimizerState {
   136          buf  : make([]*BasicBlock, 0, 16),
   137          refs : make(map[int]int),
   138          mask : make(map[*BasicBlock]bool),
   139      }
   140  }
   141  
   142  func resetOptimizerState(p *_OptimizerState) *_OptimizerState {
   143      p.buf = p.buf[:0]
   144      rt.MapClear(p.refs)
   145      rt.MapClear(p.mask)
   146      return p
   147  }