Skip to content

Commit

Permalink
feat: add NoJIT option (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost authored Nov 11, 2024
1 parent 8a2ce77 commit 051092e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
7 changes: 4 additions & 3 deletions frugal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -32,15 +33,15 @@ 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)
}

// 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)
Expand Down
12 changes: 2 additions & 10 deletions frugal_jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
package frugal

import (
"os"
"reflect"
"strconv"
"sync"

"github.com/cloudwego/frugal/internal/jit/decoder"
Expand All @@ -32,13 +30,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)
Expand Down Expand Up @@ -74,7 +66,7 @@ func newty(ty *rt.GoType, d int) *_Ty {
// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
// order to reduce the first-hit latency.
func Pretouch(vt reflect.Type, options ...Option) error {
if nojit {
if opts.NoJIT {
return nil
}
d := 0
Expand Down
8 changes: 7 additions & 1 deletion frugal_jit_go124.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 opts.NoJIT true coz jit not available
opts.NoJIT = true
}

func jitEncodedSize(val interface{}) int {
panic("not support JIT for Go version >= go1.24")
Expand Down
2 changes: 2 additions & 0 deletions internal/opts/limits.go → internal/opts/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions internal/opts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Options struct {
MaxInlineDepth int
MaxInlineILSize int
MaxPretouchDepth int

NoJIT bool
}

func (self *Options) CanInline(sp int, pc int) bool {
Expand All @@ -35,5 +37,7 @@ func GetDefaultOptions() Options {
MaxInlineDepth: MaxInlineDepth,
MaxInlineILSize: MaxInlineILSize,
MaxPretouchDepth: 0,

NoJIT: NoJIT,
}
}
13 changes: 10 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 051092e

Please sign in to comment.