github.com/cloudwego/frugal@v0.1.15/options.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 frugal
    18  
    19  import (
    20      `fmt`
    21  
    22      `github.com/cloudwego/frugal/internal/opts`
    23  )
    24  
    25  const (
    26      _MinILSize = 1024
    27  )
    28  
    29  // Option is the property setter function for opts.Options.
    30  type Option func(*opts.Options)
    31  
    32  // WithMaxInlineDepth sets the maximum inlining depth for the JIT compiler.
    33  //
    34  // Increasing of this option makes the compiler inline more aggressively, which
    35  // gives better runtime performance at the cost of a longer compilation time,
    36  // and vice versa.
    37  //
    38  // Set this option to "0" disables this limit, which means inlining everything.
    39  //
    40  // The default value of this option is "5".
    41  func WithMaxInlineDepth(depth int) Option {
    42      if depth < 0 {
    43          panic(fmt.Sprintf("frugal: invalid inline depth: %d", depth))
    44      } else {
    45          return func(o *opts.Options) { o.MaxInlineDepth = depth }
    46      }
    47  }
    48  
    49  // WithMaxInlineILSize sets the maximum IL instruction count before not inlining.
    50  //
    51  // Increasing of this option makes the compiler inline more aggressively, which
    52  // lead to more memory consumptions but better runtime performance, and vice
    53  // versa.
    54  //
    55  // Set this option to "0" disables this limit, which means unlimited inlining
    56  // IL buffer.
    57  //
    58  // The default value of this option is "50000".
    59  func WithMaxInlineILSize(size int) Option {
    60      if size != 0 && size < _MinILSize {
    61          panic(fmt.Sprintf("frugal: invalid inline IL size: %d", size))
    62      } else {
    63          return func(o *opts.Options) { o.MaxInlineILSize = size }
    64      }
    65  }
    66  
    67  // WithMaxPretouchDepth controls how deep the compiler goes to compile
    68  // indirectly referenced types.
    69  //
    70  // Larger depth means more types will be pre-compiled when pretouching,
    71  // which lead to longer compilation time, but lower runtime JIT latency,
    72  // and vice versa. You might want to tune this value to strike a balance
    73  // between compilation time and runtime performance.
    74  //
    75  // The default value "0" means unlimited, which basically turns Frugal into
    76  // an AOT compiler.
    77  //
    78  // This option is only available when performing pretouch, otherwise it is
    79  // ignored and do not have any effect.
    80  func WithMaxPretouchDepth(depth int) Option {
    81      if depth < 0 {
    82          panic(fmt.Sprintf("frugal: invalid pretouch depth: %d", depth))
    83      } else {
    84          return func(o *opts.Options) { o.MaxPretouchDepth = depth }
    85      }
    86  }
    87  
    88  // SetMaxInlineDepth sets the default maximum inlining depth for all types from
    89  // now on.
    90  //
    91  // This value can also be configured with the `FRUGAL_MAX_INLINE_DEPTH`
    92  // environment variable.
    93  //
    94  // The default value of this option is "5".
    95  //
    96  // Returns the old opts.MaxInlineDepth value.
    97  func SetMaxInlineDepth(depth int) int {
    98      depth, opts.MaxInlineDepth = opts.MaxInlineDepth, depth
    99      return depth
   100  }
   101  
   102  // SetMaxInlineILSize sets the default maximum inlining IL instructions for all
   103  // types from now on.
   104  //
   105  // This value can also be configured with the `FRUGAL_MAX_INLINE_IL_SIZE`
   106  // environment variable.
   107  //
   108  // The default value of this option is "50000".
   109  //
   110  // Returns the old opts.MaxInlineILSize value.
   111  func SetMaxInlineILSize(size int) int {
   112      size, opts.MaxInlineILSize = opts.MaxInlineILSize, size
   113      return size
   114  }