github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/option/option.go (about)

     1  /*
     2   * Copyright 2021 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 option
    18  
    19  var (
    20  	// DefaultDecoderBufferSize is the initial buffer size of StreamDecoder
    21  	DefaultDecoderBufferSize uint = 128 * 1024
    22  
    23  	// DefaultEncoderBufferSize is the initial buffer size of Encoder
    24  	DefaultEncoderBufferSize uint = 128 * 1024
    25  )
    26  
    27  // CompileOptions includes all options for encoder or decoder compiler.
    28  type CompileOptions struct {
    29  	// the maximum depth for compilation inline
    30  	MaxInlineDepth int
    31  
    32  	// the loop times for recursively pretouch
    33  	RecursiveDepth int
    34  }
    35  
    36  var (
    37  	// Default value(3) means the compiler only inline 3 layers of nested struct.
    38  	// when the depth exceeds, the compiler will recurse
    39  	// and compile subsequent structs when they are decoded
    40  	DefaultMaxInlineDepth = 3
    41  
    42  	// Default value(1) means `Pretouch()` will be recursively executed once,
    43  	// if any nested struct is left (depth exceeds MaxInlineDepth)
    44  	DefaultRecursiveDepth = 1
    45  )
    46  
    47  // DefaultCompileOptions set default compile options.
    48  func DefaultCompileOptions() CompileOptions {
    49  	return CompileOptions{
    50  		RecursiveDepth: DefaultRecursiveDepth,
    51  		MaxInlineDepth: DefaultMaxInlineDepth,
    52  	}
    53  }
    54  
    55  // CompileOption is a function used to change DefaultCompileOptions.
    56  type CompileOption func(o *CompileOptions)
    57  
    58  // WithCompileRecursiveDepth sets the loop times of recursive pretouch
    59  // in both decoder and encoder,
    60  // for both concrete type and its pointer type.
    61  //
    62  // For deep nested struct (depth exceeds MaxInlineDepth),
    63  // try to set more loops to completely compile,
    64  // thus reduce JIT unstability in the first hit.
    65  func WithCompileRecursiveDepth(loop int) CompileOption {
    66  	return func(o *CompileOptions) {
    67  		if loop < 0 {
    68  			panic("loop must be >= 0")
    69  		}
    70  		o.RecursiveDepth = loop
    71  	}
    72  }
    73  
    74  // WithCompileMaxInlineDepth sets the max depth of inline compile
    75  // in decoder and encoder.
    76  //
    77  // For large nested struct, try to set smaller depth to reduce compiling time.
    78  func WithCompileMaxInlineDepth(depth int) CompileOption {
    79  	return func(o *CompileOptions) {
    80  		if depth <= 0 {
    81  			panic("depth must be > 0")
    82  		}
    83  		o.MaxInlineDepth = depth
    84  	}
    85  }