summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-mangler/mangle.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2023-01-06 10:16:09 +0000
committerLibravatar GitHub <noreply@github.com>2023-01-06 11:16:09 +0100
commitadbc87700a5bc7a95883ba5b9688d8b946a8db48 (patch)
tree6030ff70d3eb0b9a0b8fc7d5fca378a77033d546 /vendor/codeberg.org/gruf/go-mangler/mangle.go
parent[chore] Update/add license headers for 2023 (#1304) (diff)
downloadgotosocial-adbc87700a5bc7a95883ba5b9688d8b946a8db48.tar.xz
[chore] pull in latest go-cache, go-runners versions (#1306)
Signed-off-by: kim <grufwub@gmail.com> Signed-off-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-mangler/mangle.go')
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/mangle.go64
1 files changed, 43 insertions, 21 deletions
diff --git a/vendor/codeberg.org/gruf/go-mangler/mangle.go b/vendor/codeberg.org/gruf/go-mangler/mangle.go
index 7158893ae..983216003 100644
--- a/vendor/codeberg.org/gruf/go-mangler/mangle.go
+++ b/vendor/codeberg.org/gruf/go-mangler/mangle.go
@@ -3,15 +3,13 @@ package mangler
import (
"encoding/binary"
"reflect"
+ "sync"
"unsafe"
-
- "github.com/cespare/xxhash"
- "github.com/cornelk/hashmap"
)
var (
// manglers is a map of runtime type ptrs => Mangler functions.
- manglers = hashmap.New[uintptr, Mangler]()
+ manglers = sync.Map{}
// bin is a short-hand for our chosen byteorder encoding.
bin = binary.LittleEndian
@@ -36,12 +34,38 @@ type Mangler func(buf []byte, value any) []byte
type rMangler func(buf []byte, value reflect.Value) []byte
// Get will fetch the Mangler function for given runtime type.
-func Get(t reflect.Type) (Mangler, bool) {
- if t == nil {
- return nil, false
- }
+// Note that the returned mangler will be a no-op in the case
+// that an incorrect type is passed as the value argument.
+func Get(t reflect.Type) Mangler {
+ var mng Mangler
+
+ // Get raw runtime type ptr
uptr := uintptr(iface_value(t))
- return manglers.Get(uptr)
+
+ // Look for a cached mangler
+ v, ok := manglers.Load(uptr)
+
+ if !ok {
+ // Load mangler function
+ mng = loadMangler(nil, t)
+ } else {
+ // cast cached value
+ mng = v.(Mangler)
+ }
+
+ return func(buf []byte, value any) []byte {
+ // Type check passed value against original arg type.
+ if vt := reflect.TypeOf(value); vt != t {
+ return buf
+ }
+
+ // First write the type ptr (this adds
+ // a unique prefix for each runtime type).
+ buf = mangle_platform_int(buf, uptr)
+
+ // Finally, mangle value
+ return mng(buf, value)
+ }
}
// Register will register the given Mangler function for use with vars of given runtime type. This allows
@@ -57,17 +81,19 @@ func Register(t reflect.Type, m Mangler) {
uptr := uintptr(iface_value(t))
// Ensure this is a unique encoder
- if _, ok := manglers.Get(uptr); ok {
+ if _, ok := manglers.Load(uptr); ok {
panic("already registered mangler for type: " + t.String())
}
// Cache this encoder func
- manglers.Set(uptr, m)
+ manglers.Store(uptr, m)
}
// Append will append the mangled form of input value 'a' to buffer 'b'.
// See mangler.String() for more information on mangled output.
func Append(b []byte, a any) []byte {
+ var mng Mangler
+
// Get reflect type of 'a'
t := reflect.TypeOf(a)
@@ -75,12 +101,15 @@ func Append(b []byte, a any) []byte {
uptr := uintptr(iface_value(t))
// Look for a cached mangler
- mng, ok := manglers.Get(uptr)
+ v, ok := manglers.Load(uptr)
if !ok {
// Load mangler into cache
- mng = loadMangler(a, t)
- manglers.Set(uptr, mng)
+ mng = loadMangler(nil, t)
+ manglers.Store(uptr, mng)
+ } else {
+ // cast cached value
+ mng = v.(Mangler)
}
// First write the type ptr (this adds
@@ -123,10 +152,3 @@ func String(a any) string {
b := Append(make([]byte, 0, 32), a)
return *(*string)(unsafe.Pointer(&b))
}
-
-// Hash returns the xxHash digest of the result of mangler.Append(nil, 'a').
-func Hash(a any) uint64 {
- b := make([]byte, 0, 32)
- b = Append(b, a)
- return xxhash.Sum64(b)
-}