diff options
author | 2024-06-21 15:43:17 +0000 | |
---|---|---|
committer | 2024-06-21 16:43:17 +0100 | |
commit | b93087ceb4f508c1d5262f363a21bdea3e502c31 (patch) | |
tree | 71ff80a06ee447f4694fd242cb84d9010fc46ccd /vendor/codeberg.org/gruf/go-mangler/mangle.go | |
parent | [feature] add worker task serialization logic (#2989) (diff) | |
download | gotosocial-b93087ceb4f508c1d5262f363a21bdea3e502c31.tar.xz |
[chore] update go-structr and go-mangler to no longer rely on modern-go/reflect2 (#3026)
* updates go-structr and go-mangler to no longer rely on modern-go/reflect2 (*phew* now we're go1.23 safe)
* update go-structr version
* bump go-structr to improve memory usage (v. slightly) in certain conditions
Diffstat (limited to 'vendor/codeberg.org/gruf/go-mangler/mangle.go')
-rw-r--r-- | vendor/codeberg.org/gruf/go-mangler/mangle.go | 44 |
1 files changed, 10 insertions, 34 deletions
diff --git a/vendor/codeberg.org/gruf/go-mangler/mangle.go b/vendor/codeberg.org/gruf/go-mangler/mangle.go index b44d26dc5..576286682 100644 --- a/vendor/codeberg.org/gruf/go-mangler/mangle.go +++ b/vendor/codeberg.org/gruf/go-mangler/mangle.go @@ -10,15 +10,11 @@ import ( // type ptrs => Mangler functions. var manglers sync.Map -// Mangled is an interface that allows any type to implement a custom -// Mangler function to improve performance when mangling this type. -type Mangled interface{ Mangle(buf []byte) []byte } - // Mangler is a function that will take an input interface value of known // type, and append it in mangled serialized form to the given byte buffer. // While the value type is an interface, the Mangler functions are accessed // by the value's runtime type pointer, allowing the input value type to be known. -type Mangler func(buf []byte, value any) []byte +type Mangler func(buf []byte, ptr unsafe.Pointer) []byte // Get will fetch the Mangler function for given runtime type. // Note that the returned mangler will be a no-op in the case @@ -34,27 +30,19 @@ func Get(t reflect.Type) Mangler { if !ok { // Load mangler function - mng = loadMangler(nil, t) + mng = loadMangler(t) } else { // cast cached value mng = v.(Mangler) } - // Get platform int mangler func. - mangle_int := mangle_platform_int() - - return func(buf []byte, value any) []byte { - // Type check passed against original type. - if vt := reflect.TypeOf(value); vt != t { - return buf - } - + return func(buf []byte, ptr unsafe.Pointer) []byte { // First write the type ptr (this adds // a unique prefix for each runtime type). - buf = mangle_int(buf, uptr) + buf = append_uint64(buf, uint64(uptr)) // Finally, mangle value - return mng(buf, value) + return mng(buf, ptr) } } @@ -94,23 +82,21 @@ func Append(b []byte, a any) []byte { v, ok := manglers.Load(uptr) if !ok { - // Load mangler into cache - mng = loadMangler(nil, t) + // Load into cache + mng = loadMangler(t) manglers.Store(uptr, mng) } else { // cast cached value mng = v.(Mangler) } - // Get platform int mangler func. - mangle_int := mangle_platform_int() - // First write the type ptr (this adds // a unique prefix for each runtime type). - b = mangle_int(b, uptr) + b = append_uint64(b, uint64(uptr)) // Finally, mangle value - return mng(b, a) + ptr := eface_data(a) + return mng(b, ptr) } // String will return the mangled format of input value 'a'. This @@ -136,18 +122,8 @@ func Append(b []byte, a any) []byte { // - complex64,complex128 // - arbitrary structs // - all type aliases of above -// - time.Time{} -// - url.URL{} -// - net.IPAddr{} -// - netip.Addr{}, netip.AddrPort{} -// - mangler.Mangled{} -// - fmt.Stringer{} -// - json.Marshaler{} -// - encoding.BinaryMarshaler{} -// - encoding.TextMarshaler{} // - all pointers to the above // - all slices / arrays of the above -// - all map keys / values of the above func String(a any) string { b := Append(make([]byte, 0, 32), a) return *(*string)(unsafe.Pointer(&b)) |