2
* Copyright 2021 ByteDance Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
20
// DefaultDecoderBufferSize is the initial buffer size of StreamDecoder
21
DefaultDecoderBufferSize uint = 128 * 1024
23
// DefaultEncoderBufferSize is the initial buffer size of Encoder
24
DefaultEncoderBufferSize uint = 128 * 1024
27
// CompileOptions includes all options for encoder or decoder compiler.
28
type CompileOptions struct {
29
// the maximum depth for compilation inline
32
// the loop times for recursively pretouch
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
42
// Default value(1) means `Pretouch()` will be recursively executed once,
43
// if any nested struct is left (depth exceeds MaxInlineDepth)
44
DefaultRecursiveDepth = 1
47
// DefaultCompileOptions set default compile options.
48
func DefaultCompileOptions() CompileOptions {
49
return CompileOptions{
50
RecursiveDepth: DefaultRecursiveDepth,
51
MaxInlineDepth: DefaultMaxInlineDepth,
55
// CompileOption is a function used to change DefaultCompileOptions.
56
type CompileOption func(o *CompileOptions)
58
// WithCompileRecursiveDepth sets the loop times of recursive pretouch
59
// in both decoder and encoder,
60
// for both concrete type and its pointer type.
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) {
68
panic("loop must be >= 0")
70
o.RecursiveDepth = loop
74
// WithCompileMaxInlineDepth sets the max depth of inline compile
75
// in decoder and encoder.
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) {
81
panic("depth must be > 0")
83
o.MaxInlineDepth = depth