Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Feb 7, 2023
1 parent f7daf45 commit 8216173
Show file tree
Hide file tree
Showing 29 changed files with 328 additions and 366 deletions.
6 changes: 3 additions & 3 deletions ast/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
28 changes: 14 additions & 14 deletions decoder/assembler_amd64_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions decoder/norace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 8 additions & 31 deletions decoder/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package decoder

import (
`errors`
`sync`
`unsafe`

Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 0 additions & 10 deletions decoder/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
)
14 changes: 7 additions & 7 deletions decoder/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@
package decoder

import (
"unsafe"
`unsafe`

"github.com/bytedance/sonic/loader"
`github.com/bytedance/sonic/loader`
)

//go:nosplit
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))
}
27 changes: 14 additions & 13 deletions encoder/assembler_amd64_go117.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
158 changes: 79 additions & 79 deletions encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
`sync`
`testing`
`time`

`github.com/bytedance/sonic/internal/rt`
`github.com/stretchr/testify/require`
)
Expand Down Expand Up @@ -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)
// }
// }
// }
}
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)
}
}
}
Loading

0 comments on commit 8216173

Please sign in to comment.