From 8216173cf2dd10f58447613dead0fb0c8e10ea5b Mon Sep 17 00:00:00 2001 From: "duanyi.aster" Date: Tue, 7 Feb 2023 16:52:05 +0800 Subject: [PATCH] refactor --- ast/encode.go | 6 +- decoder/assembler_amd64_go117.go | 28 +-- decoder/norace_test.go | 10 +- decoder/pools.go | 39 +--- decoder/primitives.go | 10 -- decoder/utils.go | 14 +- encoder/assembler_amd64_go117.go | 27 +-- encoder/encoder.go | 4 +- encoder/encoder_test.go | 158 ++++++++-------- encoder/primitives.go | 13 +- encoder/stubs_go116.go | 6 +- encoder/stubs_go117.go | 8 +- encoder/stubs_go120.go | 6 +- encoder/utils.go | 17 +- .../benchmark_test/encoder_test.go | 16 +- external_jsonlib_test/unit_test/api_test.go | 22 +-- internal/jit/assembler_amd64.go | 5 - internal/loader/funcdata.go | 8 +- internal/loader/funcdata_go116.go | 2 +- internal/loader/loader.go | 11 +- internal/rt/stackmap.go | 168 +++++++++--------- loader/funcdata.go | 60 +++---- loader/funcdata_go115.go | 8 +- loader/funcdata_go118.go | 8 +- loader/funcdata_go120.go | 8 +- loader/loader_go115.go | 4 +- loader/loader_go118.go | 8 +- loader/loader_test.go | 16 +- loader/mmap_unix.go | 4 +- 29 files changed, 328 insertions(+), 366 deletions(-) diff --git a/ast/encode.go b/ast/encode.go index 30677d0e9..1187e30c2 100644 --- a/ast/encode.go +++ b/ast/encode.go @@ -17,10 +17,10 @@ package ast import ( - "sync" - "unicode/utf8" + `sync` + `unicode/utf8` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) const ( diff --git a/decoder/assembler_amd64_go117.go b/decoder/assembler_amd64_go117.go index 0dc9b26e3..8a70cfff6 100644 --- a/decoder/assembler_amd64_go117.go +++ b/decoder/assembler_amd64_go117.go @@ -20,20 +20,20 @@ package decoder import ( - "encoding/json" - "fmt" - "math" - "reflect" - "strconv" - "unsafe" - - "github.com/bytedance/sonic/internal/caching" - "github.com/bytedance/sonic/internal/jit" - "github.com/bytedance/sonic/internal/native" - "github.com/bytedance/sonic/internal/native/types" - "github.com/bytedance/sonic/internal/rt" - "github.com/twitchyliquid64/golang-asm/obj" - "github.com/twitchyliquid64/golang-asm/obj/x86" + `encoding/json` + `fmt` + `math` + `reflect` + `strconv` + `unsafe` + + `github.com/bytedance/sonic/internal/caching` + `github.com/bytedance/sonic/internal/jit` + `github.com/bytedance/sonic/internal/native` + `github.com/bytedance/sonic/internal/native/types` + `github.com/bytedance/sonic/internal/rt` + `github.com/twitchyliquid64/golang-asm/obj` + `github.com/twitchyliquid64/golang-asm/obj/x86` ) /** Register Allocations diff --git a/decoder/norace_test.go b/decoder/norace_test.go index b919e0be9..9a06363a3 100644 --- a/decoder/norace_test.go +++ b/decoder/norace_test.go @@ -20,12 +20,12 @@ package decoder import ( - "runtime" - "testing" - "time" - "unsafe" + `runtime` + `testing` + `time` + `unsafe` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) var referred = false diff --git a/decoder/pools.go b/decoder/pools.go index 781efacc9..ab1e5f23c 100644 --- a/decoder/pools.go +++ b/decoder/pools.go @@ -17,7 +17,6 @@ package decoder import ( - `errors` `sync` `unsafe` @@ -82,37 +81,15 @@ var _KeepAlive struct { frame_generic [_VD_offs]byte } -var errCallShadow = errors.New("DON'T CALL THIS!") - -// Faker func of _Decoder, used to export its stackmap as _Decoder's -func _Decoder_Shadow(s string, i int, vp unsafe.Pointer, sb *_Stack, fv uint64, sv string, vk unsafe.Pointer) (ret int, err error) { - // align to assembler_amd64.go: _FP_offs - var frame [_FP_offs]byte - - // keep all args and stacks alive - _KeepAlive.s = s - _KeepAlive.i = i - _KeepAlive.vp = vp - _KeepAlive.sb = sb - _KeepAlive.fv = fv - _KeepAlive.ret = ret - _KeepAlive.err = err - _KeepAlive.sv = sv - _KeepAlive.vk = vk - _KeepAlive.frame_decoder = frame - - return 0, errCallShadow -} - -// Faker func of _Decoder_Generic, used to export its stackmap -func _Decoder_Generic_Shadow(sb *_Stack) { - // align to generic_amd64.go: _VD_offs - var frame [_VD_offs]byte +var ( + argPtrs = []bool{true, false, false, true, true, false, true, false, true} + localPtrs = []bool{} +) - // must keep sb noticeable to GC - _KeepAlive.sb = sb - _KeepAlive.frame_generic = frame -} +var ( + argPtrs_generic = []bool{true} + localPtrs_generic = []bool{} +) func newStack() *_Stack { if ret := stackPool.Get(); ret == nil { diff --git a/decoder/primitives.go b/decoder/primitives.go index 67005eb42..d6053e2cb 100644 --- a/decoder/primitives.go +++ b/decoder/primitives.go @@ -44,13 +44,3 @@ func decodeJsonUnmarshaler(vv interface{}, s string) error { func decodeTextUnmarshaler(vv interface{}, s string) error { return vv.(encoding.TextUnmarshaler).UnmarshalText(rt.Str2Mem(s)) } - -var ( - argPtrs = []bool{true, false, false, true, true, false, true, false, true} - localPtrs = []bool{} -) - -var ( - argPtrs_generic = []bool{true} - localPtrs_generic = []bool{} -) \ No newline at end of file diff --git a/decoder/utils.go b/decoder/utils.go index 96f3a8d22..23ee5d501 100644 --- a/decoder/utils.go +++ b/decoder/utils.go @@ -17,9 +17,9 @@ package decoder import ( - "unsafe" + `unsafe` - "github.com/bytedance/sonic/loader" + `github.com/bytedance/sonic/loader` ) //go:nosplit @@ -27,13 +27,13 @@ func pbool(v bool) uintptr { return freezeValue(unsafe.Pointer(&v)) } +//go:nosplit +func ptodec(p loader.Function) _Decoder { + return *(*_Decoder)(unsafe.Pointer(&p)) +} + func assert_eq(v int64, exp int64, msg string) { if v != exp { panic(msg) } } - -//go:nosplit -func ptodec(p loader.Function) _Decoder { - return *(*_Decoder)(unsafe.Pointer(&p)) -} diff --git a/encoder/assembler_amd64_go117.go b/encoder/assembler_amd64_go117.go index 3b0825e98..8cd83e868 100644 --- a/encoder/assembler_amd64_go117.go +++ b/encoder/assembler_amd64_go117.go @@ -20,19 +20,19 @@ package encoder import ( - "fmt" - "reflect" - "strconv" - "unsafe" - - "github.com/bytedance/sonic/internal/cpu" - "github.com/bytedance/sonic/internal/jit" - "github.com/bytedance/sonic/internal/native/types" - "github.com/twitchyliquid64/golang-asm/obj" - "github.com/twitchyliquid64/golang-asm/obj/x86" - - "github.com/bytedance/sonic/internal/native" - "github.com/bytedance/sonic/internal/rt" + `fmt` + `reflect` + `strconv` + `unsafe` + + `github.com/bytedance/sonic/internal/cpu` + `github.com/bytedance/sonic/internal/jit` + `github.com/bytedance/sonic/internal/native/types` + `github.com/twitchyliquid64/golang-asm/obj` + `github.com/twitchyliquid64/golang-asm/obj/x86` + + `github.com/bytedance/sonic/internal/native` + `github.com/bytedance/sonic/internal/rt` ) /** Register Allocations @@ -191,6 +191,7 @@ func newAssembler(p _Program) *_Assembler { } /** Assembler Interface **/ + func (self *_Assembler) Load() _Encoder { return ptoenc(self.BaseAssembler.Load("encode_"+self.name, _FP_size, _FP_args, argPtrs, localPtrs)) } diff --git a/encoder/encoder.go b/encoder/encoder.go index 9f7a57679..19a35def1 100644 --- a/encoder/encoder.go +++ b/encoder/encoder.go @@ -139,6 +139,7 @@ func Quote(s string) string { /* allocate space for result */ n = len(s) + 2 p = make([]byte, 0, n) + /* call the encoder */ _ = encodeString(&p, s) return rt.Mem2Str(p) @@ -172,10 +173,9 @@ func Encode(val interface{}, opts Options) ([]byte, error) { // a new one. func EncodeInto(buf *[]byte, val interface{}, opts Options) error { stk := newStack() - efv := rt.UnpackEface(val) err := encodeTypedPointer(buf, efv.Type, &efv.Value, stk, uint64(opts)) - + /* return the stack into pool */ if err != nil { resetStack(stk) diff --git a/encoder/encoder_test.go b/encoder/encoder_test.go index 449df266a..dfdf035ed 100644 --- a/encoder/encoder_test.go +++ b/encoder/encoder_test.go @@ -26,7 +26,7 @@ import ( `sync` `testing` `time` - + `github.com/bytedance/sonic/internal/rt` `github.com/stretchr/testify/require` ) @@ -558,82 +558,82 @@ func BenchmarkCompact_Std(b *testing.B) { } } -// type f64Bench struct { -// name string -// float float64 -// } -// func BenchmarkEncode_Float64(b *testing.B) { -// var bench = []f64Bench{ -// {"Zero", 0}, -// {"ShortDecimal", 1000}, -// {"Decimal", 33909}, -// {"Float", 339.7784}, -// {"Exp", -5.09e75}, -// {"NegExp", -5.11e-95}, -// {"LongExp", 1.234567890123456e-78}, -// {"Big", 123456789123456789123456789}, +type f64Bench struct { + name string + float float64 +} +func BenchmarkEncode_Float64(b *testing.B) { + var bench = []f64Bench{ + {"Zero", 0}, + {"ShortDecimal", 1000}, + {"Decimal", 33909}, + {"Float", 339.7784}, + {"Exp", -5.09e75}, + {"NegExp", -5.11e-95}, + {"LongExp", 1.234567890123456e-78}, + {"Big", 123456789123456789123456789}, -// } -// maxUint := "18446744073709551615" -// for i := 1; i <= len(maxUint); i++ { -// name := strconv.FormatInt(int64(i), 10) + "-Digs" -// num, _ := strconv.ParseUint(string(maxUint[:i]), 10, 64) -// bench = append(bench, f64Bench{name, float64(num)}) -// } -// for _, c := range bench { -// libs := []struct { -// name string -// test func(*testing.B) -// }{{ -// name: "StdLib", -// test: func(b *testing.B) { _, _ = json.Marshal(c.float); for i := 0; i < b.N; i++ { _, _ = json.Marshal(c.float) }}, -// }, { -// name: "Sonic", -// test: func(b *testing.B) { _, _ = Encode(c.float, 0); for i := 0; i < b.N; i++ { _, _ = Encode(c.float, 0) }}, -// }} -// for _, lib := range libs { -// name := lib.name + "_" + c.name -// b.Run(name, lib.test) -// } -// } -// } - -// type f32Bench struct { -// name string -// float float32 -// } -// func BenchmarkEncode_Float32(b *testing.B) { -// var bench = []f32Bench{ -// {"Zero", 0}, -// {"ShortDecimal", 1000}, -// {"Decimal", 33909}, -// {"ExactFraction", 3.375}, -// {"Point", 339.7784}, -// {"Exp", -5.09e25}, -// {"NegExp", -5.11e-25}, -// {"Shortest", 1.234567e-8}, -// } - -// maxUint := "18446744073709551615" -// for i := 1; i <= len(maxUint); i++ { -// name := strconv.FormatInt(int64(i), 10) + "-Digs" -// num, _ := strconv.ParseUint(string(maxUint[:i]), 10, 64) -// bench = append(bench, f32Bench{name, float32(num)}) -// } -// for _, c := range bench { -// libs := []struct { -// name string -// test func(*testing.B) -// }{{ -// name: "StdLib", -// test: func(b *testing.B) { _, _ = json.Marshal(c.float); for i := 0; i < b.N; i++ { _, _ = json.Marshal(c.float) }}, -// }, { -// name: "Sonic", -// test: func(b *testing.B) { _, _ = Encode(c.float, 0); for i := 0; i < b.N; i++ { _, _ = Encode(c.float, 0) }}, -// }} -// for _, lib := range libs { -// name := lib.name + "_" + c.name -// b.Run(name, lib.test) -// } -// } -// } \ No newline at end of file + } + maxUint := "18446744073709551615" + for i := 1; i <= len(maxUint); i++ { + name := strconv.FormatInt(int64(i), 10) + "-Digs" + num, _ := strconv.ParseUint(string(maxUint[:i]), 10, 64) + bench = append(bench, f64Bench{name, float64(num)}) + } + for _, c := range bench { + libs := []struct { + name string + test func(*testing.B) + }{{ + name: "StdLib", + test: func(b *testing.B) { _, _ = json.Marshal(c.float); for i := 0; i < b.N; i++ { _, _ = json.Marshal(c.float) }}, + }, { + name: "Sonic", + test: func(b *testing.B) { _, _ = Encode(c.float, 0); for i := 0; i < b.N; i++ { _, _ = Encode(c.float, 0) }}, + }} + for _, lib := range libs { + name := lib.name + "_" + c.name + b.Run(name, lib.test) + } + } +} + +type f32Bench struct { + name string + float float32 +} +func BenchmarkEncode_Float32(b *testing.B) { + var bench = []f32Bench{ + {"Zero", 0}, + {"ShortDecimal", 1000}, + {"Decimal", 33909}, + {"ExactFraction", 3.375}, + {"Point", 339.7784}, + {"Exp", -5.09e25}, + {"NegExp", -5.11e-25}, + {"Shortest", 1.234567e-8}, + } + + maxUint := "18446744073709551615" + for i := 1; i <= len(maxUint); i++ { + name := strconv.FormatInt(int64(i), 10) + "-Digs" + num, _ := strconv.ParseUint(string(maxUint[:i]), 10, 64) + bench = append(bench, f32Bench{name, float32(num)}) + } + for _, c := range bench { + libs := []struct { + name string + test func(*testing.B) + }{{ + name: "StdLib", + test: func(b *testing.B) { _, _ = json.Marshal(c.float); for i := 0; i < b.N; i++ { _, _ = json.Marshal(c.float) }}, + }, { + name: "Sonic", + test: func(b *testing.B) { _, _ = Encode(c.float, 0); for i := 0; i < b.N; i++ { _, _ = Encode(c.float, 0) }}, + }} + for _, lib := range libs { + name := lib.name + "_" + c.name + b.Run(name, lib.test) + } + } +} \ No newline at end of file diff --git a/encoder/primitives.go b/encoder/primitives.go index 79507cec0..46faece17 100644 --- a/encoder/primitives.go +++ b/encoder/primitives.go @@ -17,13 +17,13 @@ package encoder import ( - "encoding" - "encoding/json" - "unsafe" + `encoding` + `encoding/json` + `unsafe` - "github.com/bytedance/sonic/internal/jit" - "github.com/bytedance/sonic/internal/native" - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/jit` + `github.com/bytedance/sonic/internal/native` + `github.com/bytedance/sonic/internal/rt` ) /** Encoder Primitives **/ @@ -118,7 +118,6 @@ var ( localPtrs = []bool{} ) - var ( _F_assertI2I = jit.Func(assertI2I) ) diff --git a/encoder/stubs_go116.go b/encoder/stubs_go116.go index e5bb72812..40d06f4af 100644 --- a/encoder/stubs_go116.go +++ b/encoder/stubs_go116.go @@ -19,11 +19,11 @@ package encoder import ( - "unsafe" + `unsafe` - _ "github.com/chenzhuoyu/base64x" + _ `github.com/chenzhuoyu/base64x` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) //go:linkname _subr__b64encode github.com/chenzhuoyu/base64x._subr__b64encode diff --git a/encoder/stubs_go117.go b/encoder/stubs_go117.go index 2c3173405..6c8c6ec75 100644 --- a/encoder/stubs_go117.go +++ b/encoder/stubs_go117.go @@ -19,11 +19,11 @@ package encoder import ( - "unsafe" + `unsafe` - _ "github.com/chenzhuoyu/base64x" + _ `github.com/chenzhuoyu/base64x` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) //go:linkname _subr__b64encode github.com/chenzhuoyu/base64x._subr__b64encode @@ -63,4 +63,4 @@ func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) var _runtime_writeBarrier uintptr //go:linkname gcWriteBarrierAX runtime.gcWriteBarrier -func gcWriteBarrierAX() +func gcWriteBarrierAX() \ No newline at end of file diff --git a/encoder/stubs_go120.go b/encoder/stubs_go120.go index fcd0153cd..f1a7d10c7 100644 --- a/encoder/stubs_go120.go +++ b/encoder/stubs_go120.go @@ -19,11 +19,11 @@ package encoder import ( - "unsafe" + `unsafe` - _ "github.com/chenzhuoyu/base64x" + _ `github.com/chenzhuoyu/base64x` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) //go:linkname _subr__b64encode github.com/chenzhuoyu/base64x._subr__b64encode diff --git a/encoder/utils.go b/encoder/utils.go index e01313140..510596fda 100644 --- a/encoder/utils.go +++ b/encoder/utils.go @@ -17,10 +17,10 @@ package encoder import ( - "encoding/json" - "unsafe" + `encoding/json` + `unsafe` - "github.com/bytedance/sonic/loader" + `github.com/bytedance/sonic/loader` ) //go:nosplit @@ -28,6 +28,11 @@ func padd(p unsafe.Pointer, v int) unsafe.Pointer { return unsafe.Pointer(uintptr(p) + uintptr(v)) } +//go:nosplit +func ptoenc(p loader.Function) _Encoder { + return *(*_Encoder)(unsafe.Pointer(&p)) +} + func compact(p *[]byte, v []byte) error { buf := newBuffer() err := json.Compact(buf, v) @@ -45,9 +50,3 @@ func compact(p *[]byte, v []byte) error { freeBuffer(buf) return nil } - -//go:nosplit -func ptoenc(p loader.Function) _Encoder { - return *(*_Encoder)(unsafe.Pointer(&p)) -} - diff --git a/external_jsonlib_test/benchmark_test/encoder_test.go b/external_jsonlib_test/benchmark_test/encoder_test.go index 4584c674c..59fce937c 100644 --- a/external_jsonlib_test/benchmark_test/encoder_test.go +++ b/external_jsonlib_test/benchmark_test/encoder_test.go @@ -17,15 +17,15 @@ package benchmark_test import ( - "encoding/json" - "os" - "runtime" - "runtime/debug" - "testing" - "time" + `encoding/json` + `os` + `runtime` + `runtime/debug` + `testing` + `time` - gojson "github.com/goccy/go-json" - jsoniter "github.com/json-iterator/go" + gojson `github.com/goccy/go-json` + jsoniter `github.com/json-iterator/go` ) var ( diff --git a/external_jsonlib_test/unit_test/api_test.go b/external_jsonlib_test/unit_test/api_test.go index b92790538..7e4c8086c 100644 --- a/external_jsonlib_test/unit_test/api_test.go +++ b/external_jsonlib_test/unit_test/api_test.go @@ -17,17 +17,17 @@ package unit_test import ( - "bytes" - "encoding/json" - "os" - "runtime" - "runtime/debug" - "testing" - "time" - - "github.com/bytedance/sonic" - jsoniter "github.com/json-iterator/go" - "github.com/stretchr/testify/require" + `bytes` + `encoding/json` + `os` + `runtime` + `runtime/debug` + `testing` + `time` + + `github.com/bytedance/sonic` + jsoniter `github.com/json-iterator/go` + `github.com/stretchr/testify/require` ) var ( diff --git a/internal/jit/assembler_amd64.go b/internal/jit/assembler_amd64.go index bdadd1975..4e5907e35 100644 --- a/internal/jit/assembler_amd64.go +++ b/internal/jit/assembler_amd64.go @@ -202,11 +202,6 @@ func (self *BaseAssembler) Init(f func()) { self.o = sync.Once{} } -// func (self *BaseAssembler) Load(fn string, fp int, args int) loader.Function { -// self.build() -// return loader.Loader(self.c).Load(fn, fp, args) -// } - var jitLoader = loader.ModuleLoader{ Name: "sonic.jit.", File: "github.com/bytedance/sonic/jit.go", diff --git a/internal/loader/funcdata.go b/internal/loader/funcdata.go index 7fae8df32..59a3cb349 100644 --- a/internal/loader/funcdata.go +++ b/internal/loader/funcdata.go @@ -17,11 +17,11 @@ package loader import ( - "reflect" - "sync" - "unsafe" + `reflect` + `sync` + `unsafe` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) //go:linkname lastmoduledatap runtime.lastmoduledatap diff --git a/internal/loader/funcdata_go116.go b/internal/loader/funcdata_go116.go index 7c5fa22d6..f01747f93 100644 --- a/internal/loader/funcdata_go116.go +++ b/internal/loader/funcdata_go116.go @@ -20,7 +20,7 @@ package loader import ( - "unsafe" + `unsafe` ) type _Func struct { diff --git a/internal/loader/loader.go b/internal/loader/loader.go index 449d05323..6446a5f07 100644 --- a/internal/loader/loader.go +++ b/internal/loader/loader.go @@ -20,12 +20,11 @@ package loader import ( - "fmt" - "os" - "reflect" - "syscall" - "unsafe" - + `fmt` + `os` + `reflect` + `syscall` + `unsafe` ) const ( diff --git a/internal/rt/stackmap.go b/internal/rt/stackmap.go index 6296fd15a..e2c28c598 100644 --- a/internal/rt/stackmap.go +++ b/internal/rt/stackmap.go @@ -17,131 +17,131 @@ package rt import ( - "fmt" - "strings" - "unsafe" + `fmt` + `strings` + `unsafe` ) type Bitmap struct { - N int - B []byte + N int + B []byte } func (self *Bitmap) grow() { - if self.N >= len(self.B) * 8 { - self.B = append(self.B, 0) - } + if self.N >= len(self.B) * 8 { + self.B = append(self.B, 0) + } } func (self *Bitmap) mark(i int, bv int) { - if bv != 0 { - self.B[i / 8] |= 1 << (i % 8) - } else { - self.B[i / 8] &^= 1 << (i % 8) - } + if bv != 0 { + self.B[i / 8] |= 1 << (i % 8) + } else { + self.B[i / 8] &^= 1 << (i % 8) + } } func (self *Bitmap) Set(i int, bv int) { - if i >= self.N { - panic("bitmap: invalid bit position") - } else { - self.mark(i, bv) - } + if i >= self.N { + panic("bitmap: invalid bit position") + } else { + self.mark(i, bv) + } } func (self *Bitmap) Append(bv int) { - self.grow() - self.mark(self.N, bv) - self.N++ + self.grow() + self.mark(self.N, bv) + self.N++ } func (self *Bitmap) AppendMany(n int, bv int) { - for i := 0; i < n; i++ { - self.Append(bv) - } + for i := 0; i < n; i++ { + self.Append(bv) + } } // var ( -// _stackMapLock = sync.Mutex{} -// _stackMapCache = make(map[*StackMap]struct{}) +// _stackMapLock = sync.Mutex{} +// _stackMapCache = make(map[*StackMap]struct{}) // ) type BitVec struct { - N uintptr - B unsafe.Pointer + N uintptr + B unsafe.Pointer } func (self BitVec) Bit(i uintptr) byte { - return (*(*byte)(unsafe.Pointer(uintptr(self.B) + i / 8)) >> (i % 8)) & 1 + return (*(*byte)(unsafe.Pointer(uintptr(self.B) + i / 8)) >> (i % 8)) & 1 } func (self BitVec) String() string { - var i uintptr - var v []string + var i uintptr + var v []string - /* add each bit */ - for i = 0; i < self.N; i++ { - v = append(v, fmt.Sprintf("%d", self.Bit(i))) - } + /* add each bit */ + for i = 0; i < self.N; i++ { + v = append(v, fmt.Sprintf("%d", self.Bit(i))) + } - /* join them together */ - return fmt.Sprintf( - "BitVec { %s }", - strings.Join(v, ", "), - ) + /* join them together */ + return fmt.Sprintf( + "BitVec { %s }", + strings.Join(v, ", "), + ) } type StackMap struct { - N int32 - L int32 - B [1]byte + N int32 + L int32 + B [1]byte } // func (self *StackMap) add() { -// _stackMapLock.Lock() -// _stackMapCache[self] = struct{}{} -// _stackMapLock.Unlock() +// _stackMapLock.Lock() +// _stackMapCache[self] = struct{}{} +// _stackMapLock.Unlock() // } func (self *StackMap) Pin() uintptr { - // self.add() - return uintptr(unsafe.Pointer(self)) + // self.add() + return uintptr(unsafe.Pointer(self)) } func (self *StackMap) Get(i int32) BitVec { - return BitVec { - N: uintptr(self.L), - B: unsafe.Pointer(uintptr(unsafe.Pointer(&self.B)) + uintptr(i * ((self.L + 7) >> 3))), - } + return BitVec { + N: uintptr(self.L), + B: unsafe.Pointer(uintptr(unsafe.Pointer(&self.B)) + uintptr(i * ((self.L + 7) >> 3))), + } } func (self *StackMap) String() string { - sb := strings.Builder{} - sb.WriteString("StackMap {") + sb := strings.Builder{} + sb.WriteString("StackMap {") - /* dump every stack map */ - for i := int32(0); i < self.N; i++ { - sb.WriteRune('\n') - sb.WriteString(" " + self.Get(i).String()) - } + /* dump every stack map */ + for i := int32(0); i < self.N; i++ { + sb.WriteRune('\n') + sb.WriteString(" " + self.Get(i).String()) + } - /* close the stackmap */ - sb.WriteString("\n}") - return sb.String() + /* close the stackmap */ + sb.WriteString("\n}") + return sb.String() } func (self *StackMap) MarshalBinary() ([]byte, error) { - size := int(self.N) * int(self.L) + int(unsafe.Sizeof(self.L)) + int(unsafe.Sizeof(self.N)) - return BytesFrom(unsafe.Pointer(self), size, size), nil + size := int(self.N) * int(self.L) + int(unsafe.Sizeof(self.L)) + int(unsafe.Sizeof(self.N)) + return BytesFrom(unsafe.Pointer(self), size, size), nil } var ( - byteType = UnpackEface(byte(0)).Type + byteType = UnpackEface(byte(0)).Type ) const ( - _StackMapSize = unsafe.Sizeof(StackMap{}) + _StackMapSize = unsafe.Sizeof(StackMap{}) ) //go:linkname mallocgc runtime.mallocgc @@ -149,32 +149,32 @@ const ( func mallocgc(nb uintptr, vt *GoType, zero bool) unsafe.Pointer type StackMapBuilder struct { - b Bitmap + b Bitmap } func (self *StackMapBuilder) Build() (p *StackMap) { - nb := len(self.b.B) - bm := mallocgc(_StackMapSize + uintptr(nb) - 1, byteType, false) + nb := len(self.b.B) + bm := mallocgc(_StackMapSize + uintptr(nb) - 1, byteType, false) - /* initialize as 1 bitmap of N bits */ - p = (*StackMap)(bm) - p.N, p.L = 1, int32(self.b.N) - copy(BytesFrom(unsafe.Pointer(&p.B), nb, nb), self.b.B) - return + /* initialize as 1 bitmap of N bits */ + p = (*StackMap)(bm) + p.N, p.L = 1, int32(self.b.N) + copy(BytesFrom(unsafe.Pointer(&p.B), nb, nb), self.b.B) + return } func (self *StackMapBuilder) AddField(ptr bool) { - if ptr { - self.b.Append(1) - } else { - self.b.Append(0) - } + if ptr { + self.b.Append(1) + } else { + self.b.Append(0) + } } func (self *StackMapBuilder) AddFields(n int, ptr bool) { - if ptr { - self.b.AppendMany(n, 1) - } else { - self.b.AppendMany(n, 0) - } + if ptr { + self.b.AppendMany(n, 1) + } else { + self.b.AppendMany(n, 0) + } } \ No newline at end of file diff --git a/loader/funcdata.go b/loader/funcdata.go index 182ab1461..9b760f615 100644 --- a/loader/funcdata.go +++ b/loader/funcdata.go @@ -17,13 +17,13 @@ package loader import ( - "encoding" - "encoding/binary" - "fmt" - "reflect" - "strings" - "sync" - "unsafe" + `encoding` + `encoding/binary` + `fmt` + `reflect` + `strings` + `sync` + `unsafe` ) const ( @@ -46,20 +46,20 @@ const ( // // See funcdata.h and $GROOT/src/cmd/internal/objabi/funcdata.go. const ( - _FUNCDATA_ArgsPointerMaps = 0 - _FUNCDATA_LocalsPointerMaps = 1 - _FUNCDATA_StackObjects = 2 - _FUNCDATA_InlTree = 3 - _FUNCDATA_OpenCodedDeferInfo = 4 - _FUNCDATA_ArgInfo = 5 - _FUNCDATA_ArgLiveInfo = 6 - _FUNCDATA_WrapInfo = 7 + _FUNCDATA_ArgsPointerMaps = 0 + _FUNCDATA_LocalsPointerMaps = 1 + _FUNCDATA_StackObjects = 2 + _FUNCDATA_InlTree = 3 + _FUNCDATA_OpenCodedDeferInfo = 4 + _FUNCDATA_ArgInfo = 5 + _FUNCDATA_ArgLiveInfo = 6 + _FUNCDATA_WrapInfo = 7 - // ArgsSizeUnknown is set in Func.argsize to mark all functions - // whose argument size is unknown (C vararg functions, and - // assembly code without an explicit specification). - // This value is generated by the compiler, assembler, or linker. - ArgsSizeUnknown = -0x80000000 + // ArgsSizeUnknown is set in Func.argsize to mark all functions + // whose argument size is unknown (C vararg functions, and + // assembly code without an explicit specification). + // This value is generated by the compiler, assembler, or linker. + ArgsSizeUnknown = -0x80000000 ) // moduledata used to cache the funcdata and findfuncbucket of one module @@ -111,16 +111,16 @@ func getOffsetOf(data interface{}, field string) uintptr { } func rnd(v int64, r int64) int64 { - if r <= 0 { - return v - } - v += r - 1 - c := v % r - if c < 0 { - c += r - } - v -= c - return v + if r <= 0 { + return v + } + v += r - 1 + c := v % r + if c < 0 { + c += r + } + v -= c + return v } var ( diff --git a/loader/funcdata_go115.go b/loader/funcdata_go115.go index 08be36620..f35f03d56 100644 --- a/loader/funcdata_go115.go +++ b/loader/funcdata_go115.go @@ -20,11 +20,11 @@ package loader import ( - "encoding" - "os" - "unsafe" + `encoding` + `os` + `unsafe` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) const ( diff --git a/loader/funcdata_go118.go b/loader/funcdata_go118.go index 6fbd0a366..a2bac857a 100644 --- a/loader/funcdata_go118.go +++ b/loader/funcdata_go118.go @@ -20,11 +20,11 @@ package loader import ( - "encoding" - "os" - "unsafe" + `encoding` + `os` + `unsafe` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) const ( diff --git a/loader/funcdata_go120.go b/loader/funcdata_go120.go index d3ef0796d..906fe375d 100644 --- a/loader/funcdata_go120.go +++ b/loader/funcdata_go120.go @@ -20,11 +20,11 @@ package loader import ( - "encoding" - "os" - "unsafe" + `encoding` + `os` + `unsafe` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) const ( diff --git a/loader/loader_go115.go b/loader/loader_go115.go index b5fc3098b..ccf2108e0 100644 --- a/loader/loader_go115.go +++ b/loader/loader_go115.go @@ -20,9 +20,9 @@ package loader import ( - "unsafe" + `unsafe` - "github.com/bytedance/sonic/internal/loader" + `github.com/bytedance/sonic/internal/loader` ) type Options struct { diff --git a/loader/loader_go118.go b/loader/loader_go118.go index e4d1fddab..416d0e3ac 100644 --- a/loader/loader_go118.go +++ b/loader/loader_go118.go @@ -20,9 +20,9 @@ package loader import ( - "unsafe" + `unsafe` - "github.com/bytedance/sonic/internal/rt" + `github.com/bytedance/sonic/internal/rt` ) // Function is a function pointer @@ -51,8 +51,8 @@ func (self ModuleLoader) LoadFunc(text []byte, funcName string, frameSize int, a } fn.Pcsp = &Pcdata{ - {PC: size, Val: int32(frameSize)}, - } + {PC: size, Val: int32(frameSize)}, + } if self.NoPreempt { fn.PcUnsafePoint = &Pcdata{ diff --git a/loader/loader_test.go b/loader/loader_test.go index 14a2987c2..d85fb962c 100644 --- a/loader/loader_test.go +++ b/loader/loader_test.go @@ -20,14 +20,14 @@ package loader import ( - "runtime" - "runtime/debug" - "strconv" - "testing" - "unsafe" - - "github.com/bytedance/sonic/internal/rt" - "github.com/stretchr/testify/require" + `runtime` + `runtime/debug` + `strconv` + `testing` + `unsafe` + + `github.com/bytedance/sonic/internal/rt` + `github.com/stretchr/testify/require` ) func TestLoad(t *testing.T) { diff --git a/loader/mmap_unix.go b/loader/mmap_unix.go index 51f833470..50b80bf20 100644 --- a/loader/mmap_unix.go +++ b/loader/mmap_unix.go @@ -19,7 +19,9 @@ package loader -import "syscall" +import ( + `syscall` +) const ( _AP = syscall.MAP_ANON | syscall.MAP_PRIVATE