From ec9ef65d82d2d17f308c3f2fbe4bcd8c6b42a3f2 Mon Sep 17 00:00:00 2001 From: Kyle Xiao Date: Mon, 11 Nov 2024 16:24:05 +0800 Subject: [PATCH] feat: add NoJIT option --- frugal.go | 7 ++++--- frugal_jit.go | 8 +------- frugal_jit_go124.go | 8 +++++++- internal/opts/{limits.go => defaults.go} | 2 ++ internal/opts/options.go | 4 ++++ options.go | 13 ++++++++++--- 6 files changed, 28 insertions(+), 14 deletions(-) rename internal/opts/{limits.go => defaults.go} (95%) diff --git a/frugal.go b/frugal.go index 9c3467d..6cdc1e7 100644 --- a/frugal.go +++ b/frugal.go @@ -17,13 +17,14 @@ package frugal import ( + "github.com/cloudwego/frugal/internal/opts" "github.com/cloudwego/frugal/internal/reflect" "github.com/cloudwego/gopkg/protocol/thrift" ) // EncodedSize measures the encoded size of val. func EncodedSize(val interface{}) int { - if nojit { + if !jit || opts.NoJIT { return reflect.EncodedSize(val) } return jitEncodedSize(val) @@ -32,7 +33,7 @@ func EncodedSize(val interface{}) int { // EncodeObject serializes val into buf with Thrift Binary Protocol, with optional Zero-Copy thrift.NocopyWriter. // buf must be large enough to contain the entire serialization result. func EncodeObject(buf []byte, w thrift.NocopyWriter, val interface{}) (int, error) { - if nojit { + if !jit || opts.NoJIT { return reflect.Encode(buf, val) // TODO: impl thrift.NocopyWriter } return jitEncodeObject(buf, w, val) @@ -40,7 +41,7 @@ func EncodeObject(buf []byte, w thrift.NocopyWriter, val interface{}) (int, erro // DecodeObject deserializes buf into val with Thrift Binary Protocol. func DecodeObject(buf []byte, val interface{}) (int, error) { - if nojit { + if !jit || opts.NoJIT { return reflect.Decode(buf, val) } return jitDecodeObject(buf, val) diff --git a/frugal_jit.go b/frugal_jit.go index 6f65eae..acb6777 100644 --- a/frugal_jit.go +++ b/frugal_jit.go @@ -32,13 +32,7 @@ import ( "github.com/cloudwego/gopkg/protocol/thrift" ) -var nojit bool - -func init() { - if v, err := strconv.ParseBool(os.Getenv("FRUGAL_NO_JIT")); err == nil { - nojit = v - } -} +const jit = true func jitEncodedSize(val interface{}) int { return encoder.EncodedSize(val) diff --git a/frugal_jit_go124.go b/frugal_jit_go124.go index 78527a8..bad8477 100644 --- a/frugal_jit_go124.go +++ b/frugal_jit_go124.go @@ -21,10 +21,16 @@ package frugal import ( "reflect" + "github.com/cloudwego/frugal/internal/opts" "github.com/cloudwego/gopkg/protocol/thrift" ) -const nojit = true +const jit = false // not implemented for go1.24 || !amd64 || windows + +func init() { + // force set nojit true coz jit not available + opts.NoJIT = true +} func jitEncodedSize(val interface{}) int { panic("not support JIT for Go version >= go1.24") diff --git a/internal/opts/limits.go b/internal/opts/defaults.go similarity index 95% rename from internal/opts/limits.go rename to internal/opts/defaults.go index c7d5b03..bb44a77 100644 --- a/internal/opts/limits.go +++ b/internal/opts/defaults.go @@ -29,6 +29,8 @@ const ( var ( MaxInlineDepth = parseOrDefault("FRUGAL_MAX_INLINE_DEPTH", _DefaultMaxInlineDepth, 1) MaxInlineILSize = parseOrDefault("FRUGAL_MAX_INLINE_IL_SIZE", _DefaultMaxInlineILSize, 256) + + NoJIT, _ = strconv.ParseBool(os.Getenv("FRUGAL_NO_JIT")) ) func parseOrDefault(key string, def int, min int) int { diff --git a/internal/opts/options.go b/internal/opts/options.go index e17612f..815c293 100644 --- a/internal/opts/options.go +++ b/internal/opts/options.go @@ -20,6 +20,8 @@ type Options struct { MaxInlineDepth int MaxInlineILSize int MaxPretouchDepth int + + NoJIT bool } func (self *Options) CanInline(sp int, pc int) bool { @@ -35,5 +37,7 @@ func GetDefaultOptions() Options { MaxInlineDepth: MaxInlineDepth, MaxInlineILSize: MaxInlineILSize, MaxPretouchDepth: 0, + + NoJIT: NoJIT, } } diff --git a/options.go b/options.go index 304c871..e5a875b 100644 --- a/options.go +++ b/options.go @@ -22,13 +22,20 @@ import ( "github.com/cloudwego/frugal/internal/opts" ) +// Option is the property setter function for opts.Options. +type Option func(*opts.Options) + +// NoJIT disables JIT encoder and decoder explicitly. +// +// This function will be deprecated along with the JIT implementation in the future. +func NoJIT(v bool) { + opts.NoJIT = v +} + const ( _MinILSize = 1024 ) -// Option is the property setter function for opts.Options. -type Option func(*opts.Options) - // WithMaxInlineDepth sets the maximum inlining depth for the JIT compiler. // // Increasing of this option makes the compiler inline more aggressively, which