From 458b07851951885cb0eb2ff0d8350122240b7bd6 Mon Sep 17 00:00:00 2001 From: "liuqiang.06" Date: Mon, 8 Jul 2024 15:37:40 +0800 Subject: [PATCH] fix: remove some linknames --- ast/stubs.go | 105 +++++++++++++++++++++++++++-- internal/decoder/optdec/node.go | 21 ++---- internal/decoder/optdec/options.go | 58 ---------------- internal/rt/rt_stubs_go116.go | 8 +-- internal/rt/rt_stubs_go120.go | 13 +--- internal/rt/rt_stubs_go123.go | 48 ------------- internal/rt/stubs.go | 3 + internal/rt/types.go | 5 -- 8 files changed, 110 insertions(+), 151 deletions(-) delete mode 100644 internal/decoder/optdec/options.go delete mode 100644 internal/rt/rt_stubs_go123.go 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/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..735be152d 100644 --- a/internal/rt/rt_stubs_go120.go +++ b/internal/rt/rt_stubs_go120.go @@ -1,5 +1,5 @@ -//go:build go1.20 && !go1.22 -// +build go1.20,!go1.22 +//go:build go1.20 && !go1.23 +// +build go1.20,!go1.23 package rt @@ -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 deleted file mode 100644 index 91eb2844d..000000000 --- a/internal/rt/rt_stubs_go123.go +++ /dev/null @@ -1,48 +0,0 @@ -//go:build go1.22 && !go1.23 -// +build go1.22,!go1.23 - -package rt - -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, - } - } - - if *(*unsafe.Pointer)(oldPtr) == nil { - return &GoSlice{ - Ptr: makeslice(et, newLen, newLen), - Len: newLen, - Cap: newLen, - } - } - - old := (*GoSlice)(oldPtr) - if old.Cap >= newLen { - old.Len = newLen - return old - } - - new := GrowSlice(et, *old, newLen-old.Len) - new.Len = newLen - return &new -} 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) )