summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-01-26 12:14:10 +0000
committerLibravatar GitHub <noreply@github.com>2024-01-26 12:14:10 +0000
commit07207e71e932b0d4a38253e069be6090872c4e3f (patch)
treebee71828fe2e6d3086bcd36e175301388fc95972 /vendor/codeberg.org
parent[docs] Fix log-timestamp-format (#2572) (diff)
downloadgotosocial-07207e71e932b0d4a38253e069be6090872c4e3f.tar.xz
[performance] cache library performance enhancements (updates go-structr => v0.2.0) (#2575)
* update go-structr => v0.2.0 * update readme * whoops, fix the link
Diffstat (limited to 'vendor/codeberg.org')
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/LICENSE9
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/README.md36
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/helpers.go144
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/init.go119
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/load.go333
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/mangle.go154
-rw-r--r--vendor/codeberg.org/gruf/go-mangler/manglers.go265
-rw-r--r--vendor/codeberg.org/gruf/go-structr/README.md70
-rw-r--r--vendor/codeberg.org/gruf/go-structr/cache.go83
-rw-r--r--vendor/codeberg.org/gruf/go-structr/hash.go370
-rw-r--r--vendor/codeberg.org/gruf/go-structr/hasher.go176
-rw-r--r--vendor/codeberg.org/gruf/go-structr/index.go26
-rw-r--r--vendor/codeberg.org/gruf/go-structr/key.go204
-rw-r--r--vendor/codeberg.org/gruf/go-structr/util.go50
14 files changed, 680 insertions, 1359 deletions
diff --git a/vendor/codeberg.org/gruf/go-mangler/LICENSE b/vendor/codeberg.org/gruf/go-mangler/LICENSE
deleted file mode 100644
index dffbdf0c9..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/LICENSE
+++ /dev/null
@@ -1,9 +0,0 @@
-MIT License
-
-Copyright (c) 2023 gruf
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/codeberg.org/gruf/go-mangler/README.md b/vendor/codeberg.org/gruf/go-mangler/README.md
deleted file mode 100644
index d0de88a43..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/README.md
+++ /dev/null
@@ -1,36 +0,0 @@
-# go-mangler
-
-[Documentation](https://pkg.go.dev/codeberg.org/gruf/go-mangler).
-
-To put it simply is a bit of an odd library. It aims to provide incredibly fast, unique string outputs for all default supported input data types during a given runtime instance.
-
-It is useful, for example, for use as part of larger abstractions involving hashmaps. That was my particular usecase anyways...
-
-This package does make liberal use of the "unsafe" package.
-
-Benchmarks are below. Those with missing values panicked during our set of benchmarks, usually a case of not handling nil values elegantly. Please note the more important thing to notice here is the relative difference in benchmark scores, the actual `ns/op`,`B/op`,`allocs/op` accounts for running through over 80 possible test cases, including some not-ideal situations.
-
-The choice of libraries in the benchmark are just a selection of libraries that could be used in a similar manner to this one, i.e. serializing in some manner.
-
-```
-goos: linux
-goarch: amd64
-pkg: codeberg.org/gruf/go-mangler
-cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
-BenchmarkMangle
-BenchmarkMangle-8 533011 2003 ns/op 1168 B/op 120 allocs/op
-BenchmarkMangleKnown
-BenchmarkMangleKnown-8 817060 1458 ns/op 1168 B/op 120 allocs/op
-BenchmarkJSON
-BenchmarkJSON-8 188637 5899 ns/op 4211 B/op 142 allocs/op
-BenchmarkFmt
-BenchmarkFmt-8 162735 7053 ns/op 2257 B/op 161 allocs/op
-BenchmarkFxmackerCbor
-BenchmarkFxmackerCbor-8 362403 3336 ns/op 1496 B/op 122 allocs/op
-BenchmarkMitchellhHashStructure
-BenchmarkMitchellhHashStructure-8 113982 10079 ns/op 8443 B/op 961 allocs/op
-BenchmarkCnfStructhash
-BenchmarkCnfStructhash-8 7162 167613 ns/op 288619 B/op 5841 allocs/op
-PASS
-ok codeberg.org/gruf/go-mangler 11.352s
-``` \ No newline at end of file
diff --git a/vendor/codeberg.org/gruf/go-mangler/helpers.go b/vendor/codeberg.org/gruf/go-mangler/helpers.go
deleted file mode 100644
index 6658fbef4..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/helpers.go
+++ /dev/null
@@ -1,144 +0,0 @@
-package mangler
-
-import (
- "reflect"
- "unsafe"
-)
-
-type (
- // serializing interfacing types.
- stringer interface{ String() string }
- binarymarshaler interface{ MarshalBinary() ([]byte, error) }
- textmarshaler interface{ MarshalText() ([]byte, error) }
- jsonmarshaler interface{ MarshalJSON() ([]byte, error) }
-)
-
-func append_uint16(b []byte, u uint16) []byte {
- return append(b, // LE
- byte(u),
- byte(u>>8),
- )
-}
-
-func append_uint32(b []byte, u uint32) []byte {
- return append(b, // LE
- byte(u),
- byte(u>>8),
- byte(u>>16),
- byte(u>>24),
- )
-}
-
-func append_uint64(b []byte, u uint64) []byte {
- return append(b, // LE
- byte(u),
- byte(u>>8),
- byte(u>>16),
- byte(u>>24),
- byte(u>>32),
- byte(u>>40),
- byte(u>>48),
- byte(u>>56),
- )
-}
-
-func deref_ptr_mangler(mangle Mangler, count int) rMangler {
- return func(buf []byte, v reflect.Value) []byte {
- for i := 0; i < count; i++ {
- // Check for nil
- if v.IsNil() {
- buf = append(buf, '0')
- return buf
- }
-
- // Further deref ptr
- buf = append(buf, '1')
- v = v.Elem()
- }
-
- // Mangle fully deref'd ptr
- return mangle(buf, v.Interface())
- }
-}
-
-func deref_ptr_rmangler(mangle rMangler, count int) rMangler {
- return func(buf []byte, v reflect.Value) []byte {
- for i := 0; i < count; i++ {
- // Check for nil
- if v.IsNil() {
- buf = append(buf, '0')
- return buf
- }
-
- // Further deref ptr
- buf = append(buf, '1')
- v = v.Elem()
- }
-
- // Mangle fully deref'd ptr
- return mangle(buf, v)
- }
-}
-
-func array_to_slice_mangler(mangle Mangler) rMangler {
- return func(buf []byte, v reflect.Value) []byte {
- // Get slice of whole array
- v = v.Slice(0, v.Len())
-
- // Mangle as known slice type
- return mangle(buf, v.Interface())
- }
-}
-
-func iter_array_mangler(mangle Mangler) rMangler {
- return func(buf []byte, v reflect.Value) []byte {
- n := v.Len()
- for i := 0; i < n; i++ {
- buf = mangle(buf, v.Index(i).Interface())
- buf = append(buf, ',')
- }
- if n > 0 {
- buf = buf[:len(buf)-1]
- }
- return buf
- }
-}
-
-func iter_array_rmangler(mangle rMangler) rMangler {
- return func(buf []byte, v reflect.Value) []byte {
- n := v.Len()
- for i := 0; i < n; i++ {
- buf = mangle(buf, v.Index(i))
- buf = append(buf, ',')
- }
- if n > 0 {
- buf = buf[:len(buf)-1]
- }
- return buf
- }
-}
-
-func iter_map_rmangler(kMangle, vMangle rMangler) rMangler {
- return func(buf []byte, v reflect.Value) []byte {
- r := v.MapRange()
- for r.Next() {
- buf = kMangle(buf, r.Key())
- buf = append(buf, ':')
- buf = vMangle(buf, r.Value())
- buf = append(buf, ',')
- }
- if v.Len() > 0 {
- buf = buf[:len(buf)-1]
- }
- return buf
- }
-}
-
-// iface_value returns the raw value ptr for input boxed within interface{} type.
-func iface_value(a any) unsafe.Pointer {
- type eface struct {
- Type unsafe.Pointer
- Value unsafe.Pointer
- }
- return (*eface)(unsafe.Pointer(&a)).Value
-}
diff --git a/vendor/codeberg.org/gruf/go-mangler/init.go b/vendor/codeberg.org/gruf/go-mangler/init.go
deleted file mode 100644
index 2c20e6761..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/init.go
+++ /dev/null
@@ -1,119 +0,0 @@
-package mangler
-
-import (
- "net"
- "net/netip"
- "net/url"
- "reflect"
- "time"
- _ "unsafe"
-)
-
-func init() {
- // Register standard library performant manglers.
- Register(reflect.TypeOf(net.IPAddr{}), mangle_ipaddr)
- Register(reflect.TypeOf(&net.IPAddr{}), mangle_ipaddr_ptr)
- Register(reflect.TypeOf(netip.Addr{}), mangle_addr)
- Register(reflect.TypeOf(&netip.Addr{}), mangle_addr_ptr)
- Register(reflect.TypeOf(netip.AddrPort{}), mangle_addrport)
- Register(reflect.TypeOf(&netip.AddrPort{}), mangle_addrport_ptr)
- Register(reflect.TypeOf(time.Time{}), mangle_time)
- Register(reflect.TypeOf(&time.Time{}), mangle_time_ptr)
- Register(reflect.TypeOf(url.URL{}), mangle_url)
- Register(reflect.TypeOf(&url.URL{}), mangle_url_ptr)
-}
-
-//go:linkname time_sec time.(*Time).sec
-func time_sec(*time.Time) int64
-
-//go:linkname time_nsec time.(*Time).nsec
-func time_nsec(*time.Time) int32
-
-//go:linkname time_stripMono time.(*Time).stripMono
-func time_stripMono(*time.Time)
-
-func mangle_url(buf []byte, a any) []byte {
- u := (*url.URL)(iface_value(a))
- return append(buf, u.String()...)
-}
-
-func mangle_url_ptr(buf []byte, a any) []byte {
- if ptr := (*url.URL)(iface_value(a)); ptr != nil {
- s := ptr.String()
- buf = append(buf, '1')
- return append(buf, s...)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_time(buf []byte, a any) []byte {
- t := *(*time.Time)(iface_value(a))
- time_stripMono(&t) // force non-monotonic time value.
- buf = append_uint64(buf, uint64(time_sec(&t)))
- buf = append_uint32(buf, uint32(time_nsec(&t)))
- return buf
-}
-
-func mangle_time_ptr(buf []byte, a any) []byte {
- if ptr := (*time.Time)(iface_value(a)); ptr != nil {
- t := *ptr
- buf = append(buf, '1')
- time_stripMono(&t) // force non-monotonic time value.
- buf = append_uint64(buf, uint64(time_sec(&t)))
- buf = append_uint32(buf, uint32(time_nsec(&t)))
- return buf
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_ipaddr(buf []byte, a any) []byte {
- i := *(*net.IPAddr)(iface_value(a))
- buf = append(buf, i.IP...)
- buf = append(buf, i.Zone...)
- return buf
-}
-
-func mangle_ipaddr_ptr(buf []byte, a any) []byte {
- if ptr := (*net.IPAddr)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- buf = append(buf, ptr.IP...)
- buf = append(buf, ptr.Zone...)
- return buf
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_addr(buf []byte, a any) []byte {
- i := (*netip.Addr)(iface_value(a))
- b, _ := i.MarshalBinary()
- return append(buf, b...)
-}
-
-func mangle_addr_ptr(buf []byte, a any) []byte {
- if ptr := (*netip.Addr)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- b, _ := ptr.MarshalBinary()
- return append(buf, b...)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_addrport(buf []byte, a any) []byte {
- i := (*netip.AddrPort)(iface_value(a))
- b, _ := i.MarshalBinary()
- return append(buf, b...)
-}
-
-func mangle_addrport_ptr(buf []byte, a any) []byte {
- if ptr := (*netip.AddrPort)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- b, _ := ptr.MarshalBinary()
- return append(buf, b...)
- }
- buf = append(buf, '0')
- return buf
-}
diff --git a/vendor/codeberg.org/gruf/go-mangler/load.go b/vendor/codeberg.org/gruf/go-mangler/load.go
deleted file mode 100644
index 752e5d337..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/load.go
+++ /dev/null
@@ -1,333 +0,0 @@
-package mangler
-
-import (
- "reflect"
-)
-
-// loadMangler is the top-most Mangler load function. It guarantees that a Mangler
-// function will be returned for given value interface{} and reflected type. Else panics.
-func loadMangler(a any, t reflect.Type) Mangler {
- // Load mangler function
- mng, rmng := load(a, t)
-
- if mng != nil {
- // Use preferred mangler.
- return mng
- }
-
- if rmng != nil {
- // Wrap reflect mangler to handle iface
- return func(buf []byte, a any) []byte {
- return rmng(buf, reflect.ValueOf(a))
- }
- }
-
- // No mangler function could be determined
- panic("cannot mangle type: " + t.String())
-}
-
-// load will load a Mangler or reflect Mangler for given type and iface 'a'.
-// Note: allocates new interface value if nil provided, i.e. if coming via reflection.
-func load(a any, t reflect.Type) (Mangler, rMangler) {
- if t == nil {
- // There is no reflect type to search by
- panic("cannot mangle nil interface{} type")
- }
-
- if a == nil {
- // Alloc new iface instance
- v := reflect.New(t).Elem()
- a = v.Interface()
- }
-
- // Check for Mangled implementation.
- if _, ok := a.(Mangled); ok {
- return mangle_mangled, nil
- }
-
- // Search mangler by reflection.
- mng, rmng := loadReflect(t)
- if mng != nil {
- return mng, nil
- }
-
- // Prefer iface mangler, else, reflected.
- return loadIface(a), rmng
-}
-
-// loadIface is used as a near-last-resort interface{} type switch
-// loader for types implementating other known (slower) functions.
-func loadIface(a any) Mangler {
- switch a.(type) {
- case binarymarshaler:
- return mangle_binary
- case stringer:
- return mangle_stringer
- case textmarshaler:
- return mangle_text
- case jsonmarshaler:
- return mangle_json
- default:
- return nil
- }
-}
-
-// loadReflect will load a Mangler (or rMangler) function for the given reflected type info.
-// NOTE: this is used as the top level load function for nested reflective searches.
-func loadReflect(t reflect.Type) (Mangler, rMangler) {
- switch t.Kind() {
- case reflect.Pointer:
- return loadReflectPtr(t.Elem())
-
- case reflect.String:
- return mangle_string, nil
-
- case reflect.Array:
- return nil, loadReflectArray(t.Elem())
-
- case reflect.Slice:
- // Element type
- et := t.Elem()
-
- // Preferably look for known slice mangler func
- if mng := loadReflectKnownSlice(et); mng != nil {
- return mng, nil
- }
-
- // Else handle as array elements
- return nil, loadReflectArray(et)
-
- case reflect.Map:
- return nil, loadReflectMap(t.Key(), t.Elem())
-
- case reflect.Bool:
- return mangle_bool, nil
-
- case reflect.Int,
- reflect.Uint,
- reflect.Uintptr:
- return mangle_platform_int, nil
-
- case reflect.Int8, reflect.Uint8:
- return mangle_8bit, nil
-
- case reflect.Int16, reflect.Uint16:
- return mangle_16bit, nil
-
- case reflect.Int32, reflect.Uint32:
- return mangle_32bit, nil
-
- case reflect.Int64, reflect.Uint64:
- return mangle_64bit, nil
-
- case reflect.Float32:
- return mangle_32bit, nil
-
- case reflect.Float64:
- return mangle_64bit, nil
-
- case reflect.Complex64:
- return mangle_64bit, nil
-
- case reflect.Complex128:
- return mangle_128bit, nil
-
- default:
- return nil, nil
- }
-}
-
-// loadReflectPtr loads a Mangler (or rMangler) function for a ptr's element type.
-// This also handles further dereferencing of any further ptr indrections (e.g. ***int).
-func loadReflectPtr(et reflect.Type) (Mangler, rMangler) {
- count := 1
-
- // Iteratively dereference ptrs
- for et.Kind() == reflect.Pointer {
- et = et.Elem()
- count++
- }
-
- if et.Kind() == reflect.Array {
- // Array elem type
- at := et.Elem()
-
- // Special case of addressable (sliceable) array
- if mng := loadReflectKnownSlice(at); mng != nil {
- rmng := array_to_slice_mangler(mng)
- return nil, deref_ptr_rmangler(rmng, count)
- }
-
- // Look for an array mangler function, this will
- // access elements by index using reflect.Value and
- // pass each one to a separate mangler function.
- if rmng := loadReflectArray(at); rmng != nil {
- return nil, deref_ptr_rmangler(rmng, count)
- }
-
- return nil, nil
- }
-
- // Try remove a layer of derefs by loading a mangler
- // for a known ptr kind. The less reflection the better!
- if mng := loadReflectKnownPtr(et); mng != nil {
- if count == 1 {
- return mng, nil
- }
- return nil, deref_ptr_mangler(mng, count-1)
- }
-
- // Search for ptr elemn type mangler
- if mng, rmng := load(nil, et); mng != nil {
- return nil, deref_ptr_mangler(mng, count)
- } else if rmng != nil {
- return nil, deref_ptr_rmangler(rmng, count)
- }
-
- return nil, nil
-}
-
-// loadReflectKnownPtr loads a Mangler function for a known ptr-of-element type (in this case, primtive ptrs).
-func loadReflectKnownPtr(et reflect.Type) Mangler {
- switch et.Kind() {
- case reflect.String:
- return mangle_string_ptr
-
- case reflect.Bool:
- return mangle_bool_ptr
-
- case reflect.Int,
- reflect.Uint,
- reflect.Uintptr:
- return mangle_platform_int_ptr
-
- case reflect.Int8, reflect.Uint8:
- return mangle_8bit_ptr
-
- case reflect.Int16, reflect.Uint16:
- return mangle_16bit_ptr
-
- case reflect.Int32, reflect.Uint32:
- return mangle_32bit_ptr
-
- case reflect.Int64, reflect.Uint64:
- return mangle_64bit_ptr
-
- case reflect.Float32:
- return mangle_32bit_ptr
-
- case reflect.Float64:
- return mangle_64bit_ptr
-
- case reflect.Complex64:
- return mangle_64bit_ptr
-
- case reflect.Complex128:
- return mangle_128bit_ptr
-
- default:
- return nil
- }
-}
-
-// loadReflectKnownSlice loads a Mangler function for a known slice-of-element type (in this case, primtives).
-func loadReflectKnownSlice(et reflect.Type) Mangler {
- switch et.Kind() {
- case reflect.String:
- return mangle_string_slice
-
- case reflect.Bool:
- return mangle_bool_slice
-
- case reflect.Int,
- reflect.Uint,
- reflect.Uintptr:
- return mangle_platform_int_slice
-
- case reflect.Int8, reflect.Uint8:
- return mangle_8bit_slice
-
- case reflect.Int16, reflect.Uint16:
- return mangle_16bit_slice
-
- case reflect.Int32, reflect.Uint32:
- return mangle_32bit_slice
-
- case reflect.Int64, reflect.Uint64:
- return mangle_64bit_slice
-
- case reflect.Float32:
- return mangle_32bit_slice
-
- case reflect.Float64:
- return mangle_64bit_slice
-
- case reflect.Complex64:
- return mangle_64bit_slice
-
- case reflect.Complex128:
- return mangle_128bit_slice
-
- default:
- return nil
- }
-}
-
-// loadReflectArray loads an rMangler function for an array (or slice) or given element type.
-func loadReflectArray(et reflect.Type) rMangler {
- // Search via reflected array element type
- if mng, rmng := load(nil, et); mng != nil {
- return iter_array_mangler(mng)
- } else if rmng != nil {
- return iter_array_rmangler(rmng)
- }
- return nil
-}
-
-// loadReflectMap loads an rMangler function for a map of given key and value types.
-func loadReflectMap(kt, vt reflect.Type) rMangler {
- var kmng, vmng rMangler
-
- // Search for mangler for key type
- mng, rmng := load(nil, kt)
-
- switch {
- // Wrap key mangler to reflect
- case mng != nil:
- mng := mng // take our own ptr
- kmng = func(buf []byte, v reflect.Value) []byte {
- return mng(buf, v.Interface())
- }
-
- // Use reflect key mangler as-is
- case rmng != nil:
- kmng = rmng
-
- // No mangler found
- default:
- return nil
- }
-
- // Search for mangler for value type
- mng, rmng = load(nil, vt)
-
- switch {
- // Wrap value mangler to reflect
- case mng != nil:
- mng := mng // take our own ptr
- vmng = func(buf []byte, v reflect.Value) []byte {
- return mng(buf, v.Interface())
- }
-
- // Use reflect value mangler as-is
- case rmng != nil:
- vmng = rmng
-
- // No mangler found
- default:
- return nil
- }
-
- // Wrap key/value manglers in map iter
- return iter_map_rmangler(kmng, vmng)
-}
diff --git a/vendor/codeberg.org/gruf/go-mangler/mangle.go b/vendor/codeberg.org/gruf/go-mangler/mangle.go
deleted file mode 100644
index e12748e67..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/mangle.go
+++ /dev/null
@@ -1,154 +0,0 @@
-package mangler
-
-import (
- "reflect"
- "sync"
- "unsafe"
-)
-
-// manglers is a map of runtime 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
-
-// rMangler is functionally the same as a Mangler function, but it
-// takes the value input in reflected form. By specifying these differences
-// in mangler function types, it allows us to cut back on new calls to
-// `reflect.ValueOf()` and instead pass by existing reflected values.
-type rMangler func(buf []byte, value reflect.Value) []byte
-
-// Get will fetch the Mangler function for given runtime type.
-// 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))
-
- // 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
-// registering performant manglers for existing types not implementing Mangled (e.g. std library types).
-// NOTE: panics if there already exists a Mangler function for given type. Register on init().
-func Register(t reflect.Type, m Mangler) {
- if t == nil {
- // Nil interface{} types cannot be searched by, do not accept
- panic("cannot register mangler for nil interface{} type")
- }
-
- // Get raw runtime type ptr
- uptr := uintptr(iface_value(t))
-
- // Ensure this is a unique encoder
- if _, ok := manglers.Load(uptr); ok {
- panic("already registered mangler for type: " + t.String())
- }
-
- // Cache this encoder func
- 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)
-
- // Get raw runtime type ptr
- uptr := uintptr(iface_value(t))
-
- // Look for a cached mangler
- v, ok := manglers.Load(uptr)
-
- if !ok {
- // Load mangler into cache
- mng = loadMangler(nil, t)
- manglers.Store(uptr, mng)
- } else {
- // cast cached value
- mng = v.(Mangler)
- }
-
- // First write the type ptr (this adds
- // a unique prefix for each runtime type).
- b = mangle_platform_int(b, uptr)
-
- // Finally, mangle value
- return mng(b, a)
-}
-
-// String will return the mangled format of input value 'a'. This
-// mangled output will be unique for all default supported input types
-// during a single runtime instance. Uniqueness cannot be guaranteed
-// between separate runtime instances (whether running concurrently, or
-// the same application running at different times).
-//
-// The exact formatting of the output data should not be relied upon,
-// only that it is unique given the above constraints. Generally though,
-// the mangled output is the binary formatted text of given input data.
-//
-// Uniqueness is guaranteed for similar input data of differing types
-// (e.g. string("hello world") vs. []byte("hello world")) by prefixing
-// mangled output with the input data's runtime type pointer.
-//
-// Default supported types include:
-// - string
-// - bool
-// - int,int8,int16,int32,int64
-// - uint,uint8,uint16,uint32,uint64,uintptr
-// - float32,float64
-// - complex64,complex128
-// - 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))
-}
diff --git a/vendor/codeberg.org/gruf/go-mangler/manglers.go b/vendor/codeberg.org/gruf/go-mangler/manglers.go
deleted file mode 100644
index b9ba81705..000000000
--- a/vendor/codeberg.org/gruf/go-mangler/manglers.go
+++ /dev/null
@@ -1,265 +0,0 @@
-package mangler
-
-import (
- "math/bits"
- _ "unsafe"
-)
-
-// Notes:
-// the use of unsafe conversion from the direct interface values to
-// the chosen types in each of the below functions allows us to convert
-// not only those types directly, but anything type-aliased to those
-// types. e.g. `time.Duration` directly as int64.
-
-func mangle_string(buf []byte, a any) []byte {
- return append(buf, *(*string)(iface_value(a))...)
-}
-
-func mangle_string_ptr(buf []byte, a any) []byte {
- if ptr := (*string)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- return append(buf, *ptr...)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_string_slice(buf []byte, a any) []byte {
- s := *(*[]string)(iface_value(a))
- for _, s := range s {
- buf = append(buf, s...)
- buf = append(buf, ',')
- }
- if len(s) > 0 {
- buf = buf[:len(buf)-1]
- }
- return buf
-}
-
-func mangle_bool(buf []byte, a any) []byte {
- if *(*bool)(iface_value(a)) {
- return append(buf, '1')
- }
- return append(buf, '0')
-}
-
-func mangle_bool_ptr(buf []byte, a any) []byte {
- if ptr := (*bool)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- if *ptr {
- return append(buf, '1')
- }
- return append(buf, '0')
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_bool_slice(buf []byte, a any) []byte {
- for _, b := range *(*[]bool)(iface_value(a)) {
- if b {
- buf = append(buf, '1')
- } else {
- buf = append(buf, '0')
- }
- }
- return buf
-}
-
-func mangle_8bit(buf []byte, a any) []byte {
- return append(buf, *(*uint8)(iface_value(a)))
-}
-
-func mangle_8bit_ptr(buf []byte, a any) []byte {
- if ptr := (*uint8)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- return append(buf, *ptr)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_8bit_slice(buf []byte, a any) []byte {
- return append(buf, *(*[]uint8)(iface_value(a))...)
-}
-
-func mangle_16bit(buf []byte, a any) []byte {
- return append_uint16(buf, *(*uint16)(iface_value(a)))
-}
-
-func mangle_16bit_ptr(buf []byte, a any) []byte {
- if ptr := (*uint16)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- return append_uint16(buf, *ptr)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_16bit_slice(buf []byte, a any) []byte {
- for _, u := range *(*[]uint16)(iface_value(a)) {
- buf = append_uint16(buf, u)
- }
- return buf
-}
-
-func mangle_32bit(buf []byte, a any) []byte {
- return append_uint32(buf, *(*uint32)(iface_value(a)))
-}
-
-func mangle_32bit_ptr(buf []byte, a any) []byte {
- if ptr := (*uint32)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- return append_uint32(buf, *ptr)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_32bit_slice(buf []byte, a any) []byte {
- for _, u := range *(*[]uint32)(iface_value(a)) {
- buf = append_uint32(buf, u)
- }
- return buf
-}
-
-func mangle_64bit(buf []byte, a any) []byte {
- return append_uint64(buf, *(*uint64)(iface_value(a)))
-}
-
-func mangle_64bit_ptr(buf []byte, a any) []byte {
- if ptr := (*uint64)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- return append_uint64(buf, *ptr)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_64bit_slice(buf []byte, a any) []byte {
- for _, u := range *(*[]uint64)(iface_value(a)) {
- buf = append_uint64(buf, u)
- }
- return buf
-}
-
-// mangle_platform_int contains the correct iface mangler on runtime for platform int size.
-var mangle_platform_int = func() Mangler {
- switch bits.UintSize {
- case 32:
- return mangle_32bit
- case 64:
- return mangle_64bit
- default:
- panic("unexpected platform int size")
- }
-}()
-
-// mangle_platform_int_ptr contains the correct iface mangler on runtime for platform int size.
-var mangle_platform_int_ptr = func() Mangler {
- switch bits.UintSize {
- case 32:
- return mangle_32bit_ptr
- case 64:
- return mangle_64bit_ptr
- default:
- panic("unexpected platform int size")
- }
-}()
-
-// mangle_platform_int_slice contains the correct iface mangler on runtime for platform int size.
-var mangle_platform_int_slice = func() Mangler {
- switch bits.UintSize {
- case 32:
- return mangle_32bit_slice
- case 64:
- return mangle_64bit_slice
- default:
- panic("unexpected platform int size")
- }
-}()
-
-// uint128 provides an easily mangleable data type for 128bit data types to be cast into.
-type uint128 [2]uint64
-
-func mangle_128bit(buf []byte, a any) []byte {
- u2 := *(*uint128)(iface_value(a))
- buf = append_uint64(buf, u2[0])
- buf = append_uint64(buf, u2[1])
- return buf
-}
-
-func mangle_128bit_ptr(buf []byte, a any) []byte {
- if ptr := (*uint128)(iface_value(a)); ptr != nil {
- buf = append(buf, '1')
- buf = append_uint64(buf, (*ptr)[0])
- buf = append_uint64(buf, (*ptr)[1])
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_128bit_slice(buf []byte, a any) []byte {
- for _, u2 := range *(*[]uint128)(iface_value(a)) {
- buf = append_uint64(buf, u2[0])
- buf = append_uint64(buf, u2[1])
- }
- return buf
-}
-
-func mangle_mangled(buf []byte, a any) []byte {
- if v := a.(Mangled); v != nil {
- buf = append(buf, '1')
- return v.Mangle(buf)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_binary(buf []byte, a any) []byte {
- if v := a.(binarymarshaler); v != nil {
- b, err := v.MarshalBinary()
- if err != nil {
- panic("mangle_binary: " + err.Error())
- }
- buf = append(buf, '1')
- return append(buf, b...)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_stringer(buf []byte, a any) []byte {
- if v := a.(stringer); v != nil {
- buf = append(buf, '1')
- return append(buf, v.String()...)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_text(buf []byte, a any) []byte {
- if v := a.(textmarshaler); v != nil {
- b, err := v.MarshalText()
- if err != nil {
- panic("mangle_text: " + err.Error())
- }
- buf = append(buf, '1')
- return append(buf, b...)
- }
- buf = append(buf, '0')
- return buf
-}
-
-func mangle_json(buf []byte, a any) []byte {
- if v := a.(jsonmarshaler); v != nil {
- b, err := v.MarshalJSON()
- if err != nil {
- panic("mangle_json: " + err.Error())
- }
- buf = append(buf, '1')
- return append(buf, b...)
- }
- buf = append(buf, '0')
- return buf
-}
diff --git a/vendor/codeberg.org/gruf/go-structr/README.md b/vendor/codeberg.org/gruf/go-structr/README.md
index e2a9bdc15..125b20090 100644
--- a/vendor/codeberg.org/gruf/go-structr/README.md
+++ b/vendor/codeberg.org/gruf/go-structr/README.md
@@ -2,4 +2,74 @@
A performant struct caching library with automated indexing by arbitrary combinations of fields, including support for negative results (errors!). An example use case is in database lookups.
+Some example code of how you can use `go-structr` in your application:
+```golang
+type Cached struct {
+ Username string
+ Domain string
+ URL string
+ CountryCode int
+}
+
+var c structr.Cache[*Cached]
+
+c.Init(structr.Config[*Cached]{
+
+ // Fields this cached struct type
+ // will be indexed and stored under.
+ Indices: []structr.IndexConfig{
+ {Fields: "Username,Domain", AllowZero: true},
+ {Fields: "URL"},
+ {Fields: "CountryCode", Multiple: true},
+ },
+
+ // Maximum LRU cache size before
+ // new entries cause evictions.
+ MaxSize: 1000,
+
+ // User provided value copy function to
+ // reduce need for reflection + ensure
+ // concurrency safety for returned values.
+ CopyValue: func(c *Cached) *Cached {
+ c2 := new(Cached)
+ *c2 = *c
+ return c2
+ },
+
+ // User defined invalidation hook.
+ Invalidate: func(c *Cached) {
+ log.Println("invalidated:", c)
+ },
+})
+
+var url string
+
+// Load value from cache, with callback function to hydrate
+// cache if value cannot be found under index name with key.
+// Negative (error) results are also cached, with user definable
+// errors to ignore from caching (e.g. context deadline errs).
+value, err := c.LoadOne("URL", func() (*Cached, error) {
+ return dbType.SelectByURL(url)
+}, url)
+if err != nil {
+ return nil, err
+}
+
+// Store value in cache, only if provided callback
+// function returns without error. Passes value through
+// invalidation hook regardless of error return value.
+//
+// On success value will be automatically added to and
+// accessible under all initially configured indices.
+if err := c.Store(value, func() error {
+ return dbType.Insert(value)
+}); err != nil {
+ return nil, err
+}
+
+// Invalidate all cached results stored under
+// provided index name with give field value(s).
+c.Invalidate("CountryCode", 42)
+```
+
This is a core underpinning of [GoToSocial](https://github.com/superseriousbusiness/gotosocial)'s performance. \ No newline at end of file
diff --git a/vendor/codeberg.org/gruf/go-structr/cache.go b/vendor/codeberg.org/gruf/go-structr/cache.go
index b958fdfdb..fb52f0d8d 100644
--- a/vendor/codeberg.org/gruf/go-structr/cache.go
+++ b/vendor/codeberg.org/gruf/go-structr/cache.go
@@ -111,8 +111,8 @@ func (c *Cache[T]) Init(config Config[T]) {
// provided config.
c.mutex.Lock()
c.indices = make([]Index[T], len(config.Indices))
- for i, config := range config.Indices {
- c.indices[i].init(config)
+ for i, cfg := range config.Indices {
+ c.indices[i].init(cfg, config.MaxSize)
}
c.ignore = config.IgnoreErr
c.copy = config.CopyValue
@@ -138,7 +138,7 @@ func (c *Cache[T]) GetOne(index string, keyParts ...any) (T, bool) {
idx := c.Index(index)
// Generate index key from provided parts.
- key, ok := idx.keygen.FromParts(keyParts...)
+ key, ok := idx.hasher.FromParts(keyParts...)
if !ok {
var zero T
return zero, false
@@ -149,7 +149,7 @@ func (c *Cache[T]) GetOne(index string, keyParts ...any) (T, bool) {
}
// GetOneBy fetches value from cache stored under index, using precalculated index key.
-func (c *Cache[T]) GetOneBy(index *Index[T], key string) (T, bool) {
+func (c *Cache[T]) GetOneBy(index *Index[T], key uint64) (T, bool) {
if index == nil {
panic("no index given")
} else if !index.unique {
@@ -170,37 +170,33 @@ func (c *Cache[T]) Get(index string, keysParts ...[]any) []T {
idx := c.Index(index)
// Preallocate expected keys slice length.
- keys := make([]string, 0, len(keysParts))
+ keys := make([]uint64, 0, len(keysParts))
- // Acquire buf.
- buf := getBuf()
+ // Acquire hasher.
+ h := getHasher()
for _, parts := range keysParts {
- // Reset buf.
- buf.Reset()
+ h.Reset()
// Generate key from provided parts into buffer.
- if !idx.keygen.AppendFromParts(buf, parts...) {
+ key, ok := idx.hasher.fromParts(h, parts...)
+ if !ok {
continue
}
- // Get string copy of
- // genarated idx key.
- key := string(buf.B)
-
- // Append key to keys.
+ // Append hash sum to keys.
keys = append(keys, key)
}
- // Done with buf.
- putBuf(buf)
+ // Done with h.
+ putHasher(h)
// Continue fetching values.
return c.GetBy(idx, keys...)
}
// GetBy fetches values from the cache stored under index, using precalculated index keys.
-func (c *Cache[T]) GetBy(index *Index[T], keys ...string) []T {
+func (c *Cache[T]) GetBy(index *Index[T], keys ...uint64) []T {
if index == nil {
panic("no index given")
}
@@ -265,7 +261,7 @@ func (c *Cache[T]) Put(values ...T) {
// Store all the passed values.
for _, value := range values {
- c.store(nil, "", value, nil)
+ c.store(nil, 0, value, nil)
}
// Done with lock.
@@ -288,7 +284,7 @@ func (c *Cache[T]) LoadOne(index string, load func() (T, error), keyParts ...any
idx := c.Index(index)
// Generate cache from from provided parts.
- key, _ := idx.keygen.FromParts(keyParts...)
+ key, _ := idx.hasher.FromParts(keyParts...)
// Continue loading this result.
return c.LoadOneBy(idx, load, key)
@@ -296,7 +292,7 @@ func (c *Cache[T]) LoadOne(index string, load func() (T, error), keyParts ...any
// LoadOneBy fetches one result from the cache stored under index, using precalculated index key.
// In the case that no result is found, provided load callback will be used to hydrate the cache.
-func (c *Cache[T]) LoadOneBy(index *Index[T], load func() (T, error), key string) (T, error) {
+func (c *Cache[T]) LoadOneBy(index *Index[T], load func() (T, error), key uint64) (T, error) {
if index == nil {
panic("no index given")
} else if !index.unique {
@@ -421,26 +417,21 @@ func (c *Cache[T]) LoadBy(index *Index[T], get func(load func(keyParts ...any) b
}
}()
- // Acquire buf.
- buf := getBuf()
+ // Acquire hasher.
+ h := getHasher()
// Pass cache check to user func.
get(func(keyParts ...any) bool {
-
- // Reset buf.
- buf.Reset()
+ h.Reset()
// Generate index key from provided key parts.
- if !index.keygen.AppendFromParts(buf, keyParts...) {
+ key, ok := index.hasher.fromParts(h, keyParts...)
+ if !ok {
return false
}
- // Get temp generated key str,
- // (not needed after return).
- keyStr := buf.String()
-
// Get all indexed results.
- list := index.data[keyStr]
+ list := index.data[key]
if list != nil && list.len > 0 {
// Value length before
@@ -471,8 +462,8 @@ func (c *Cache[T]) LoadBy(index *Index[T], get func(load func(keyParts ...any) b
return false
})
- // Done with buf.
- putBuf(buf)
+ // Done with h.
+ putHasher(h)
// Done with lock.
c.mutex.Unlock()
@@ -528,7 +519,7 @@ func (c *Cache[T]) Invalidate(index string, keyParts ...any) {
idx := c.Index(index)
// Generate cache from from provided parts.
- key, ok := idx.keygen.FromParts(keyParts...)
+ key, ok := idx.hasher.FromParts(keyParts...)
if !ok {
return
}
@@ -538,7 +529,7 @@ func (c *Cache[T]) Invalidate(index string, keyParts ...any) {
}
// InvalidateBy invalidates all results stored under index key.
-func (c *Cache[T]) InvalidateBy(index *Index[T], key string) {
+func (c *Cache[T]) InvalidateBy(index *Index[T], key uint64) {
if index == nil {
panic("no index given")
}
@@ -639,7 +630,7 @@ func (c *Cache[T]) Cap() int {
// store will store the given value / error result in the cache, storing it under the
// already provided index + key if provided, else generating keys from provided value.
-func (c *Cache[T]) store(index *Index[T], key string, value T, err error) {
+func (c *Cache[T]) store(index *Index[T], key uint64, value T, err error) {
// Acquire new result.
res := result_acquire(c)
@@ -671,8 +662,8 @@ func (c *Cache[T]) store(index *Index[T], key string, value T, err error) {
// value, used during cache key gen.
rvalue := reflect.ValueOf(value)
- // Acquire buf.
- buf := getBuf()
+ // Acquire hasher.
+ h := getHasher()
for i := range c.indices {
// Get current index ptr.
@@ -684,22 +675,20 @@ func (c *Cache[T]) store(index *Index[T], key string, value T, err error) {
continue
}
- // Generate key from reflect value,
+ // Generate hash from reflect value,
// (this ignores zero value keys).
- buf.Reset() // reset buf first
- if !idx.keygen.appendFromRValue(buf, rvalue) {
+ h.Reset() // reset buf first
+ key, ok := idx.hasher.fromRValue(h, rvalue)
+ if !ok {
continue
}
- // Alloc key copy.
- key := string(buf.B)
-
// Append result to index at key.
index_append(c, idx, key, res)
}
- // Done with buf.
- putBuf(buf)
+ // Done with h.
+ putHasher(h)
}
if c.lruList.len > c.maxSize {
diff --git a/vendor/codeberg.org/gruf/go-structr/hash.go b/vendor/codeberg.org/gruf/go-structr/hash.go
new file mode 100644
index 000000000..84f0e62fc
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-structr/hash.go
@@ -0,0 +1,370 @@
+package structr
+
+import (
+ "reflect"
+ "unsafe"
+
+ "github.com/zeebo/xxh3"
+)
+
+func hasher(t reflect.Type) func(*xxh3.Hasher, any) bool {
+ switch t.Kind() {
+ case reflect.Int,
+ reflect.Uint,
+ reflect.Uintptr:
+ switch unsafe.Sizeof(int(0)) {
+ case 4:
+ return hash32bit
+ case 8:
+ return hash64bit
+ default:
+ panic("unexpected platform int size")
+ }
+
+ case reflect.Int8,
+ reflect.Uint8:
+ return hash8bit
+
+ case reflect.Int16,
+ reflect.Uint16:
+ return hash16bit
+
+ case reflect.Int32,
+ reflect.Uint32,
+ reflect.Float32:
+ return hash32bit
+
+ case reflect.Int64,
+ reflect.Uint64,
+ reflect.Float64,
+ reflect.Complex64:
+ return hash64bit
+
+ case reflect.String:
+ return hashstring
+
+ case reflect.Pointer:
+ switch t.Elem().Kind() {
+ case reflect.Int,
+ reflect.Uint,
+ reflect.Uintptr:
+ switch unsafe.Sizeof(int(0)) {
+ case 4:
+ return hash32bitptr
+ case 8:
+ return hash64bitptr
+ default:
+ panic("unexpected platform int size")
+ }
+
+ case reflect.Int8,
+ reflect.Uint8:
+ return hash8bitptr
+
+ case reflect.Int16,
+ reflect.Uint16:
+ return hash16bitptr
+
+ case reflect.Int32,
+ reflect.Uint32,
+ reflect.Float32:
+ return hash32bitptr
+
+ case reflect.Int64,
+ reflect.Uint64,
+ reflect.Float64,
+ reflect.Complex64:
+ return hash64bitptr
+
+ case reflect.String:
+ return hashstringptr
+ }
+
+ case reflect.Slice:
+ switch t.Elem().Kind() {
+ case reflect.Int,
+ reflect.Uint,
+ reflect.Uintptr:
+ switch unsafe.Sizeof(int(0)) {
+ case 4:
+ return hash32bitslice
+ case 8:
+ return hash64bitslice
+ default:
+ panic("unexpected platform int size")
+ }
+
+ case reflect.Int8,
+ reflect.Uint8:
+ return hash8bitslice
+
+ case reflect.Int16,
+ reflect.Uint16:
+ return hash16bitslice
+
+ case reflect.Int32,
+ reflect.Uint32,
+ reflect.Float32:
+ return hash32bitslice
+
+ case reflect.Int64,
+ reflect.Uint64,
+ reflect.Float64,
+ reflect.Complex64:
+ return hash64bitslice
+
+ case reflect.String:
+ return hashstringslice
+ }
+ }
+ switch {
+ case t.Implements(reflect.TypeOf((*interface{ MarshalBinary() ([]byte, error) })(nil)).Elem()):
+ return hashbinarymarshaler
+
+ case t.Implements(reflect.TypeOf((*interface{ Bytes() []byte })(nil)).Elem()):
+ return hashbytesmethod
+
+ case t.Implements(reflect.TypeOf((*interface{ String() string })(nil)).Elem()):
+ return hashstringmethod
+
+ case t.Implements(reflect.TypeOf((*interface{ MarshalText() ([]byte, error) })(nil)).Elem()):
+ return hashtextmarshaler
+
+ case t.Implements(reflect.TypeOf((*interface{ MarshalJSON() ([]byte, error) })(nil)).Elem()):
+ return hashjsonmarshaler
+ }
+ panic("unhashable type")
+}
+
+func hash8bit(h *xxh3.Hasher, a any) bool {
+ u := *(*uint8)(iface_value(a))
+ _, _ = h.Write([]byte{u})
+ return u == 0
+}
+
+func hash8bitptr(h *xxh3.Hasher, a any) bool {
+ u := (*uint8)(iface_value(a))
+ if u == nil {
+ _, _ = h.Write([]byte{
+ 0,
+ })
+ return true
+ } else {
+ _, _ = h.Write([]byte{
+ 1,
+ byte(*u),
+ })
+ return false
+ }
+}
+
+func hash8bitslice(h *xxh3.Hasher, a any) bool {
+ b := *(*[]byte)(iface_value(a))
+ _, _ = h.Write(b)
+ return b == nil
+}
+
+func hash16bit(h *xxh3.Hasher, a any) bool {
+ u := *(*uint16)(iface_value(a))
+ _, _ = h.Write([]byte{
+ byte(u),
+ byte(u >> 8),
+ })
+ return u == 0
+}
+
+func hash16bitptr(h *xxh3.Hasher, a any) bool {
+ u := (*uint16)(iface_value(a))
+ if u == nil {
+ _, _ = h.Write([]byte{
+ 0,
+ })
+ return true
+ } else {
+ _, _ = h.Write([]byte{
+ 1,
+ byte(*u),
+ byte(*u >> 8),
+ })
+ return false
+ }
+}
+
+func hash16bitslice(h *xxh3.Hasher, a any) bool {
+ u := *(*[]uint16)(iface_value(a))
+ for i := range u {
+ _, _ = h.Write([]byte{
+ byte(u[i]),
+ byte(u[i] >> 8),
+ })
+ }
+ return u == nil
+}
+
+func hash32bit(h *xxh3.Hasher, a any) bool {
+ u := *(*uint32)(iface_value(a))
+ _, _ = h.Write([]byte{
+ byte(u),
+ byte(u >> 8),
+ byte(u >> 16),
+ byte(u >> 24),
+ })
+ return u == 0
+}
+
+func hash32bitptr(h *xxh3.Hasher, a any) bool {
+ u := (*uint32)(iface_value(a))
+ if u == nil {
+ _, _ = h.Write([]byte{
+ 0,
+ })
+ return true
+ } else {
+ _, _ = h.Write([]byte{
+ 1,
+ byte(*u),
+ byte(*u >> 8),
+ byte(*u >> 16),
+ byte(*u >> 24),
+ })
+ return false
+ }
+}
+
+func hash32bitslice(h *xxh3.Hasher, a any) bool {
+ u := *(*[]uint32)(iface_value(a))
+ for i := range u {
+ _, _ = h.Write([]byte{
+ byte(u[i]),
+ byte(u[i] >> 8),
+ byte(u[i] >> 16),
+ byte(u[i] >> 24),
+ })
+ }
+ return u == nil
+}
+
+func hash64bit(h *xxh3.Hasher, a any) bool {
+ u := *(*uint64)(iface_value(a))
+ _, _ = h.Write([]byte{
+ byte(u),
+ byte(u >> 8),
+ byte(u >> 16),
+ byte(u >> 24),
+ byte(u >> 32),
+ byte(u >> 40),
+ byte(u >> 48),
+ byte(u >> 56),
+ })
+ return u == 0
+}
+
+func hash64bitptr(h *xxh3.Hasher, a any) bool {
+ u := (*uint64)(iface_value(a))
+ if u == nil {
+ _, _ = h.Write([]byte{
+ 0,
+ })
+ return true
+ } else {
+ _, _ = h.Write([]byte{
+ 1,
+ byte(*u),
+ byte(*u >> 8),
+ byte(*u >> 16),
+ byte(*u >> 24),
+ byte(*u >> 32),
+ byte(*u >> 40),
+ byte(*u >> 48),
+ byte(*u >> 56),
+ })
+ return false
+ }
+}
+
+func hash64bitslice(h *xxh3.Hasher, a any) bool {
+ u := *(*[]uint64)(iface_value(a))
+ for i := range u {
+ _, _ = h.Write([]byte{
+ byte(u[i]),
+ byte(u[i] >> 8),
+ byte(u[i] >> 16),
+ byte(u[i] >> 24),
+ byte(u[i] >> 32),
+ byte(u[i] >> 40),
+ byte(u[i] >> 48),
+ byte(u[i] >> 56),
+ })
+ }
+ return u == nil
+}
+
+func hashstring(h *xxh3.Hasher, a any) bool {
+ s := *(*string)(iface_value(a))
+ _, _ = h.WriteString(s)
+ return s == ""
+}
+
+func hashstringptr(h *xxh3.Hasher, a any) bool {
+ s := (*string)(iface_value(a))
+ if s == nil {
+ _, _ = h.Write([]byte{
+ 0,
+ })
+ return true
+ } else {
+ _, _ = h.Write([]byte{
+ 1,
+ })
+ _, _ = h.WriteString(*s)
+ return false
+ }
+}
+
+func hashstringslice(h *xxh3.Hasher, a any) bool {
+ s := *(*[]string)(iface_value(a))
+ for i := range s {
+ _, _ = h.WriteString(s[i])
+ }
+ return s == nil
+}
+
+func hashbinarymarshaler(h *xxh3.Hasher, a any) bool {
+ i := a.(interface{ MarshalBinary() ([]byte, error) })
+ b, _ := i.MarshalBinary()
+ _, _ = h.Write(b)
+ return b == nil
+}
+
+func hashbytesmethod(h *xxh3.Hasher, a any) bool {
+ i := a.(interface{ Bytes() []byte })
+ b := i.Bytes()
+ _, _ = h.Write(b)
+ return b == nil
+}
+
+func hashstringmethod(h *xxh3.Hasher, a any) bool {
+ i := a.(interface{ String() string })
+ s := i.String()
+ _, _ = h.WriteString(s)
+ return s == ""
+}
+
+func hashtextmarshaler(h *xxh3.Hasher, a any) bool {
+ i := a.(interface{ MarshalText() ([]byte, error) })
+ b, _ := i.MarshalText()
+ _, _ = h.Write(b)
+ return b == nil
+}
+
+func hashjsonmarshaler(h *xxh3.Hasher, a any) bool {
+ i := a.(interface{ MarshalJSON() ([]byte, error) })
+ b, _ := i.MarshalJSON()
+ _, _ = h.Write(b)
+ return b == nil
+}
+
+func iface_value(a any) unsafe.Pointer {
+ type eface struct{ _, v unsafe.Pointer }
+ return (*eface)(unsafe.Pointer(&a)).v
+}
diff --git a/vendor/codeberg.org/gruf/go-structr/hasher.go b/vendor/codeberg.org/gruf/go-structr/hasher.go
new file mode 100644
index 000000000..77b8a0991
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-structr/hasher.go
@@ -0,0 +1,176 @@
+package structr
+
+import (
+ "reflect"
+ "strings"
+
+ "github.com/zeebo/xxh3"
+)
+
+// Hasher provides hash checksumming for a configured
+// index, based on an arbitrary combination of generic
+// paramter struct type's fields. This provides hashing
+// both by input of the fields separately, or passing
+// an instance of the generic paramter struct type.
+//
+// Supported field types by the hasher include:
+// - ~int
+// - ~int8
+// - ~int16
+// - ~int32
+// - ~int64
+// - ~float32
+// - ~float64
+// - ~string
+// - slices / ptrs of the above
+type Hasher[StructType any] struct {
+
+ // fields contains our representation
+ // of struct fields contained in the
+ // creation of sums by this hasher.
+ fields []structfield
+
+ // zero specifies whether zero
+ // value fields are permitted.
+ zero bool
+}
+
+// NewHasher returns a new initialized Hasher for the receiving generic
+// parameter type, comprising of the given field strings, and whether to
+// allow zero values to be incldued within generated hash checksum values.
+func NewHasher[T any](fields []string, allowZero bool) Hasher[T] {
+ var h Hasher[T]
+
+ // Preallocate expected struct field slice.
+ h.fields = make([]structfield, len(fields))
+
+ // Get the reflected struct ptr type.
+ t := reflect.TypeOf((*T)(nil)).Elem()
+
+ for i, fieldName := range fields {
+ // Split name to account for nesting.
+ names := strings.Split(fieldName, ".")
+
+ // Look for a usable struct field from type.
+ sfield, ok := findField(t, names, allowZero)
+ if !ok {
+ panicf("failed finding field: %s", fieldName)
+ }
+
+ // Set parsed struct field.
+ h.fields[i] = sfield
+ }
+
+ // Set config flags.
+ h.zero = allowZero
+
+ return h
+}
+
+// FromParts generates hash checksum (used as index key) from individual key parts.
+func (h *Hasher[T]) FromParts(parts ...any) (sum uint64, ok bool) {
+ hh := getHasher()
+ sum, ok = h.fromParts(hh, parts...)
+ putHasher(hh)
+ return
+
+}
+
+func (h *Hasher[T]) fromParts(hh *xxh3.Hasher, parts ...any) (sum uint64, ok bool) {
+ if len(parts) != len(h.fields) {
+ // User must provide correct number of parts for key.
+ panicf("incorrect number key parts: want=%d received=%d",
+ len(parts),
+ len(h.fields),
+ )
+ }
+
+ if h.zero {
+ // Zero values are permitted,
+ // mangle all values and ignore
+ // zero value return booleans.
+ for i, part := range parts {
+
+ // Write mangled part to hasher.
+ _ = h.fields[i].hasher(hh, part)
+ }
+ } else {
+ // Zero values are NOT permitted.
+ for i, part := range parts {
+
+ // Write mangled field to hasher.
+ z := h.fields[i].hasher(hh, part)
+
+ if z {
+ // The value was zero for
+ // this type, return early.
+ return 0, false
+ }
+ }
+ }
+
+ return hh.Sum64(), true
+}
+
+// FromValue generates hash checksum (used as index key) from a value, via reflection.
+func (h *Hasher[T]) FromValue(value T) (sum uint64, ok bool) {
+ rvalue := reflect.ValueOf(value)
+ hh := getHasher()
+ sum, ok = h.fromRValue(hh, rvalue)
+ putHasher(hh)
+ return
+}
+
+func (h *Hasher[T]) fromRValue(hh *xxh3.Hasher, rvalue reflect.Value) (uint64, bool) {
+ // Follow any ptrs leading to value.
+ for rvalue.Kind() == reflect.Pointer {
+ rvalue = rvalue.Elem()
+ }
+
+ if h.zero {
+ // Zero values are permitted,
+ // mangle all values and ignore
+ // zero value return booleans.
+ for i := range h.fields {
+
+ // Get the reflect value's field at idx.
+ fv := rvalue.FieldByIndex(h.fields[i].index)
+ fi := fv.Interface()
+
+ // Write mangled field to hasher.
+ _ = h.fields[i].hasher(hh, fi)
+ }
+ } else {
+ // Zero values are NOT permitted.
+ for i := range h.fields {
+
+ // Get the reflect value's field at idx.
+ fv := rvalue.FieldByIndex(h.fields[i].index)
+ fi := fv.Interface()
+
+ // Write mangled field to hasher.
+ z := h.fields[i].hasher(hh, fi)
+
+ if z {
+ // The value was zero for
+ // this type, return early.
+ return 0, false
+ }
+ }
+ }
+
+ return hh.Sum64(), true
+}
+
+type structfield struct {
+ // index is the reflected index
+ // of this field (this takes into
+ // account struct nesting).
+ index []int
+
+ // hasher is the relevant function
+ // for hashing value of structfield
+ // into the supplied hashbuf, where
+ // return value indicates if zero.
+ hasher func(*xxh3.Hasher, any) bool
+}
diff --git a/vendor/codeberg.org/gruf/go-structr/index.go b/vendor/codeberg.org/gruf/go-structr/index.go
index bacf6142e..4999249f5 100644
--- a/vendor/codeberg.org/gruf/go-structr/index.go
+++ b/vendor/codeberg.org/gruf/go-structr/index.go
@@ -45,12 +45,12 @@ type Index[StructType any] struct {
// string value of contained fields.
name string
- // struct field key serializer.
- keygen KeyGen[StructType]
+ // struct field key hasher.
+ hasher Hasher[StructType]
// backing in-memory data store of
// generated index keys to result lists.
- data map[string]*list[*result[StructType]]
+ data map[uint64]*list[*result[StructType]]
// whether to allow
// multiple results
@@ -59,20 +59,20 @@ type Index[StructType any] struct {
}
// init initializes this index with the given configuration.
-func (i *Index[T]) init(config IndexConfig) {
+func (i *Index[T]) init(config IndexConfig, max int) {
fields := strings.Split(config.Fields, ",")
i.name = config.Fields
- i.keygen = NewKeyGen[T](fields, config.AllowZero)
+ i.hasher = NewHasher[T](fields, config.AllowZero)
i.unique = !config.Multiple
- i.data = make(map[string]*list[*result[T]])
+ i.data = make(map[uint64]*list[*result[T]], max+1)
}
-// KeyGen returns the key generator associated with this index.
-func (i *Index[T]) KeyGen() *KeyGen[T] {
- return &i.keygen
+// Hasher returns the hash checksummer associated with this index.
+func (i *Index[T]) Hasher() *Hasher[T] {
+ return &i.hasher
}
-func index_append[T any](c *Cache[T], i *Index[T], key string, res *result[T]) {
+func index_append[T any](c *Cache[T], i *Index[T], key uint64, res *result[T]) {
// Acquire + setup indexkey.
ikey := indexkey_acquire(c)
ikey.entry.Value = res
@@ -138,7 +138,7 @@ func index_deleteOne[T any](c *Cache[T], i *Index[T], ikey *indexkey[T]) {
}
}
-func index_delete[T any](c *Cache[T], i *Index[T], key string, fn func(*result[T])) {
+func index_delete[T any](c *Cache[T], i *Index[T], key uint64, fn func(*result[T])) {
if fn == nil {
panic("nil fn")
}
@@ -180,7 +180,7 @@ type indexkey[T any] struct {
// key is the generated index key
// the related result is indexed
// under, in the below index.
- key string
+ key uint64
// index is the index that the
// related result is indexed in.
@@ -205,7 +205,7 @@ func indexkey_acquire[T any](c *Cache[T]) *indexkey[T] {
func indexkey_release[T any](c *Cache[T], ikey *indexkey[T]) {
// Reset indexkey.
ikey.entry.Value = nil
- ikey.key = ""
+ ikey.key = 0
ikey.index = nil
// Release indexkey to memory pool.
diff --git a/vendor/codeberg.org/gruf/go-structr/key.go b/vendor/codeberg.org/gruf/go-structr/key.go
deleted file mode 100644
index 557a5f033..000000000
--- a/vendor/codeberg.org/gruf/go-structr/key.go
+++ /dev/null
@@ -1,204 +0,0 @@
-package structr
-
-import (
- "reflect"
- "strings"
-
- "codeberg.org/gruf/go-byteutil"
- "codeberg.org/gruf/go-mangler"
-)
-
-// KeyGen is the underlying index key generator
-// used within Index, and therefore Cache itself.
-type KeyGen[StructType any] struct {
-
- // fields contains our representation of
- // the struct fields contained in the
- // creation of keys by this generator.
- fields []structfield
-
- // zero specifies whether zero
- // value fields are permitted.
- zero bool
-}
-
-// NewKeyGen returns a new initialized KeyGen for the receiving generic
-// parameter type, comprising of the given field strings, and whether to
-// allow zero values to be included within generated output strings.
-func NewKeyGen[T any](fields []string, allowZero bool) KeyGen[T] {
- var kgen KeyGen[T]
-
- // Preallocate expected struct field slice.
- kgen.fields = make([]structfield, len(fields))
-
- // Get the reflected struct ptr type.
- t := reflect.TypeOf((*T)(nil)).Elem()
-
- for i, fieldName := range fields {
- // Split name to account for nesting.
- names := strings.Split(fieldName, ".")
-
- // Look for a usable struct field from type.
- sfield, ok := findField(t, names, allowZero)
- if !ok {
- panicf("failed finding field: %s", fieldName)
- }
-
- // Set parsed struct field.
- kgen.fields[i] = sfield
- }
-
- // Set config flags.
- kgen.zero = allowZero
-
- return kgen
-}
-
-// FromParts generates key string from individual key parts.
-func (kgen *KeyGen[T]) FromParts(parts ...any) (key string, ok bool) {
- buf := getBuf()
- if ok = kgen.AppendFromParts(buf, parts...); ok {
- key = string(buf.B)
- }
- putBuf(buf)
- return
-}
-
-// FromValue generates key string from a value, via reflection.
-func (kgen *KeyGen[T]) FromValue(value T) (key string, ok bool) {
- buf := getBuf()
- rvalue := reflect.ValueOf(value)
- if ok = kgen.appendFromRValue(buf, rvalue); ok {
- key = string(buf.B)
- }
- putBuf(buf)
- return
-}
-
-// AppendFromParts generates key string into provided buffer, from individual key parts.
-func (kgen *KeyGen[T]) AppendFromParts(buf *byteutil.Buffer, parts ...any) bool {
- if len(parts) != len(kgen.fields) {
- // User must provide correct number of parts for key.
- panicf("incorrect number key parts: want=%d received=%d",
- len(parts),
- len(kgen.fields),
- )
- }
-
- if kgen.zero {
- // Zero values are permitted,
- // mangle all values and ignore
- // zero value return booleans.
- for i, part := range parts {
-
- // Mangle this value into buffer.
- _ = kgen.fields[i].Mangle(buf, part)
-
- // Append part separator.
- buf.B = append(buf.B, '.')
- }
- } else {
- // Zero values are NOT permitted.
- for i, part := range parts {
-
- // Mangle this value into buffer.
- z := kgen.fields[i].Mangle(buf, part)
-
- if z {
- // The value was zero for
- // this type, return early.
- return false
- }
-
- // Append part separator.
- buf.B = append(buf.B, '.')
- }
- }
-
- // Drop the last separator.
- buf.B = buf.B[:len(buf.B)-1]
-
- return true
-}
-
-// AppendFromValue generates key string into provided buffer, from a value via reflection.
-func (kgen *KeyGen[T]) AppendFromValue(buf *byteutil.Buffer, value T) bool {
- return kgen.appendFromRValue(buf, reflect.ValueOf(value))
-}
-
-// appendFromRValue is the underlying generator function for the exported ___FromValue() functions,
-// accepting a reflected input. We do not expose this as the reflected value is EXPECTED to be right.
-func (kgen *KeyGen[T]) appendFromRValue(buf *byteutil.Buffer, rvalue reflect.Value) bool {
- // Follow any ptrs leading to value.
- for rvalue.Kind() == reflect.Pointer {
- rvalue = rvalue.Elem()
- }
-
- if kgen.zero {
- // Zero values are permitted,
- // mangle all values and ignore
- // zero value return booleans.
- for i := range kgen.fields {
-
- // Get the reflect value's field at idx.
- fv := rvalue.FieldByIndex(kgen.fields[i].index)
- fi := fv.Interface()
-
- // Mangle this value into buffer.
- _ = kgen.fields[i].Mangle(buf, fi)
-
- // Append part separator.
- buf.B = append(buf.B, '.')
- }
- } else {
- // Zero values are NOT permitted.
- for i := range kgen.fields {
-
- // Get the reflect value's field at idx.
- fv := rvalue.FieldByIndex(kgen.fields[i].index)
- fi := fv.Interface()
-
- // Mangle this value into buffer.
- z := kgen.fields[i].Mangle(buf, fi)
-
- if z {
- // The value was zero for
- // this type, return early.
- return false
- }
-
- // Append part separator.
- buf.B = append(buf.B, '.')
- }
- }
-
- // Drop the last separator.
- buf.B = buf.B[:len(buf.B)-1]
-
- return true
-}
-
-type structfield struct {
- // index is the reflected index
- // of this field (this takes into
- // account struct nesting).
- index []int
-
- // zero is the possible mangled
- // zero value for this field.
- zero string
-
- // mangler is the mangler function for
- // serializing values of this field.
- mangler mangler.Mangler
-}
-
-// Mangle mangles the given value, using the determined type-appropriate
-// field's type. The returned boolean indicates whether this is a zero value.
-func (f *structfield) Mangle(buf *byteutil.Buffer, value any) (isZero bool) {
- s := len(buf.B) // start pos.
- buf.B = f.mangler(buf.B, value)
- e := len(buf.B) // end pos.
- isZero = (f.zero == string(buf.B[s:e]))
- return
-}
diff --git a/vendor/codeberg.org/gruf/go-structr/util.go b/vendor/codeberg.org/gruf/go-structr/util.go
index d8f227baf..01ac06cf1 100644
--- a/vendor/codeberg.org/gruf/go-structr/util.go
+++ b/vendor/codeberg.org/gruf/go-structr/util.go
@@ -7,8 +7,7 @@ import (
"unicode"
"unicode/utf8"
- "codeberg.org/gruf/go-byteutil"
- "codeberg.org/gruf/go-mangler"
+ "github.com/zeebo/xxh3"
)
// findField will search for a struct field with given set of names, where names is a len > 0 slice of names account for nesting.
@@ -68,22 +67,8 @@ func findField(t reflect.Type, names []string, allowZero bool) (sfield structfie
t = field.Type
}
- // Get final type mangler func.
- sfield.mangler = mangler.Get(t)
-
- if allowZero {
- var buf []byte
-
- // Allocate field instance.
- v := reflect.New(field.Type)
- v = v.Elem()
-
- // Serialize this zero value into buf.
- buf = sfield.mangler(buf, v.Interface())
-
- // Set zero value str.
- sfield.zero = string(buf)
- }
+ // Get final type hash func.
+ sfield.hasher = hasher(t)
return
}
@@ -93,26 +78,21 @@ func panicf(format string, args ...any) {
panic(fmt.Sprintf(format, args...))
}
-// bufpool provides a memory pool of byte
-// buffers used when encoding key types.
-var bufPool sync.Pool
+// hashPool provides a memory pool of xxh3
+// hasher objects used indexing field vals.
+var hashPool sync.Pool
-// getBuf fetches buffer from memory pool.
-func getBuf() *byteutil.Buffer {
- v := bufPool.Get()
+// gethashbuf fetches hasher from memory pool.
+func getHasher() *xxh3.Hasher {
+ v := hashPool.Get()
if v == nil {
- buf := new(byteutil.Buffer)
- buf.B = make([]byte, 0, 512)
- v = buf
+ v = new(xxh3.Hasher)
}
- return v.(*byteutil.Buffer)
+ return v.(*xxh3.Hasher)
}
-// putBuf replaces buffer in memory pool.
-func putBuf(buf *byteutil.Buffer) {
- if buf.Cap() > int(^uint16(0)) {
- return // drop large bufs
- }
- buf.Reset()
- bufPool.Put(buf)
+// putHasher replaces hasher in memory pool.
+func putHasher(h *xxh3.Hasher) {
+ h.Reset()
+ hashPool.Put(h)
}