option.go 2.8 KB

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