diff --git a/ast/api_compat.go b/ast/api_compat.go index 19c5cd681..59020caeb 100644 --- a/ast/api_compat.go +++ b/ast/api_compat.go @@ -23,11 +23,12 @@ import ( `unicode/utf8` `github.com/bytedance/sonic/internal/native/types` + `github.com/bytedance/sonic/internal/envs` `github.com/bytedance/sonic/internal/rt` ) func init() { - println("WARNING:(ast) sonic only supports go1.17~1.22, but your environment is not suitable") + println("WARNING: sonic/decoder only supports", envs.SupportedEnvs, "but your environment is not suitable and fallback to standard library now") } func quote(buf *[]byte, val string) { diff --git a/ast/stubs.go b/ast/stubs.go index ae08f33d6..a02172163 100644 --- a/ast/stubs.go +++ b/ast/stubs.go @@ -37,13 +37,106 @@ func mem2ptr(s []byte) unsafe.Pointer { return (*rt.GoSlice)(unsafe.Pointer(&s)).Ptr } -var ( - //go:linkname safeSet encoding/json.safeSet - safeSet [utf8.RuneSelf]bool +var safeSet = [utf8.RuneSelf]bool{ + ' ': true, + '!': true, + '"': false, + '#': true, + '$': true, + '%': true, + '&': true, + '\'': true, + '(': true, + ')': true, + '*': true, + '+': true, + ',': true, + '-': true, + '.': true, + '/': true, + '0': true, + '1': true, + '2': true, + '3': true, + '4': true, + '5': true, + '6': true, + '7': true, + '8': true, + '9': true, + ':': true, + ';': true, + '<': true, + '=': true, + '>': true, + '?': true, + '@': true, + 'A': true, + 'B': true, + 'C': true, + 'D': true, + 'E': true, + 'F': true, + 'G': true, + 'H': true, + 'I': true, + 'J': true, + 'K': true, + 'L': true, + 'M': true, + 'N': true, + 'O': true, + 'P': true, + 'Q': true, + 'R': true, + 'S': true, + 'T': true, + 'U': true, + 'V': true, + 'W': true, + 'X': true, + 'Y': true, + 'Z': true, + '[': true, + '\\': false, + ']': true, + '^': true, + '_': true, + '`': true, + 'a': true, + 'b': true, + 'c': true, + 'd': true, + 'e': true, + 'f': true, + 'g': true, + 'h': true, + 'i': true, + 'j': true, + 'k': true, + 'l': true, + 'm': true, + 'n': true, + 'o': true, + 'p': true, + 'q': true, + 'r': true, + 's': true, + 't': true, + 'u': true, + 'v': true, + 'w': true, + 'x': true, + 'y': true, + 'z': true, + '{': true, + '|': true, + '}': true, + '~': true, + '\u007f': true, +} - //go:linkname hex encoding/json.hex - hex string -) +var hex = "0123456789abcdef" //go:linkname unquoteBytes encoding/json.unquoteBytes func unquoteBytes(s []byte) (t []byte, ok bool) diff --git a/decoder/decoder_compat.go b/decoder/decoder_compat.go index a5c1fb39f..14184879c 100644 --- a/decoder/decoder_compat.go +++ b/decoder/decoder_compat.go @@ -26,11 +26,12 @@ import ( `unsafe` `github.com/bytedance/sonic/internal/native/types` + `github.com/bytedance/sonic/internal/envs` `github.com/bytedance/sonic/option` ) func init() { - println("WARNING: sonic/decoder only supports (Go1.17~1.22 && CPU amd64) or (go1.20~1.22 && CPU arm64), but your environment is not suitable") + println("WARNING: sonic/decoder only supports", envs.SupportedEnvs, "but your environment is not suitable and fallback to standard library now") } const ( diff --git a/encoder/encoder_compat.go b/encoder/encoder_compat.go index f4195b1e0..8bd5771ac 100644 --- a/encoder/encoder_compat.go +++ b/encoder/encoder_compat.go @@ -25,10 +25,11 @@ import ( `reflect` `github.com/bytedance/sonic/option` + `github.com/bytedance/sonic/internal/envs` ) func init() { - println("WARNING:(encoder) sonic only supports (Go1.17~1.22 && CPU amd64) or (G01.20~1.22 && CPU arm64) , but your environment is not suitable") + println("WARNING: sonic/decoder only supports", envs.SupportedEnvs, "but your environment is not suitable and fallback to standard library now") } // EnableFallback indicates if encoder use fallback diff --git a/internal/decoder/optdec/node.go b/internal/decoder/optdec/node.go index 8b29e03cc..d55c2eaa7 100644 --- a/internal/decoder/optdec/node.go +++ b/internal/decoder/optdec/node.go @@ -967,11 +967,7 @@ func AsEfaceFast(iter *NodeIter, ctx *Context) interface{} { val = sp goto _arr_val; } else { - ctx.efacePool.ConvTSlice(rt.GoSlice{ - Ptr: rt.ZSTPtr, - Len: 0, - Cap: 0, - }, rt.SliceEfaceType, unsafe.Pointer(&root)) + ctx.efacePool.ConvTSlice(rt.EmptySlice, rt.SliceEfaceType, unsafe.Pointer(&root)) } case KStringCommon: ctx.efacePool.ConvTstring(node.StringRef(ctx), unsafe.Pointer(&root)) case KStringEscaped: ctx.efacePool.ConvTstring(node.StringCopyEsc(ctx), unsafe.Pointer(&root)) @@ -993,9 +989,8 @@ _object_key: } else { key = node.StringCopyEsc(ctx) } - // interface{} slot in map bucket - //println("key is **", key, "** mp is ", mp) + // interface{} slot in map bucket val = rt.Mapassign_faststr(rt.MapEfaceMapType, mp, key) vt = &(*rt.GoEface)(val).Type vp = &(*rt.GoEface)(val).Value @@ -1021,11 +1016,7 @@ _object_key: case KArray: newSize := node.Array().Len() if newSize == 0 { - ctx.efacePool.ConvTSlice(rt.GoSlice{ - Ptr: rt.ZSTPtr, - Len: 0, - Cap: 0, - }, rt.SliceEfaceType, val) + ctx.efacePool.ConvTSlice(rt.EmptySlice, rt.SliceEfaceType, val) break; } @@ -1124,11 +1115,7 @@ _arr_val: case KArray: newSize := node.Array().Len() if newSize == 0 { - ctx.efacePool.ConvTSlice(rt.GoSlice{ - Ptr: rt.ZSTPtr, - Len: newSize, - Cap: newSize, - }, rt.SliceEfaceType, val) + ctx.efacePool.ConvTSlice(rt.EmptySlice, rt.SliceEfaceType, val) break; } diff --git a/internal/decoder/optdec/options.go b/internal/decoder/optdec/options.go deleted file mode 100644 index 416af1d29..000000000 --- a/internal/decoder/optdec/options.go +++ /dev/null @@ -1,58 +0,0 @@ -package optdec - -// type Options uint64 - -// const ( -// OptionUseInt64 Options = 1 << _F_use_int64 // 1 -// OptionUseNumber Options = 1 << _F_use_number // 2 -// OptionUseUnicodeErrors Options = 1 << _F_disable_urc // 4 -// OptionDisableUnknown Options = 1 << _F_disable_unknown // 8 -// OptionCopyString Options = 1 << _F_copy_string // 16 -// OptionValidateString Options = 1 << _F_validate_string // 32 -// ) - -// func (self *Decoder) SetOptions(opts Options) { -// if (opts&OptionUseNumber != 0) && (opts&OptionUseInt64 != 0) { -// panic("can't set OptionUseInt64 and OptionUseNumber both!") -// } -// self.opts = opts -// } - -// // UseInt64 indicates the Decoder to unmarshal an integer into an interface{} as an -// // int64 instead of as a float64. -// func (self *Decoder) UseInt64() { -// self.opts |= 1 << _F_use_int64 -// self.opts &^= 1 << _F_use_number -// } - -// // UseNumber indicates the Decoder to unmarshal a number into an interface{} as a -// // json.Number instead of as a float64. -// func (self *Decoder) UseNumber() { -// self.opts &^= 1 << _F_use_int64 -// self.opts |= 1 << _F_use_number -// } - -// // UseUnicodeErrors indicates the Decoder to return an error when encounter invalid -// // UTF-8 escape sequences. -// func (self *Decoder) UseUnicodeErrors() { -// self.opts |= 1 << _F_disable_urc -// } - -// // DisallowUnknownFields indicates the Decoder to return an error when the destination -// // is a struct and the input contains object keys which do not match any -// // non-ignored, exported fields in the destination. -// func (self *Decoder) DisallowUnknownFields() { -// self.opts |= 1 << _F_disable_unknown -// } - -// // CopyString indicates the Decoder to decode string values by copying instead of referring. -// func (self *Decoder) CopyString() { -// self.opts |= 1 << _F_copy_string -// } - -// // ValidateString causes the Decoder to validate string values when decoding string value -// // in JSON. Validation is that, returning error when unescaped control chars(0x00-0x1f) or -// // invalid UTF-8 chars in the string value of JSON. -// func (self *Decoder) ValidateString() { -// self.opts |= 1 << _F_validate_string -// } diff --git a/internal/rt/rt_stubs_go116.go b/internal/rt/rt_stubs_go116.go index a2961f067..2c60e164c 100644 --- a/internal/rt/rt_stubs_go116.go +++ b/internal/rt/rt_stubs_go116.go @@ -7,8 +7,6 @@ import ( "unsafe" ) -//go:linkname roundupsize runtime.roundupsize -func roundupsize(size uintptr) uintptr //go:linkname makeslice runtime.makeslice //goland:noinspection GoUnusedParameter @@ -16,11 +14,7 @@ func makeslice(et *GoType, len int, cap int) unsafe.Pointer func MakeSlice(oldPtr unsafe.Pointer, et *GoType, newLen int) *GoSlice { if newLen == 0 { - return &GoSlice{ - Ptr: ZSTPtr, - Len: 0, - Cap: 0, - } + return &EmptySlice } if *(*unsafe.Pointer)(oldPtr) == nil { diff --git a/internal/rt/rt_stubs_go120.go b/internal/rt/rt_stubs_go120.go index ddde46bf6..b0bb9c7e7 100644 --- a/internal/rt/rt_stubs_go120.go +++ b/internal/rt/rt_stubs_go120.go @@ -7,20 +7,13 @@ import ( "unsafe" ) -//go:linkname roundupsize runtime.roundupsize -func roundupsize(size uintptr) uintptr - //go:linkname makeslice runtime.makeslice //goland:noinspection GoUnusedParameter func makeslice(et *GoType, len int, cap int) unsafe.Pointer func MakeSlice(oldPtr unsafe.Pointer, et *GoType, newLen int) *GoSlice { if newLen == 0 { - return &GoSlice{ - Ptr: ZSTPtr, - Len: 0, - Cap: 0, - } + return &EmptySlice } if *(*unsafe.Pointer)(oldPtr) == nil { diff --git a/internal/rt/rt_stubs_go123.go b/internal/rt/rt_stubs_go123.go index 91eb2844d..47cf51e72 100644 --- a/internal/rt/rt_stubs_go123.go +++ b/internal/rt/rt_stubs_go123.go @@ -7,25 +7,13 @@ import ( "unsafe" ) -//go:linkname rt_roundupsize runtime.roundupsize -func rt_roundupsize(size uintptr, noscan bool) uintptr - -// a wrapper for fastmap -func roundupsize(size uintptr) uintptr { - return rt_roundupsize(size, MapType(MapEfaceType).Bucket.PtrData == 0) -} - //go:linkname makeslice runtime.makeslice //goland:noinspection GoUnusedParameter func makeslice(et *GoType, len int, cap int) unsafe.Pointer func MakeSlice(oldPtr unsafe.Pointer, et *GoType, newLen int) *GoSlice { if newLen == 0 { - return &GoSlice{ - Ptr: ZSTPtr, - Len: 0, - Cap: 0, - } + return &EmptySlice } if *(*unsafe.Pointer)(oldPtr) == nil { diff --git a/internal/rt/stubs.go b/internal/rt/stubs.go index 5cb3b18f9..2ca735cc3 100644 --- a/internal/rt/stubs.go +++ b/internal/rt/stubs.go @@ -124,3 +124,6 @@ func GetMap64Assign(vt reflect.Type) Map64Assign { } } + +var emptyBytes = make([]byte, 0, 0) +var EmptySlice = *(*GoSlice)(unsafe.Pointer(&emptyBytes)) \ No newline at end of file diff --git a/internal/rt/types.go b/internal/rt/types.go index 7574f7d89..2c9aaa583 100644 --- a/internal/rt/types.go +++ b/internal/rt/types.go @@ -10,9 +10,6 @@ func AsGoType(t uintptr) *GoType { return (*GoType)(unsafe.Pointer(t)) } -//go:linkname zerobase runtime.zerobase -var zerobase uintptr - var ( BoolType = UnpackType(reflect.TypeOf(false)) ByteType = UnpackType(reflect.TypeOf(byte(0))) @@ -45,6 +42,4 @@ var ( MapStringType = UnpackType(reflect.TypeOf(map[string]string(nil))) MapEfaceMapType = MapType(UnpackType(reflect.TypeOf(map[string]interface{}(nil)))) - - ZSTPtr = unsafe.Pointer(&zerobase) )