Skip to content

Commit

Permalink
refactor(jit): clear up and no longer support >=go1.24 (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost authored Aug 2, 2024
1 parent 5a7ade6 commit 37f518d
Show file tree
Hide file tree
Showing 155 changed files with 350 additions and 960 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "tools/asm2asm"]
path = tools/asm2asm
url = https://github.com/chenzhuoyu/asm2asm
28 changes: 0 additions & 28 deletions Makefile

This file was deleted.

6 changes: 3 additions & 3 deletions debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package debug

import (
"github.com/cloudwego/frugal/internal/binary/decoder"
"github.com/cloudwego/frugal/internal/binary/encoder"
"github.com/cloudwego/frugal/internal/loader"
"github.com/cloudwego/frugal/internal/jit/decoder"
"github.com/cloudwego/frugal/internal/jit/encoder"
"github.com/cloudwego/frugal/internal/jit/loader"
)

// A Stats records statistics about the JIT compiler.
Expand Down
16 changes: 7 additions & 9 deletions frugal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,31 @@
package frugal

import (
"github.com/cloudwego/frugal/internal/binary/decoder"
"github.com/cloudwego/frugal/internal/binary/encoder"
"github.com/cloudwego/frugal/internal/reflect"
"github.com/cloudwego/frugal/iov"
"github.com/cloudwego/gopkg/protocol/thrift"
)

// EncodedSize measures the encoded size of val.
func EncodedSize(val interface{}) int {
if nojit {
return reflect.EncodedSize(val)
}
return encoder.EncodedSize(val)
return jitEncodedSize(val)
}

// EncodeObject serializes val into buf with Thrift Binary Protocol, with optional Zero-Copy iov.BufferWriter.
// 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, mem iov.BufferWriter, val interface{}) (int, error) {
func EncodeObject(buf []byte, w thrift.NocopyWriter, val interface{}) (int, error) {
if nojit {
return reflect.Encode(buf, val) // TODO: impl iov.BufferWriter
return reflect.Encode(buf, val) // TODO: impl thrift.NocopyWriter
}
return encoder.EncodeObject(buf, mem, val)
return jitEncodeObject(buf, w, val)
}

// DecodeObject deserializes buf into val with Thrift Binary Protocol.
func DecodeObject(buf []byte, val interface{}) (int, error) {
if nojit {
return reflect.Decode(buf, val)
}
return decoder.DecodeObject(buf, val)
return jitDecodeObject(buf, val)
}
29 changes: 22 additions & 7 deletions pretouch.go → frugal_jit.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//go:build !go1.24

/*
* Copyright 2022 CloudWeGo Authors
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,13 +22,26 @@ import (
"reflect"
"sync"

"github.com/cloudwego/frugal/internal/binary/decoder"
"github.com/cloudwego/frugal/internal/binary/encoder"
"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/frugal/internal/rt"
"github.com/oleiade/lane"
"github.com/cloudwego/gopkg/protocol/thrift"
)

func jitEncodedSize(val interface{}) int {
return encoder.EncodedSize(val)
}

func jitEncodeObject(buf []byte, w thrift.NocopyWriter, val interface{}) (int, error) {
return encoder.EncodeObject(buf, w, val)
}

func jitDecodeObject(buf []byte, val interface{}) (int, error) {
return decoder.DecodeObject(buf, val)
}

type _Ty struct {
d int
ty *rt.GoType
Expand Down Expand Up @@ -65,12 +80,12 @@ func Pretouch(vt reflect.Type, options ...Option) error {
t := rt.Dereference(rt.UnpackType(vt))

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

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

/* also pretouch the encoder */
Expand Down
47 changes: 47 additions & 0 deletions frugal_jit_go124.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build go1.24

/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package frugal

import (
"reflect"

"github.com/cloudwego/gopkg/protocol/thrift"
)

func init() {
nojit = true
}

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

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

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

// 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 {
panic("not support JIT for Go version >= go1.24")
}
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ require (
github.com/cloudwego/gopkg v0.1.0
github.com/cloudwego/iasm v0.2.0
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/klauspost/cpuid/v2 v2.2.4
github.com/oleiade/lane v1.0.1
github.com/stretchr/testify v1.9.0
golang.org/x/arch v0.2.0
)
Expand All @@ -16,6 +14,5 @@ require (
github.com/bytedance/gopkg v0.0.0-20240711085056-a03554c296f8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
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/oleiade/lane v1.0.1 h1:hXofkn7GEOubzTwNpeL9MaNy8WxolCYb9cInAIeqShU=
github.com/oleiade/lane v1.0.1/go.mod h1:IyTkraa4maLfjq/GmHR+Dxb4kCMtEGeb+qmhlrQ5Mk4=
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=
Expand All @@ -38,8 +34,6 @@ golang.org/x/arch v0.2.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down
Loading

0 comments on commit 37f518d

Please sign in to comment.