From 3ac1ee16f377d31a0fb80c8dae28b6239ac4229e Mon Sep 17 00:00:00 2001 From: Terin Stock Date: Sun, 9 Mar 2025 17:47:56 +0100 Subject: [chore] remove vendor --- .../sonic/internal/encoder/alg/mapiter.go | 206 ---- .../bytedance/sonic/internal/encoder/alg/opts.go | 31 - .../sonic/internal/encoder/alg/primitives.go | 95 -- .../bytedance/sonic/internal/encoder/alg/sort.go | 206 ---- .../bytedance/sonic/internal/encoder/alg/spec.go | 198 ---- .../sonic/internal/encoder/alg/spec_compat.go | 148 --- .../bytedance/sonic/internal/encoder/compiler.go | 676 ----------- .../sonic/internal/encoder/encode_norace.go | 24 - .../sonic/internal/encoder/encode_race.go | 54 - .../bytedance/sonic/internal/encoder/encoder.go | 318 ------ .../bytedance/sonic/internal/encoder/ir/op.go | 473 -------- .../sonic/internal/encoder/pools_amd64.go | 97 -- .../sonic/internal/encoder/pools_compt.go | 24 - .../bytedance/sonic/internal/encoder/stream.go | 91 -- .../bytedance/sonic/internal/encoder/vars/cache.go | 48 - .../bytedance/sonic/internal/encoder/vars/const.go | 42 - .../sonic/internal/encoder/vars/errors.go | 65 -- .../bytedance/sonic/internal/encoder/vars/stack.go | 146 --- .../bytedance/sonic/internal/encoder/vars/types.go | 47 - .../bytedance/sonic/internal/encoder/vm/stbus.go | 45 - .../bytedance/sonic/internal/encoder/vm/vm.go | 374 ------ .../internal/encoder/x86/asm_stubs_amd64_go117.go | 53 - .../internal/encoder/x86/asm_stubs_amd64_go121.go | 52 - .../internal/encoder/x86/assembler_regabi_amd64.go | 1195 -------------------- .../sonic/internal/encoder/x86/debug_go116.go | 66 -- .../sonic/internal/encoder/x86/debug_go117.go | 201 ---- .../bytedance/sonic/internal/encoder/x86/stbus.go | 54 - 27 files changed, 5029 deletions(-) delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/alg/mapiter.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/alg/opts.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/alg/primitives.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/alg/sort.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/alg/spec.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/alg/spec_compat.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/compiler.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/encode_norace.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/encode_race.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/encoder.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/ir/op.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/pools_amd64.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/pools_compt.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/stream.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/vars/cache.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/vars/const.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/vars/errors.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/vars/stack.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/vars/types.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/vm/stbus.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/vm/vm.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/x86/asm_stubs_amd64_go117.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/x86/asm_stubs_amd64_go121.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/x86/assembler_regabi_amd64.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/x86/debug_go116.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/x86/debug_go117.go delete mode 100644 vendor/github.com/bytedance/sonic/internal/encoder/x86/stbus.go (limited to 'vendor/github.com/bytedance/sonic/internal/encoder') diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/alg/mapiter.go b/vendor/github.com/bytedance/sonic/internal/encoder/alg/mapiter.go deleted file mode 100644 index 5d9956a90..000000000 --- a/vendor/github.com/bytedance/sonic/internal/encoder/alg/mapiter.go +++ /dev/null @@ -1,206 +0,0 @@ -/* - * Copyright 2021 ByteDance Inc. - * - * 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 alg - -import ( - "encoding" - "reflect" - "strconv" - "sync" - "unsafe" - - "github.com/bytedance/sonic/internal/encoder/vars" - "github.com/bytedance/sonic/internal/rt" -) - -type _MapPair struct { - k string // when the map key is integer, k is pointed to m - v unsafe.Pointer - m [32]byte -} - -type MapIterator struct { - It rt.GoMapIterator // must be the first field - kv rt.GoSlice // slice of _MapPair - ki int -} - -var ( - iteratorPool = sync.Pool{} - iteratorPair = rt.UnpackType(reflect.TypeOf(_MapPair{})) -) - -func init() { - if unsafe.Offsetof(MapIterator{}.It) != 0 { - panic("_MapIterator.it is not the first field") - } -} - - -func newIterator() *MapIterator { - if v := iteratorPool.Get(); v == nil { - return new(MapIterator) - } else { - return resetIterator(v.(*MapIterator)) - } -} - -func resetIterator(p *MapIterator) *MapIterator { - p.ki = 0 - p.It = rt.GoMapIterator{} - p.kv.Len = 0 - return p -} - -func (self *MapIterator) at(i int) *_MapPair { - return (*_MapPair)(unsafe.Pointer(uintptr(self.kv.Ptr) + uintptr(i) * unsafe.Sizeof(_MapPair{}))) -} - -func (self *MapIterator) add() (p *_MapPair) { - p = self.at(self.kv.Len) - self.kv.Len++ - return -} - -func (self *MapIterator) data() (p []_MapPair) { - *(*rt.GoSlice)(unsafe.Pointer(&p)) = self.kv - return -} - -func (self *MapIterator) append(t *rt.GoType, k unsafe.Pointer, v unsafe.Pointer) (err error) { - p := self.add() - p.v = v - - /* check for strings */ - if tk := t.Kind(); tk != reflect.String { - return self.appendGeneric(p, t, tk, k) - } - - /* fast path for strings */ - p.k = *(*string)(k) - return nil -} - -func (self *MapIterator) appendGeneric(p *_MapPair, t *rt.GoType, v reflect.Kind, k unsafe.Pointer) error { - switch v { - case reflect.Int : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int)(k)), 10)) ; return nil - case reflect.Int8 : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int8)(k)), 10)) ; return nil - case reflect.Int16 : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int16)(k)), 10)) ; return nil - case reflect.Int32 : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int32)(k)), 10)) ; return nil - case reflect.Int64 : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int64)(k)), 10)) ; return nil - case reflect.Uint : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint)(k)), 10)) ; return nil - case reflect.Uint8 : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint8)(k)), 10)) ; return nil - case reflect.Uint16 : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint16)(k)), 10)) ; return nil - case reflect.Uint32 : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint32)(k)), 10)) ; return nil - case reflect.Uint64 : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint64)(k)), 10)) ; return nil - case reflect.Uintptr : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uintptr)(k)), 10)) ; return nil - case reflect.Interface : return self.appendInterface(p, t, k) - case reflect.Struct, reflect.Ptr : return self.appendConcrete(p, t, k) - default : panic("unexpected map key type") - } -} - -func (self *MapIterator) appendConcrete(p *_MapPair, t *rt.GoType, k unsafe.Pointer) (err error) { - // compiler has already checked that the type implements the encoding.MarshalText interface - if !t.Indirect() { - k = *(*unsafe.Pointer)(k) - } - eface := rt.GoEface{Value: k, Type: t}.Pack() - out, err := eface.(encoding.TextMarshaler).MarshalText() - if err != nil { - return err - } - p.k = rt.Mem2Str(out) - return -} - -func (self *MapIterator) appendInterface(p *_MapPair, t *rt.GoType, k unsafe.Pointer) (err error) { - if len(rt.IfaceType(t).Methods) == 0 { - panic("unexpected map key type") - } else if p.k, err = asText(k); err == nil { - return nil - } else { - return - } -} - -func IteratorStop(p *MapIterator) { - iteratorPool.Put(p) -} - -func IteratorNext(p *MapIterator) { - i := p.ki - t := &p.It - - /* check for unordered iteration */ - if i < 0 { - rt.Mapiternext(t) - return - } - - /* check for end of iteration */ - if p.ki >= p.kv.Len { - t.K = nil - t.V = nil - return - } - - /* update the key-value pair, and increase the pointer */ - t.K = unsafe.Pointer(&p.at(p.ki).k) - t.V = p.at(p.ki).v - p.ki++ -} - -func IteratorStart(t *rt.GoMapType, m *rt.GoMap, fv uint64) (*MapIterator, error) { - it := newIterator() - rt.Mapiterinit(t, m, &it.It) - - /* check for key-sorting, empty map don't need sorting */ - if m.Count == 0 || (fv & (1< it.kv.Cap { - it.kv = rt.GrowSlice(iteratorPair, it.kv, m.Count) - } - - /* dump all the key-value pairs */ - for ; it.It.K != nil; rt.Mapiternext(&it.It) { - if err := it.append(t.Key, it.It.K, it.It.V); err != nil { - IteratorStop(it) - return nil, err - } - } - - /* sort the keys, map with only 1 item don't need sorting */ - if it.ki = 1; m.Count > 1 { - radixQsort(it.data(), 0, maxDepth(it.kv.Len)) - } - - /* load the first pair into iterator */ - it.It.V = it.at(0).v - it.It.K = unsafe.Pointer(&it.at(0).k) - return it, nil -} - -func asText(v unsafe.Pointer) (string, error) { - text := rt.AssertI2I(rt.UnpackType(vars.EncodingTextMarshalerType), *(*rt.GoIface)(v)) - r, e := (*(*encoding.TextMarshaler)(unsafe.Pointer(&text))).MarshalText() - return rt.Mem2Str(r), e -} diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/alg/opts.go b/vendor/github.com/bytedance/sonic/internal/encoder/alg/opts.go deleted file mode 100644 index c19e2de4e..000000000 --- a/vendor/github.com/bytedance/sonic/internal/encoder/alg/opts.go +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright 2024 ByteDance Inc. - * - * 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 alg - -const ( - BitSortMapKeys = iota - BitEscapeHTML - BitCompactMarshaler - BitNoQuoteTextMarshaler - BitNoNullSliceOrMap - BitValidateString - BitNoValidateJSONMarshaler - BitNoEncoderNewline - BitEncodeNullForInfOrNan - - BitPointerValue = 63 -) diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/alg/primitives.go b/vendor/github.com/bytedance/sonic/internal/encoder/alg/primitives.go deleted file mode 100644 index 63fa01890..000000000 --- a/vendor/github.com/bytedance/sonic/internal/encoder/alg/primitives.go +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Copyright 2024 ByteDance Inc. - * - * 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 alg - -import ( - "encoding" - "encoding/json" - - "github.com/bytedance/sonic/internal/encoder/vars" - "github.com/bytedance/sonic/internal/rt" -) - -func Compact(p *[]byte, v []byte) error { - buf := vars.NewBuffer() - err := json.Compact(buf, v) - - /* check for errors */ - if err != nil { - return err - } - - /* add to result */ - v = buf.Bytes() - *p = append(*p, v...) - - /* return the buffer into pool */ - vars.FreeBuffer(buf) - return nil -} - -func EncodeNil(rb *[]byte) error { - *rb = append(*rb, 'n', 'u', 'l', 'l') - return nil -} - -// func Make_EncodeTypedPointer(computor func(*rt.GoType, ...interface{}) (interface{}, error)) func(*[]byte, *rt.GoType, *unsafe.Pointer, *vars.Stack, uint64) error { -// return func(buf *[]byte, vt *rt.GoType, vp *unsafe.Pointer, sb *vars.Stack, fv uint64) error { -// if vt == nil { -// return EncodeNil(buf) -// } else if fn, err := vars.FindOrCompile(vt, (fv&(1< 11 { - // To avoid the worst case of quickSort (time: O(n^2)), use introsort here. - // Reference: https://en.wikipedia.org/wiki/Introsort and - // https://github.com/golang/go/issues/467 - if maxDepth == 0 { - heapSort(kvs, 0, len(kvs)) - return - } - maxDepth-- - - p := pivot(kvs, d) - lt, i, gt := 0, 0, len(kvs) - for i < gt { - c := byteAt(kvs[i].k, d) - if c < p { - swap(kvs, lt, i) - i++ - lt++ - } else if c > p { - gt-- - swap(kvs, i, gt) - } else { - i++ - } - } - - // kvs[0:lt] < v = kvs[lt:gt] < kvs[gt:len(kvs)] - // Native implementation: - // radixQsort(kvs[:lt], d, maxDepth) - // if p > -1 { - // radixQsort(kvs[lt:gt], d+1, maxDepth) - // } - // radixQsort(kvs[gt:], d, maxDepth) - // Optimize as follows: make recursive calls only for the smaller parts. - // Reference: https://www.geeksforgeeks.org/quicksort-tail-call-optimization-reducing-worst-case-space-log-n/ - if p == -1 { - if lt > len(kvs) - gt { - radixQsort(kvs[gt:], d, maxDepth) - kvs = kvs[:lt] - } else { - radixQsort(kvs[:lt], d, maxDepth) - kvs = kvs[gt:] - } - } else { - ml := maxThree(lt, gt-lt, len(kvs)-gt) - if ml == lt { - radixQsort(kvs[lt:gt], d+1, maxDepth) - radixQsort(kvs[gt:], d, maxDepth) - kvs = kvs[:lt] - } else if ml == gt-lt { - radixQsort(kvs[:lt], d, maxDepth) - radixQsort(kvs[gt:], d, maxDepth) - kvs = kvs[lt:gt] - d += 1 - } else { - radixQsort(kvs[:lt], d, maxDepth) - radixQsort(kvs[lt:gt], d+1, maxDepth) - kvs = kvs[gt:] - } - } - } - insertRadixSort(kvs, d) -} - -func insertRadixSort(kvs []_MapPair, d int) { - for i := 1; i < len(kvs); i++ { - for j := i; j > 0 && lessFrom(kvs[j].k, kvs[j-1].k, d); j-- { - swap(kvs, j, j-1) - } - } -} - -func pivot(kvs []_MapPair, d int) int { - m := len(kvs) >> 1 - if len(kvs) > 40 { - // Tukey's ``Ninther,'' median of three mediankvs of three. - t := len(kvs) / 8 - return medianThree( - medianThree(byteAt(kvs[0].k, d), byteAt(kvs[t].k, d), byteAt(kvs[2*t].k, d)), - medianThree(byteAt(kvs[m].k, d), byteAt(kvs[m-t].k, d), byteAt(kvs[m+t].k, d)), - medianThree(byteAt(kvs[len(kvs)-1].k, d), - byteAt(kvs[len(kvs)-1-t].k, d), - byteAt(kvs[len(kvs)-1-2*t].k, d))) - } - return medianThree(byteAt(kvs[0].k, d), byteAt(kvs[m].k, d), byteAt(kvs[len(kvs)-1].k, d)) -} - -func medianThree(i, j, k int) int { - if i > j { - i, j = j, i - } // i < j - if k < i { - return i - } - if k > j { - return j - } - return k -} - -func maxThree(i, j, k int) int { - max := i - if max < j { - max = j - } - if max < k { - max = k - } - return max -} - -// maxDepth returns a threshold at which quicksort should switch -// to heapsort. It returnkvs 2*ceil(lg(n+1)). -func maxDepth(n int) int { - var depth int - for i := n; i > 0; i >>= 1 { - depth++ - } - return depth * 2 -} - -// siftDown implements the heap property on kvs[lo:hi]. -// first is an offset into the array where the root of the heap lies. -func siftDown(kvs []_MapPair, lo, hi, first int) { - root := lo - for { - child := 2*root + 1 - if child >= hi { - break - } - if child+1 < hi && kvs[first+child].k < kvs[first+child+1].k { - child++ - } - if kvs[first+root].k >= kvs[first+child].k { - return - } - swap(kvs, first+root, first+child) - root = child - } -} - -func heapSort(kvs []_MapPair, a, b int) { - first := a - lo := 0 - hi := b - a - - // Build heap with the greatest element at top. - for i := (hi - 1) / 2; i >= 0; i-- { - siftDown(kvs, i, hi, first) - } - - // Pop elements, the largest first, into end of kvs. - for i := hi - 1; i >= 0; i-- { - swap(kvs, first, first+i) - siftDown(kvs, lo, i, first) - } -} - -// Note that _MapPair.k is NOT pointed to _MapPair.m when map key is integer after swap -func swap(kvs []_MapPair, a, b int) { - kvs[a].k, kvs[b].k = kvs[b].k, kvs[a].k - kvs[a].v, kvs[b].v = kvs[b].v, kvs[a].v -} - -// Compare two strings from the pos d. -func lessFrom(a, b string, d int) bool { - l := len(a) - if l > len(b) { - l = len(b) - } - for i := d; i < l; i++ { - if a[i] == b[i] { - continue - } - return a[i] < b[i] - } - return len(a) < len(b) -} - -func byteAt(b string, p int) int { - if p < len(b) { - return int(b[p]) - } - return -1 -} diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/alg/spec.go b/vendor/github.com/bytedance/sonic/internal/encoder/alg/spec.go deleted file mode 100644 index bff943626..000000000 --- a/vendor/github.com/bytedance/sonic/internal/encoder/alg/spec.go +++ /dev/null @@ -1,198 +0,0 @@ -//go:build (amd64 && go1.16 && !go1.24) || (arm64 && go1.20 && !go1.24) -// +build amd64,go1.16,!go1.24 arm64,go1.20,!go1.24 - -/** - * Copyright 2024 ByteDance Inc. - * - * 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 alg - -import ( - "runtime" - "unsafe" - - "github.com/bytedance/sonic/internal/native" - "github.com/bytedance/sonic/internal/native/types" - "github.com/bytedance/sonic/internal/rt" -) - -// Valid validates json and returns first non-blank character position, -// if it is only one valid json value. -// Otherwise returns invalid character position using start. -// -// Note: it does not check for the invalid UTF-8 characters. -func Valid(data []byte) (ok bool, start int) { - n := len(data) - if n == 0 { - return false, -1 - } - s := rt.Mem2Str(data) - p := 0 - m := types.NewStateMachine() - ret := native.ValidateOne(&s, &p, m, 0) - types.FreeStateMachine(m) - - if ret < 0 { - return false, p-1 - } - - /* check for trailing spaces */ - for ;p < n; p++ { - if (types.SPACE_MASK & (1 << data[p])) == 0 { - return false, p - } - } - - return true, ret -} - -var typeByte = rt.UnpackEface(byte(0)).Type - -//go:nocheckptr -func Quote(buf []byte, val string, double bool) []byte { - if len(val) == 0 { - if double { - return append(buf, `"\"\""`...) - } - return append(buf, `""`...) - } - - if double { - buf = append(buf, `"\"`...) - } else { - buf = append(buf, `"`...) - } - sp := rt.IndexChar(val, 0) - nb := len(val) - b := (*rt.GoSlice)(unsafe.Pointer(&buf)) - - // input buffer - for nb > 0 { - // output buffer - dp := unsafe.Pointer(uintptr(b.Ptr) + uintptr(b.Len)) - dn := b.Cap - b.Len - // call native.Quote, dn is byte count it outputs - opts := uint64(0) - if double { - opts = types.F_DOUBLE_UNQUOTE - } - ret := native.Quote(sp, nb, dp, &dn, opts) - // update *buf length - b.Len += dn - - // no need more output - if ret >= 0 { - break - } - - // double buf size - *b = rt.GrowSlice(typeByte, *b, b.Cap*2) - // ret is the complement of consumed input - ret = ^ret - // update input buffer - nb -= ret - sp = unsafe.Pointer(uintptr(sp) + uintptr(ret)) - } - - runtime.KeepAlive(buf) - runtime.KeepAlive(sp) - if double { - buf = append(buf, `\""`...) - } else { - buf = append(buf, `"`...) - } - - return buf -} - -func HtmlEscape(dst []byte, src []byte) []byte { - var sidx int - - dst = append(dst, src[:0]...) // avoid check nil dst - sbuf := (*rt.GoSlice)(unsafe.Pointer(&src)) - dbuf := (*rt.GoSlice)(unsafe.Pointer(&dst)) - - /* grow dst if it is shorter */ - if cap(dst)-len(dst) < len(src)+types.BufPaddingSize { - cap := len(src)*3/2 + types.BufPaddingSize - *dbuf = rt.GrowSlice(typeByte, *dbuf, cap) - } - - for sidx < sbuf.Len { - sp := rt.Add(sbuf.Ptr, uintptr(sidx)) - dp := rt.Add(dbuf.Ptr, uintptr(dbuf.Len)) - - sn := sbuf.Len - sidx - dn := dbuf.Cap - dbuf.Len - nb := native.HTMLEscape(sp, sn, dp, &dn) - - /* check for errors */ - if dbuf.Len += dn; nb >= 0 { - break - } - - /* not enough space, grow the slice and try again */ - sidx += ^nb - *dbuf = rt.GrowSlice(typeByte, *dbuf, dbuf.Cap*2) - } - return dst -} - -func F64toa(buf []byte, v float64) ([]byte) { - if v == 0 { - return append(buf, '0') - } - buf = rt.GuardSlice2(buf, 64) - ret := native.F64toa((*byte)(rt.IndexByte(buf, len(buf))), v) - if ret > 0 { - return buf[:len(buf)+ret] - } else { - return buf - } -} - -func F32toa(buf []byte, v float32) ([]byte) { - if v == 0 { - return append(buf, '0') - } - buf = rt.GuardSlice2(buf, 64) - ret := native.F32toa((*byte)(rt.IndexByte(buf, len(buf))), v) - if ret > 0 { - return buf[:len(buf)+ret] - } else { - return buf - } -} - -func I64toa(buf []byte, v int64) ([]byte) { - buf = rt.GuardSlice2(buf, 32) - ret := native.I64toa((*byte)(rt.IndexByte(buf, len(buf))), v) - if ret > 0 { - return buf[:len(buf)+ret] - } else { - return buf - } -} - -func U64toa(buf []byte, v uint64) ([]byte) { - buf = rt.GuardSlice2(buf, 32) - ret := native.U64toa((*byte)(rt.IndexByte(buf, len(buf))), v) - if ret > 0 { - return buf[:len(buf)+ret] - } else { - return buf - } -} - diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/alg/spec_compat.go b/vendor/github.com/bytedance/sonic/internal/encoder/alg/spec_compat.go deleted file mode 100644 index c15cbf7d8..000000000 --- a/vendor/github.com/bytedance/sonic/internal/encoder/alg/spec_compat.go +++ /dev/null @@ -1,148 +0,0 @@ -// +build !amd64,!arm64 go1.24 !go1.16 arm64,!go1.20 - -/** - * Copyright 2024 ByteDance Inc. - * - * 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 alg - -import ( - _ "unsafe" - "unicode/utf8" - "strconv" - "bytes" - "encoding/json" - - "github.com/bytedance/sonic/internal/rt" -) - -// Valid validates json and returns first non-blank character position, -// if it is only one valid json value. -// Otherwise returns invalid character position using start. -// -// Note: it does not check for the invalid UTF-8 characters. -func Valid(data []byte) (ok bool, start int) { - ok = json.Valid(data) - return ok, 0 -} - -var typeByte = rt.UnpackEface(byte(0)).Type - -func Quote(e []byte, s string, double bool) []byte { - if len(s) == 0 { - if double { - return append(e, `"\"\""`...) - } - return append(e, `""`...) - } - - b := e - ss := len(e) - e = append(e, '"') - start := 0 - - for i := 0; i < len(s); { - if b := s[i]; b < utf8.RuneSelf { - if rt.SafeSet[b] { - i++ - continue - } - if start < i { - e = append(e, s[start:i]...) - } - e = append(e, '\\') - switch b { - case '\\', '"': - e = append(e, b) - case '\n': - e = append(e, 'n') - case '\r': - e = append(e, 'r') - case '\t': - e = append(e, 't') - default: - // This encodes bytes < 0x20 except for \t, \n and \r. - // If escapeHTML is set, it also escapes <, >, and & - // because they can lead to security holes when - // user-controlled strings are rendered into JSON - // and served to some browsers. - e = append(e, `u00`...) - e = append(e, rt.Hex[b>>4]) - e = append(e, rt.Hex[b&0xF]) - } - i++ - start = i - continue - } - c, size := utf8.DecodeRuneInString(s[i:]) - // if correct && c == utf8.RuneError && size == 1 { - // if start < i { - // e = append(e, s[start:i]...) - // } - // e = append(e, `\ufffd`...) - // i += size - // start = i - // continue - // } - if c == '\u2028' || c == '\u2029' { - if start < i { - e = append(e, s[start:i]...) - } - e = append(e, `\u202`...) - e = append(e, rt.Hex[c&0xF]) - i += size - start = i - continue - } - i += size - } - - if start < len(s) { - e = append(e, s[start:]...) - } - e = append(e, '"') - - if double { - return strconv.AppendQuote(b, string(e[ss:])) - } else { - return e - } -} - -func HtmlEscape(dst []byte, src []byte) []byte { - buf := bytes.NewBuffer(dst) - json.HTMLEscape(buf, src) - return buf.Bytes() -} - -func F64toa(buf []byte, v float64) ([]byte) { - bs := bytes.NewBuffer(buf) - _ = json.NewEncoder(bs).Encode(v) - return bs.Bytes() -} - -func F32toa(buf []byte, v float32) ([]byte) { - bs := bytes.NewBuffer(buf) - _ = json.NewEncoder(bs).Encode(v) - return bs.Bytes() -} - -func I64toa(buf []byte, v int64) ([]byte) { - return strconv.AppendInt(buf, int64(v), 10) -} - -func U64toa(buf []byte, v uint64) ([]byte) { - return strconv.AppendUint(buf, v, 10) -} diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/compiler.go b/vendor/github.com/bytedance/sonic/internal/encoder/compiler.go deleted file mode 100644 index 902fbc98b..000000000 --- a/vendor/github.com/bytedance/sonic/internal/encoder/compiler.go +++ /dev/null @@ -1,676 +0,0 @@ -/* - * Copyright 2021 ByteDance Inc. - * - * 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 encoder - -import ( - "reflect" - "unsafe" - - "github.com/bytedance/sonic/internal/encoder/ir" - "github.com/bytedance/sonic/internal/encoder/vars" - "github.com/bytedance/sonic/internal/encoder/vm" - "github.com/bytedance/sonic/internal/resolver" - "github.com/bytedance/sonic/internal/rt" - "github.com/bytedance/sonic/option" -) - -func ForceUseVM() { - vm.SetCompiler(makeEncoderVM) - pretouchType = pretouchTypeVM - encodeTypedPointer = vm.EncodeTypedPointer - vars.UseVM = true -} - -var encodeTypedPointer func(buf *[]byte, vt *rt.GoType, vp *unsafe.Pointer, sb *vars.Stack, fv uint64) error - -func makeEncoderVM(vt *rt.GoType, ex ...interface{}) (interface{}, error) { - pp, err := NewCompiler().Compile(vt.Pack(), ex[0].(bool)) - if err != nil { - return nil, err - } - return &pp, nil -} - -var pretouchType func(_vt reflect.Type, opts option.CompileOptions, v uint8) (map[reflect.Type]uint8, error) - -func pretouchTypeVM(_vt reflect.Type, opts option.CompileOptions, v uint8) (map[reflect.Type]uint8, error) { - /* compile function */ - compiler := NewCompiler().apply(opts) - - /* find or compile */ - vt := rt.UnpackType(_vt) - if val := vars.GetProgram(vt); val != nil { - return nil, nil - } else if _, err := vars.ComputeProgram(vt, makeEncoderVM, v == 1); err == nil { - return compiler.rec, nil - } else { - return nil, err - } -} - -func pretouchRec(vtm map[reflect.Type]uint8, opts option.CompileOptions) error { - if opts.RecursiveDepth < 0 || len(vtm) == 0 { - return nil - } - next := make(map[reflect.Type]uint8) - for vt, v := range vtm { - sub, err := pretouchType(vt, opts, v) - if err != nil { - return err - } - for svt, v := range sub { - next[svt] = v - } - } - opts.RecursiveDepth -= 1 - return pretouchRec(next, opts) -} - -type Compiler struct { - opts option.CompileOptions - pv bool - tab map[reflect.Type]bool - rec map[reflect.Type]uint8 -} - -func NewCompiler() *Compiler { - return &Compiler{ - opts: option.DefaultCompileOptions(), - tab: map[reflect.Type]bool{}, - rec: map[reflect.Type]uint8{}, - } -} - -func (self *Compiler) apply(opts option.CompileOptions) *Compiler { - self.opts = opts - if self.opts.RecursiveDepth > 0 { - self.rec = map[reflect.Type]uint8{} - } - return self -} - -func (self *Compiler) rescue(ep *error) { - if val := recover(); val != nil { - if err, ok := val.(error); ok { - *ep = err - } else { - panic(val) - } - } -} - -func (self *Compiler) Compile(vt reflect.Type, pv bool) (ret ir.Program, err error) { - defer self.rescue(&err) - self.compileOne(&ret, 0, vt, pv) - return -} - -func (self *Compiler) compileOne(p *ir.Program, sp int, vt reflect.Type, pv bool) { - if self.tab[vt] { - p.Vp(ir.OP_recurse, vt, pv) - } else { - self.compileRec(p, sp, vt, pv) - } -} - -func (self *Compiler) tryCompileMarshaler(p *ir.Program, vt reflect.Type, pv bool) bool { - pt := reflect.PtrTo(vt) - - /* check for addressable `json.Marshaler` with pointer receiver */ - if pv && pt.Implements(vars.JsonMarshalerType) { - addMarshalerOp(p, ir.OP_marshal_p, pt, vars.JsonMarshalerType) - return true - } - - /* check for `json.Marshaler` */ - if vt.Implements(vars.JsonMarshalerType) { - self.compileMarshaler(p, ir.OP_marshal, vt, vars.JsonMarshalerType) - return true - } - - /* check for addressable `encoding.TextMarshaler` with pointer receiver */ - if pv && pt.Implements(vars.EncodingTextMarshalerType) { - addMarshalerOp(p, ir.OP_marshal_text_p, pt, vars.EncodingTextMarshalerType) - return true - } - - /* check for `encoding.TextMarshaler` */ - if vt.Implements(vars.EncodingTextMarshalerType) { - self.compileMarshaler(p, ir.OP_marshal_text, vt, vars.EncodingTextMarshalerType) - return true - } - - return false -} - -func (self *Compiler) compileRec(p *ir.Program, sp int, vt reflect.Type, pv bool) { - pr := self.pv - - if self.tryCompileMarshaler(p, vt, pv) { - return - } - - /* enter the recursion, and compile the type */ - self.pv = pv - self.tab[vt] = true - self.compileOps(p, sp, vt) - - /* exit the recursion */ - self.pv = pr - delete(self.tab, vt) -} - -func (self *Compiler) compileOps(p *ir.Program, sp int, vt reflect.Type) { - switch vt.Kind() { - case reflect.Bool: - p.Add(ir.OP_bool) - case reflect.Int: - p.Add(ir.OP_int()) - case reflect.Int8: - p.Add(ir.OP_i8) - case reflect.Int16: - p.Add(ir.OP_i16) - case reflect.Int32: - p.Add(ir.OP_i32) - case reflect.Int64: - p.Add(ir.OP_i64) - case reflect.Uint: - p.Add(ir.OP_uint()) - case reflect.Uint8: - p.Add(ir.OP_u8) - case reflect.Uint16: - p.Add(ir.OP_u16) - case reflect.Uint32: - p.Add(ir.OP_u32) - case reflect.Uint64: - p.Add(ir.OP_u64) - case reflect.Uintptr: - p.Add(ir.OP_uintptr()) - case reflect.Float32: - p.Add(ir.OP_f32) - case reflect.Float64: - p.Add(ir.OP_f64) - case reflect.String: - self.compileString(p, vt) - case reflect.Array: - self.compileArray(p, sp, vt.Elem(), vt.Len()) - case reflect.Interface: - self.compileInterface(p, vt) - case reflect.Map: - self.compileMap(p, sp, vt) - case reflect.Ptr: - self.compilePtr(p, sp, vt.Elem()) - case reflect.Slice: - self.compileSlice(p, sp, vt.Elem()) - case reflect.Struct: - self.compileStruct(p, sp, vt) - default: - panic(vars.Error_type(vt)) - } -} - -func (self *Compiler) compileNil(p *ir.Program, sp int, vt reflect.Type, nil_op ir.Op, fn func(*ir.Program, int, reflect.Type)) { - x := p.PC() - p.Add(ir.OP_is_nil) - fn(p, sp, vt) - e := p.PC() - p.Add(ir.OP_goto) - p.Pin(x) - p.Add(nil_op) - p.Pin(e) -} - -func (self *Compiler) compilePtr(p *ir.Program, sp int, vt reflect.Type) { - self.compileNil(p, sp, vt, ir.OP_null, self.compilePtrBody) -} - -func (self *Compiler) compilePtrBody(p *ir.Program, sp int, vt reflect.Type) { - p.Tag(sp) - p.Add(ir.OP_save) - p.Add(ir.OP_deref) - self.compileOne(p, sp+1, vt, true) - p.Add(ir.OP_drop) -} - -func (self *Compiler) compileMap(p *ir.Program, sp int, vt reflect.Type) { - self.compileNil(p, sp, vt, ir.OP_empty_obj, self.compileMapBody) -} - -func (self *Compiler) compileMapBody(p *ir.Program, sp int, vt reflect.Type) { - p.Tag(sp + 1) - p.Int(ir.OP_byte, '{') - e := p.PC() - p.Add(ir.OP_is_zero_map) - p.Add(ir.OP_save) - p.Rtt(ir.OP_map_iter, vt) - p.Add(ir.OP_save) - i := p.PC() - p.Add(ir.OP_map_check_key) - u := p.PC() - p.Add(ir.OP_map_write_key) - self.compileMapBodyKey(p, vt.Key()) - p.Pin(u) - p.Int(ir.OP_byte, ':') - p.Add(ir.OP_map_value_next) - self.compileOne(p, sp+2, vt.Elem(), false) - j := p.PC() - p.Add(ir.OP_map_check_key) - p.Int(ir.OP_byte, ',') - v := p.PC() - p.Add(ir.OP_map_write_key) - self.compileMapBodyKey(p, vt.Key()) - p.Pin(v) - p.Int(ir.OP_byte, ':') - p.Add(ir.OP_map_value_next) - self.compileOne(p, sp+2, vt.Elem(), false) - p.Int(ir.OP_goto, j) - p.Pin(i) - p.Pin(j) - p.Add(ir.OP_map_stop) - p.Add(ir.OP_drop_2) - p.Pin(e) - p.Int(ir.OP_byte, '}') -} - -func (self *Compiler) compileMapBodyKey(p *ir.Program, vk reflect.Type) { - if !vk.Implements(vars.EncodingTextMarshalerType) { - self.compileMapBodyTextKey(p, vk) - } else { - self.compileMapBodyUtextKey(p, vk) - } -} - -func (self *Compiler) compileMapBodyTextKey(p *ir.Program, vk reflect.Type) { - switch vk.Kind() { - case reflect.Invalid: - panic("map key is nil") - case reflect.Bool: - p.Key(ir.OP_bool) - case reflect.Int: - p.Key(ir.OP_int()) - case reflect.Int8: - p.Key(ir.OP_i8) - case reflect.Int16: - p.Key(ir.OP_i16) - case reflect.Int32: - p.Key(ir.OP_i32) - case reflect.Int64: - p.Key(ir.OP_i64) - case reflect.Uint: - p.Key(ir.OP_uint()) - case reflect.Uint8: - p.Key(ir.OP_u8) - case reflect.Uint16: - p.Key(ir.OP_u16) - case reflect.Uint32: - p.Key(ir.OP_u32) - case reflect.Uint64: - p.Key(ir.OP_u64) - case reflect.Uintptr: - p.Key(ir.OP_uintptr()) - case reflect.Float32: - p.Key(ir.OP_f32) - case reflect.Float64: - p.Key(ir.OP_f64) - case reflect.String: - self.compileString(p, vk) - default: - panic(vars.Error_type(vk)) - } -} - -func (self *Compiler) compileMapBodyUtextKey(p *ir.Program, vk reflect.Type) { - if vk.Kind() != reflect.Ptr { - addMarshalerOp(p, ir.OP_marshal_text, vk, vars.EncodingTextMarshalerType) - } else { - self.compileMapBodyUtextPtr(p, vk) - } -} - -func (self *Compiler) compileMapBodyUtextPtr(p *ir.Program, vk reflect.Type) { - i := p.PC() - p.Add(ir.OP_is_nil) - addMarshalerOp(p, ir.OP_marshal_text, vk, vars.EncodingTextMarshalerType) - j := p.PC() - p.Add(ir.OP_goto) - p.Pin(i) - p.Str(ir.OP_text, "\"\"") - p.Pin(j) -} - -func (self *Compiler) compileSlice(p *ir.Program, sp int, vt reflect.Type) { - self.compileNil(p, sp, vt, ir.OP_empty_arr, self.compileSliceBody) -} - -func (self *Compiler) compileSliceBody(p *ir.Program, sp int, vt reflect.Type) { - if vars.IsSimpleByte(vt) { - p.Add(ir.OP_bin) - } else { - self.compileSliceArray(p, sp, vt) - } -} - -func (self *Compiler) compileSliceArray(p *ir.Program, sp int, vt reflect.Type) { - p.Tag(sp) - p.Int(ir.OP_byte, '[') - e := p.PC() - p.Add(ir.OP_is_nil) - p.Add(ir.OP_save) - p.Add(ir.OP_slice_len) - i := p.PC() - p.Rtt(ir.OP_slice_next, vt) - self.compileOne(p, sp+1, vt, true) - j := p.PC() - p.Rtt(ir.OP_slice_next, vt) - p.Int(ir.OP_byte, ',') - self.compileOne(p, sp+1, vt, true) - p.Int(ir.OP_goto, j) - p.Pin(i) - p.Pin(j) - p.Add(ir.OP_drop) - p.Pin(e) - p.Int(ir.OP_byte, ']') -} - -func (self *Compiler) compileArray(p *ir.Program, sp int, vt reflect.Type, nb int) { - p.Tag(sp) - p.Int(ir.OP_byte, '[') - p.Add(ir.OP_save) - - /* first item */ - if nb != 0 { - self.compileOne(p, sp+1, vt, self.pv) - p.Add(ir.OP_load) - } - - /* remaining items */ - for i := 1; i < nb; i++ { - p.Int(ir.OP_byte, ',') - p.Int(ir.OP_index, i*int(vt.Size())) - self.compileOne(p, sp+1, vt, self.pv) - p.Add(ir.OP_load) - } - - /* end of array */ - p.Add(ir.OP_drop) - p.Int(ir.OP_byte, ']') -} - -func (self *Compiler) compileString(p *ir.Program, vt reflect.Type) { - if vt != vars.JsonNumberType { - p.Add(ir.OP_str) - } else { - p.Add(ir.OP_number) - } -} - -func (self *Compiler) compileStruct(p *ir.Program, sp int, vt reflect.Type) { - if sp >= self.opts.MaxInlineDepth || p.PC() >= vars.MAX_ILBUF || (sp > 0 && vt.NumField() >= vars.MAX_FIELDS) { - p.Vp(ir.OP_recurse, vt, self.pv) - if self.opts.RecursiveDepth > 0 { - if self.pv { - self.rec[vt] = 1 - } else { - self.rec[vt] = 0 - } - } - } else { - self.compileStructBody(p, sp, vt) - } -} - -func (self *Compiler) compileStructBody(p *ir.Program, sp int, vt reflect.Type) { - p.Tag(sp) - p.Int(ir.OP_byte, '{') - p.Add(ir.OP_save) - p.Add(ir.OP_cond_set) - - /* compile each field */ - for _, fv := range resolver.ResolveStruct(vt) { - var s []int - var o resolver.Offset - - /* "omitempty" for arrays */ - if fv.Type.Kind() == reflect.Array { - if fv.Type.Len() == 0 && (fv.Opts&resolver.F_omitempty) != 0 { - continue - } - } - - /* index to the field */ - for _, o = range fv.Path { - if p.Int(ir.OP_index, int(o.Size)); o.Kind == resolver.F_deref { - s = append(s, p.PC()) - p.Add(ir.OP_is_nil) - p.Add(ir.OP_deref) - } - } - - /* check for "omitempty" option */ - if fv.Type.Kind() != reflect.Struct && fv.Type.Kind() != reflect.Array && (fv.Opts&resolver.F_omitempty) != 0 { - s = append(s, p.PC()) - self.compileStructFieldZero(p, fv.Type) - } - - /* add the comma if not the first element */ - i := p.PC() - p.Add(ir.OP_cond_testc) - p.Int(ir.OP_byte, ',') - p.Pin(i) - - /* compile the key and value */ - ft := fv.Type - p.Str(ir.OP_text, Quote(fv.Name)+":") - - /* check for "stringnize" option */ - if (fv.Opts & resolver.F_stringize) == 0 { - self.compileOne(p, sp+1, ft, self.pv) - } else { - self.compileStructFieldStr(p, sp+1, ft) - } - - /* patch the skipping jumps and reload the struct pointer */ - p.Rel(s) - p.Add(ir.OP_load) - } - - /* end of object */ - p.Add(ir.OP_drop) - p.Int(ir.OP_byte, '}') -} - -func (self *Compiler) compileStructFieldStr(p *ir.Program, sp int, vt reflect.Type) { - // NOTICE: according to encoding/json, Marshaler type has higher priority than string option - // see issue: - if self.tryCompileMarshaler(p, vt, self.pv) { - return - } - - pc := -1 - ft := vt - sv := false - - /* dereference the pointer if needed */ - if ft.Kind() == reflect.Ptr { - ft = ft.Elem() - } - - /* check if it can be stringized */ - switch ft.Kind() { - case reflect.Bool: - sv = true - case reflect.Int: - sv = true - case reflect.Int8: - sv = true - case reflect.Int16: - sv = true - case reflect.Int32: - sv = true - case reflect.Int64: - sv = true - case reflect.Uint: - sv = true - case reflect.Uint8: - sv = true - case reflect.Uint16: - sv = true - case reflect.Uint32: - sv = true - case reflect.Uint64: - sv = true - case reflect.Uintptr: - sv = true - case reflect.Float32: - sv = true - case reflect.Float64: - sv = true - case reflect.String: - sv = true - } - - /* if it's not, ignore the "string" and follow the regular path */ - if !sv { - self.compileOne(p, sp, vt, self.pv) - return - } - - /* dereference the pointer */ - if vt.Kind() == reflect.Ptr { - pc = p.PC() - vt = vt.Elem() - p.Add(ir.OP_is_nil) - p.Add(ir.OP_deref) - } - - /* special case of a double-quoted string */ - if ft != vars.JsonNumberType && ft.Kind() == reflect.String { - p.Add(ir.OP_quote) - } else { - self.compileStructFieldQuoted(p, sp, vt) - } - - /* the "null" case of the pointer */ - if pc != -1 { - e := p.PC() - p.Add(ir.OP_goto) - p.Pin(pc) - p.Add(ir.OP_null) - p.Pin(e) - } -} - -func (self *Compiler) compileStructFieldZero(p *ir.Program, vt reflect.Type) { - switch vt.Kind() { - case reflect.Bool: - p.Add(ir.OP_is_zero_1) - case reflect.Int: - p.Add(ir.OP_is_zero_ints()) - case reflect.Int8: - p.Add(ir.OP_is_zero_1) - case reflect.Int16: - p.Add(ir.OP_is_zero_2) - case reflect.Int32: - p.Add(ir.OP_is_zero_4) - case reflect.Int64: - p.Add(ir.OP_is_zero_8) - case reflect.Uint: - p.Add(ir.OP_is_zero_ints()) - case reflect.Uint8: - p.Add(ir.OP_is_zero_1) - case reflect.Uint16: - p.Add(ir.OP_is_zero_2) - case reflect.Uint32: - p.Add(ir.OP_is_zero_4) - case reflect.Uint64: - p.Add(ir.OP_is_zero_8) - case reflect.Uintptr: - p.Add(ir.OP_is_nil) - case reflect.Float32: - p.Add(ir.OP_is_zero_4) - case reflect.Float64: - p.Add(ir.OP_is_zero_8) - case reflect.String: - p.Add(ir.OP_is_nil_p1) - case reflect.Interface: - p.Add(ir.OP_is_nil) - case reflect.Map: - p.Add(ir.OP_is_zero_map) - case reflect.Ptr: - p.Add(ir.OP_is_nil) - case reflect.Slice: - p.Add(ir.OP_is_nil_p1) - default: - panic(vars.Error_type(vt)) - } -} - -func (self *Compiler) compileStructFieldQuoted(p *ir.Program, sp int, vt reflect.Type) { - p.Int(ir.OP_byte, '"') - self.compileOne(p, sp, vt, self.pv) - p.Int(ir.OP_byte, '"') -} - -func (self *Compiler) compileInterface(p *ir.Program, vt reflect.Type) { - x := p.PC() - p.Add(ir.OP_is_nil_p1) - - /* iface and efaces are different */ - if vt.NumMethod() == 0 { - p.Add(ir.OP_eface) - } else { - p.Add(ir.OP_iface) - } - - /* the "null" value */ - e := p.PC() - p.Add(ir.OP_goto) - p.Pin(x) - p.Add(ir.OP_null) - p.Pin(e) -} - -func (self *Compiler) compileMarshaler(p *ir.Program, op ir.Op, vt reflect.Type, mt reflect.Type) { - pc := p.PC() - vk := vt.Kind() - - /* direct receiver */ - if vk != reflect.Ptr { - addMarshalerOp(p, op, vt, mt) - return - } - /* value receiver with a pointer type, check for nil before calling the marshaler */ - p.Add(ir.OP_is_nil) - - addMarshalerOp(p, op, vt, mt) - - i := p.PC() - p.Add(ir.OP_goto) - p.Pin(pc) - p.Add(ir.OP_null) - p.Pin(i) -} - -func addMarshalerOp(p *ir.Program, op ir.Op, vt reflect.Type, mt reflect.Type) { - if vars.UseVM { - itab := rt.GetItab(rt.IfaceType(rt.UnpackType(mt)), rt.UnpackType(vt), true) - p.Vtab(op, vt, itab) - } else { - // OPT: get itab here - p.Rtt(op, vt) - } -} diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/encode_norace.go b/vendor/github.com/bytedance/sonic/internal/encoder/encode_norace.go deleted file mode 100644 index c53206433..000000000 --- a/vendor/github.com/bytedance/sonic/internal/encoder/encode_norace.go +++ /dev/null @@ -1,24 +0,0 @@ -//go:build !race -// +build !race - -/* - * Copyright 2021 ByteDance Inc. - * - * 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 encoder - -func encodeIntoCheckRace(buf *[]byte, val interface{}, opts Options) error { - return encodeInto(buf, val, opts) -} diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/encode_race.go b/vendor/github.com/bytedance/sonic/internal/encoder/encode_race.go deleted file mode 100644 index c373c55f9..000000000 --- a/vendor/github.com/bytedance/sonic/internal/encoder/encode_race.go +++ /dev/null @@ -1,54 +0,0 @@ -//go:build race -// +build race - -/* - * Copyright 2021 ByteDance Inc. - * - * 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 encoder - -import ( - `encoding/json` - - `github.com/bytedance/sonic/internal/rt` -) - - -func helpDetectDataRace(val interface{}) { - var out []byte - defer func() { - if v := recover(); v != nil { - // NOTICE: help user to locate where panic occurs - println("panic when encoding on: ", truncate(out)) - panic(v) - } - }() - out, _ = json.Marshal(val) -} - -func encodeIntoCheckRace(buf *[]byte, val interface{}, opts Options) error { - err := encodeInto(buf, val, opts) - /* put last to make the panic from sonic will always be caught at first */ - helpDetectDataRace(val) - return err -} - -func truncate(json []byte) string { - if len(json) <= 256 { - return rt.Mem2Str(json) - } else { - return rt.Mem2Str(json[len(json)-256:]) - } -} diff --git a/vendor/github.com/bytedance/sonic/internal/encoder/encoder.go b/vendor/github.com/bytedance/sonic/internal/encoder/encoder.go deleted file mode 100644 index 4cba1a168..000000000 --- a/vendor/github.com/bytedance/sonic/internal/encoder/encoder.go +++ /dev/null @@ -1,318 +0,0 @@ -/* - * Copyright 2021 ByteDance Inc. - * - * 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 encoder - -import ( - "bytes" - "encoding/json" - "reflect" - "runtime" - "unsafe" - - "github.com/bytedance/sonic/utf8" - "github.com/bytedance/sonic/internal/encoder/alg" - "github.com/bytedance/sonic/internal/encoder/vars" - "github.com/bytedance/sonic/internal/rt" - "github.com/bytedance/sonic/option" -) - -// Options is a set of encoding options. -type Options uint64 - -const ( - // SortMapKeys indicates that the keys of a map needs to be sorted - // before serializing into JSON. - // WARNING: This hurts performance A LOT, USE WITH CARE. - SortMapKeys Options = 1 << alg.BitSortMapKeys - - // EscapeHTML indicates encoder to escape all HTML characters - // after serializing into JSON (see https://pkg.go.dev/encoding/json#HTMLEscape). - // WARNING: This hurts performance A LOT, USE WITH CARE. - EscapeHTML Options = 1 << alg.BitEscapeHTML - - // CompactMarshaler indicates that the output JSON from json.Marshaler - // is always compact and needs no validation - CompactMarshaler Options = 1 << alg.BitCompactMarshaler - - // NoQuoteTextMarshaler indicates that the output text from encoding.TextMarshaler - // is always escaped string and needs no quoting - NoQuoteTextMarshaler Options = 1 << alg.BitNoQuoteTextMarshaler - - // NoNullSliceOrMap indicates all empty Array or Object are encoded as '[]' or '{}', - // instead of 'null'. - // NOTE: The priority of this option is lower than json tag `omitempty`. - NoNullSliceOrMap Options = 1 << alg.BitNoNullSliceOrMap - - // ValidateString indicates that encoder should validate the input string - // before encoding it into JSON. - ValidateString Options = 1 << alg.BitValidateString - - // NoValidateJSONMarshaler indicates that the encoder should not validate the output string - // after encoding the JSONMarshaler to JSON. - NoValidateJSONMarshaler Options = 1 << alg.BitNoValidateJSONMarshaler - - // NoEncoderNewline indicates that the encoder should not add a newline after every message - NoEncoderNewline Options = 1 << alg.BitNoEncoderNewline - - // CompatibleWithStd is used to be compatible with std encoder. - CompatibleWithStd Options = SortMapKeys | EscapeHTML | CompactMarshaler - - // Encode Infinity or Nan float into `null`, instead of returning an error. - EncodeNullForInfOrNan Options = 1 << alg.BitEncodeNullForInfOrNan -) - -// Encoder represents a specific set of encoder configurations. -type Encoder struct { - Opts Options - prefix string - indent string -} - -// Encode returns the JSON encoding of v. -func (self *Encoder) Encode(v interface{}) ([]byte, error) { - if self.indent != "" || self.prefix != "" { - return EncodeIndented(v, self.prefix, self.indent, self.Opts) - } - return Encode(v, self.Opts) -} - -// SortKeys enables the SortMapKeys option. -func (self *Encoder) SortKeys() *Encoder { - self.Opts |= SortMapKeys - return self -} - -// SetEscapeHTML specifies if option EscapeHTML opens -func (self *Encoder) SetEscapeHTML(f bool) { - if f { - self.Opts |= EscapeHTML - } else { - self.Opts &= ^EscapeHTML - } -} - -// SetValidateString specifies if option ValidateString opens -func (self *Encoder) SetValidateString(f bool) { - if f { - self.Opts |= ValidateString - } else { - self.Opts &= ^ValidateString - } -} - -// SetNoValidateJSONMarshaler specifies if option NoValidateJSONMarshaler opens -func (self *Encoder) SetNoValidateJSONMarshaler(f bool) { - if f { - self.Opts |= NoValidateJSONMarshaler - } else { - self.Opts &= ^NoValidateJSONMarshaler - } -} - -// SetNoEncoderNewline specifies if option NoEncoderNewline opens -func (self *Encoder) SetNoEncoderNewline(f bool) { - if f { - self.Opts |= NoEncoderNewline - } else { - self.Opts &= ^NoEncoderNewline - } -} - - -// SetCompactMarshaler specifies if option CompactMarshaler opens -func (self *Encoder) SetCompactMarshaler(f bool) { - if f { - self.Opts |= CompactMarshaler - } else { - self.Opts &= ^CompactMarshaler - } -} - -// SetNoQuoteTextMarshaler specifies if option NoQuoteTextMarshaler opens -func (self *Encoder) SetNoQuoteTextMarshaler(f bool) { - if f { - self.Opts |= NoQuoteTextMarshaler - } else { - self.Opts &= ^NoQuoteTextMarshaler - } -} - -// SetIndent instructs the encoder to format each subsequent encoded -// value as if indented by the package-level function EncodeIndent(). -// Calling SetIndent("", "") disables indentation. -func (enc *Encoder) SetIndent(prefix, indent string) { - enc.prefix = prefix - enc.indent = indent -} - -// Quote returns the JSON-quoted version of s. -func Quote(s string) string { - buf := make([]byte, 0, len(s)+2) - buf = alg.Quote(buf, s, false) - return rt.Mem2Str(buf) -} - -// Encode returns the JSON encoding of val, encoded with opts. -func Encode(val interface{}, opts Options) ([]byte, error) { - var ret []byte - - buf := vars.NewBytes() - err := encodeIntoCheckRace(buf, val, opts) - - /* check for errors */ - if err != nil { - vars.FreeBytes(buf) - return nil, err - } - - /* htmlescape or correct UTF-8 if opts enable */ - old := buf - *buf = encodeFinish(*old, opts) - pbuf := ((*rt.GoSlice)(unsafe.Pointer(buf))).Ptr - pold := ((*rt.GoSlice)(unsafe.Pointer(old))).Ptr - - /* return when allocated a new buffer */ - if pbuf != pold { - vars.FreeBytes(old) - return *buf, nil - } - - /* make a copy of the result */ - if rt.CanSizeResue(cap(*buf)) { - ret = make([]byte, len(*buf)) - copy(ret, *buf) - vars.FreeBytes(buf) - } else { - ret = *buf - } - - /* return the buffer into pool */ - return ret, nil -} - -// EncodeInto is like Encode but uses a user-supplied buffer instead of allocating -// a new one. -func EncodeInto(buf *[]byte, val interface{}, opts Options) error { - err := encodeIntoCheckRace(buf, val, opts) - if err != nil { - return err - } - *buf = encodeFinish(*buf, opts) - return err -} - -func encodeInto(buf *[]byte, val interface{}, opts Options) error { - stk := vars.NewStack() - efv := rt.UnpackEface(val) - err := encodeTypedPointer(buf, efv.Type, &efv.Value, stk, uint64(opts)) - - /* return the stack into pool */ - if err != nil { - vars.ResetStack(stk) - } - vars.FreeStack(stk) - - /* avoid GC ahead */ - runtime.KeepAlive(buf) - runtime.KeepAlive(efv) - return err -} - -func encodeFinish(buf []byte, opts Options) []byte { - if opts & EscapeHTML != 0 { - buf = HTMLEscape(nil, buf) - } - if (opts & ValidateString != 0) && !utf8.Validate(buf) { - buf = utf8.CorrectWith(nil, buf, `\ufffd`) - } - return buf -} - - -// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 -// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 -// so that the JSON will be safe to embed inside HTML