Skip to content

Commit

Permalink
refactor: disable JIT by default (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost authored Jan 9, 2025
1 parent e25b2fb commit 26b037d
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 164 deletions.
22 changes: 20 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Tests
on: [ push, pull_request ]

jobs:
compatibility-test-amd64:
unittest-amd64:
strategy:
matrix:
go: [ "1.18", "1.19", "1.20", "1.21", "1.22", "1.23" ]
Expand All @@ -23,7 +23,7 @@ jobs:
- name: Test Benchmark
run: go test -bench=. -benchmem -run=none ./... -benchtime=100ms

compatibility-test-arm64:
unittest-arm64:
strategy:
matrix:
go: [ "1.18", "1.19", "1.20", "1.21", "1.22", "1.23" ]
Expand All @@ -42,3 +42,21 @@ jobs:
run: cd tests && go test -race
- name: Test Benchmark
run: go test -bench=. -benchmem -run=none ./internal/reflect -benchtime=100ms

buildtag-amd64:
strategy:
matrix:
go: [ "1.18", "1.23" ]
os: [ X64 ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: false # don't use cache for self-hosted runners
- name: Build without frugal_jit
run: go build -v ./...
- name: Build with frugal_jit
run: go build -v -tags=frugal_jit ./...
2 changes: 1 addition & 1 deletion debug/debug_jit.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !go1.24 && amd64 && !windows
//go:build frugal_jit

/*
* Copyright 2022 CloudWeGo Authors
Expand Down
2 changes: 1 addition & 1 deletion debug/debug_go124.go → debug/debug_nojit.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build go1.24 || !amd64 || windows
//go:build !frugal_jit

/*
* Copyright 2024 CloudWeGo Authors
Expand Down
7 changes: 3 additions & 4 deletions frugal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package frugal
import (
"fmt"

"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 !jit || opts.NoJIT {
if nojit {
return reflect.EncodedSize(val)
}
return jitEncodedSize(val)
Expand All @@ -35,7 +34,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 !jit || opts.NoJIT {
if nojit {
ret, err := reflect.Append(buf[:0], val)
if len(ret) > len(buf) {
return 0, fmt.Errorf("index out of range [%d] with length %d.\n"+
Expand All @@ -49,7 +48,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 !jit || opts.NoJIT {
if nojit {
return reflect.Decode(buf, val)
}
return jitDecodeObject(buf, val)
Expand Down
79 changes: 4 additions & 75 deletions frugal_jit.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !go1.24 && amd64 && !windows
//go:build frugal_jit

/*
* Copyright 2024 CloudWeGo Authors
Expand All @@ -20,17 +20,15 @@ package frugal

import (
"reflect"
"sync"

"github.com/cloudwego/frugal/internal/jit"
"github.com/cloudwego/frugal/internal/jit/decoder"
"github.com/cloudwego/frugal/internal/jit/encoder"
"github.com/cloudwego/frugal/internal/jit/rt"
"github.com/cloudwego/frugal/internal/jit/utils"
"github.com/cloudwego/frugal/internal/opts"
"github.com/cloudwego/gopkg/protocol/thrift"
)

const jit = true
const nojit = false

func jitEncodedSize(val interface{}) int {
return encoder.EncodedSize(val)
Expand All @@ -44,79 +42,10 @@ func jitDecodeObject(buf []byte, val interface{}) (int, error) {
return decoder.DecodeObject(buf, val)
}

type _Ty struct {
d int
ty *rt.GoType
}

var (
typool sync.Pool
)

func newty(ty *rt.GoType, d int) *_Ty {
if v := typool.Get(); v == nil {
return &_Ty{d, ty}
} else {
r := v.(*_Ty)
r.d, r.ty = d, ty
return r
}
}

// 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 opts.NoJIT {
return nil
}
d := 0
o := opts.GetDefaultOptions()

/* apply all the options */
for _, fn := range options {
fn(&o)
}

/* unpack the type */
v := make(map[*rt.GoType]bool)
t := rt.Dereference(rt.UnpackType(vt))

/* add the root type */
q := utils.NewQueue()
q.Enqueue(newty(t, 1))

/* BFS the type tree */
for !q.Empty() {
ty := q.Dequeue().(*_Ty)
tv, err := decoder.Pretouch(ty.ty, o)

/* also pretouch the encoder */
if err == nil {
err = encoder.Pretouch(ty.ty, o)
}

/* mark the type as been visited */
d, v[ty.ty] = ty.d, true
typool.Put(ty)

/* check for errors */
if err != nil {
return err
}

/* check for cutoff conditions */
if !o.CanPretouch(d) {
continue
}

/* add all the not visited sub-types */
for s := range tv {
if t = rt.UnpackType(s); !v[t] {
q.Enqueue(newty(t, d+1))
}
}
}

/* completed with no errors */
return nil
return jit.Pretouch(vt, o)
}
16 changes: 5 additions & 11 deletions frugal_jit_go124.go → frugal_nojit.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build go1.24 || !amd64 || windows
//go:build !frugal_jit

/*
* Copyright 2024 CloudWeGo Authors
Expand All @@ -21,27 +21,21 @@ package frugal
import (
"reflect"

"github.com/cloudwego/frugal/internal/opts"
"github.com/cloudwego/gopkg/protocol/thrift"
)

const jit = false // not implemented for go1.24 || !amd64 || windows

func init() {
// force set opts.NoJIT true coz jit not available
opts.NoJIT = true
}
const nojit = true

func jitEncodedSize(val interface{}) int {
panic("not support JIT for Go version >= go1.24")
panic("not supported")
}

func jitEncodeObject(buf []byte, w thrift.NocopyWriter, val interface{}) (int, error) {
panic("not support JIT for Go version >= go1.24")
panic("not supported")
}

func jitDecodeObject(buf []byte, val interface{}) (int, error) {
panic("not support JIT for Go version >= go1.24")
panic("not supported")
}

// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
Expand Down
55 changes: 0 additions & 55 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,74 +12,19 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8Yc
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/arch v0.2.0 h1:W1sUEHXiJTfjaFJ5SLo0N6lZn+0eO5gWD1MFeTGqQEY=
golang.org/x/arch v0.2.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
Loading

0 comments on commit 26b037d

Please sign in to comment.