From e3dfd8889315af38c4eef1eb4247dc07a51899c7 Mon Sep 17 00:00:00 2001 From: kim Date: Tue, 29 Jul 2025 09:23:20 +0200 Subject: [performance] bump codeberg.org/gruf/go-kv to v2 (#4341) updates our codeberg.org/gruf/go-kv log key-value formatting library to latest version, which comes with some maaaaaaajor speed boosts in the form of: - very minimal reflect.Value{} usage - caching prepared formatting functions per type ~~still a work-in-progress until i make a release tag on the go-kv repository, which itself is waiting on published benchmark results in the README and finishing writing some code comments~~ benchmarks so far show this to be ~3x faster than the "fmt" stdlib package on average, when run across a wide variety (106 different types) of test cases, while still creating more visually friendly log output and actually recursing down nested struct ptrs Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4341 Co-authored-by: kim Co-committed-by: kim --- .../codeberg.org/gruf/go-kv/v2/format/methods.go | 153 +++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 vendor/codeberg.org/gruf/go-kv/v2/format/methods.go (limited to 'vendor/codeberg.org/gruf/go-kv/v2/format/methods.go') diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go b/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go new file mode 100644 index 000000000..7c1795771 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-kv/v2/format/methods.go @@ -0,0 +1,153 @@ +package format + +import ( + "reflect" + "unsafe" +) + +type Stringer interface{ String() string } + +var ( + // stringer type for implement checks. + stringerType = typeof[Stringer]() + + // error type for implement checks. + errorType = typeof[error]() +) + +// getMethodType returns a *possible* FormatFunc to handle case +// of a type that implements any known interface{} types, else nil. +func getMethodType(t typenode) FormatFunc { + switch { + case t.rtype.Implements(stringerType): + switch t.rtype.Kind() { + case reflect.Interface: + return getInterfaceStringerType(t) + default: + return getConcreteStringerType(t) + } + case t.rtype.Implements(errorType): + switch t.rtype.Kind() { + case reflect.Interface: + return getInterfaceErrorType(t) + default: + return getConcreteErrorType(t) + } + default: + return nil + } +} + +// getInterfaceStringerType returns a FormatFunc to handle case of an interface{} +// type that implements Stringer{}, i.e. Stringer{} itself and any superset of. +func getInterfaceStringerType(t typenode) FormatFunc { + switch t.indirect() && !t.iface_indir() { + case true: + return with_typestr_ptrs(t, func(s *State) { + s.P = *(*unsafe.Pointer)(s.P) + if s.P == nil || (*reflect_nonEmptyInterface)(s.P).word == nil { + appendNil(s) + return + } + v := *(*Stringer)(s.P) + appendString(s, v.String()) + }) + case false: + return with_typestr_ptrs(t, func(s *State) { + if s.P == nil || (*reflect_nonEmptyInterface)(s.P).word == nil { + appendNil(s) + return + } + v := *(*Stringer)(s.P) + appendString(s, v.String()) + }) + default: + panic("unreachable") + } +} + +// getConcreteStringerType returns a FormatFunc to handle case of concrete +// (i.e. non-interface{}) type that has a Stringer{} method receiver. +func getConcreteStringerType(t typenode) FormatFunc { + itab := get_iface_ITab[Stringer](t.rtype) + switch t.indirect() && !t.iface_indir() { + case true: + return with_typestr_ptrs(t, func(s *State) { + s.P = *(*unsafe.Pointer)(s.P) + if s.P == nil { + appendNil(s) + return + } + v := *(*Stringer)(pack_iface(itab, s.P)) + appendString(s, v.String()) + }) + case false: + return with_typestr_ptrs(t, func(s *State) { + if s.P == nil { + appendNil(s) + return + } + v := *(*Stringer)(pack_iface(itab, s.P)) + appendString(s, v.String()) + }) + default: + panic("unreachable") + } +} + +// getInterfaceErrorType returns a FormatFunc to handle case of an interface{} +// type that implements error{}, i.e. error{} itself and any superset of. +func getInterfaceErrorType(t typenode) FormatFunc { + switch t.indirect() && !t.iface_indir() { + case true: + return with_typestr_ptrs(t, func(s *State) { + s.P = *(*unsafe.Pointer)(s.P) + if s.P == nil || (*reflect_nonEmptyInterface)(s.P).word == nil { + appendNil(s) + return + } + v := *(*error)(s.P) + appendString(s, v.Error()) + }) + case false: + return with_typestr_ptrs(t, func(s *State) { + if s.P == nil || (*reflect_nonEmptyInterface)(s.P).word == nil { + appendNil(s) + return + } + v := *(*error)(s.P) + appendString(s, v.Error()) + }) + default: + panic("unreachable") + } +} + +// getConcreteErrorType returns a FormatFunc to handle case of concrete +// (i.e. non-interface{}) type that has an error{} method receiver. +func getConcreteErrorType(t typenode) FormatFunc { + itab := get_iface_ITab[error](t.rtype) + switch t.indirect() && !t.iface_indir() { + case true: + return with_typestr_ptrs(t, func(s *State) { + s.P = *(*unsafe.Pointer)(s.P) + if s.P == nil { + appendNil(s) + return + } + v := *(*error)(pack_iface(itab, s.P)) + appendString(s, v.Error()) + }) + case false: + return with_typestr_ptrs(t, func(s *State) { + if s.P == nil { + appendNil(s) + return + } + v := *(*error)(pack_iface(itab, s.P)) + appendString(s, v.Error()) + }) + default: + panic("unreachable") + } +} -- cgit v1.2.3