option.go 3.0 KB

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