summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go
diff options
context:
space:
mode:
authorLibravatar kim <grufwub@gmail.com>2025-07-29 09:23:20 +0200
committerLibravatar tobi <kipvandenbos@noreply.codeberg.org>2025-07-29 09:23:20 +0200
commite3dfd8889315af38c4eef1eb4247dc07a51899c7 (patch)
tree5841d80475be7a7336ae968057f3d4ffdd2e3627 /vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go
parent[chore] Only display report comment forwarding notice when reporting account ... (diff)
downloadgotosocial-e3dfd8889315af38c4eef1eb4247dc07a51899c7.tar.xz
[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 <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go')
-rw-r--r--vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go128
1 files changed, 128 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go b/vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go
new file mode 100644
index 000000000..1f860aba9
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-kv/v2/format/pointer.go
@@ -0,0 +1,128 @@
+package format
+
+import (
+ "reflect"
+ "unsafe"
+)
+
+// derefPointerType returns a FormatFunc capable of dereferencing
+// and formatting the given pointer type currently in typenode{}.
+// note this will fetch a sub-FormatFunc for resulting value type.
+func (fmt *Formatter) derefPointerType(t typenode) FormatFunc {
+ var n int
+ rtype := t.rtype
+ flags := t.flags
+
+ // Iteratively dereference pointer types.
+ for rtype.Kind() == reflect.Pointer {
+
+ // If this is actual indirect
+ // memory, increase dereferences.
+ if flags&reflect_flagIndir != 0 {
+ n++
+ }
+
+ // Get next elem type.
+ rtype = rtype.Elem()
+
+ // Get next set of dereferenced elem type flags.
+ flags = reflect_pointer_elem_flags(flags, rtype)
+ }
+
+ // Wrap value as typenode.
+ vt := t.next(rtype, flags)
+
+ // Get value format func.
+ fn := fmt.loadOrGet(vt)
+ if fn == nil {
+ panic("unreachable")
+ }
+
+ if !t.needs_typestr() {
+ if n <= 0 {
+ // No derefs are needed.
+ return func(s *State) {
+ if s.P == nil {
+ // Final check.
+ appendNil(s)
+ return
+ }
+
+ // Format
+ // final
+ // value.
+ fn(s)
+ }
+ }
+
+ return func(s *State) {
+ // Deref n number times.
+ for i := n; i > 0; i-- {
+
+ if s.P == nil {
+ // Nil check.
+ appendNil(s)
+ return
+ }
+
+ // Further deref pointer value.
+ s.P = *(*unsafe.Pointer)(s.P)
+ }
+
+ if s.P == nil {
+ // Final check.
+ appendNil(s)
+ return
+ }
+
+ // Format
+ // final
+ // value.
+ fn(s)
+ }
+ }
+
+ // Final type string with ptrs.
+ typestr := t.typestr_with_ptrs()
+
+ if n <= 0 {
+ // No derefs are needed.
+ return func(s *State) {
+ if s.P == nil {
+ // Final nil value check.
+ appendNilType(s, typestr)
+ return
+ }
+
+ // Format
+ // final
+ // value.
+ fn(s)
+ }
+ }
+
+ return func(s *State) {
+ // Deref n number times.
+ for i := n; i > 0; i-- {
+ if s.P == nil {
+ // Check for nil value.
+ appendNilType(s, typestr)
+ return
+ }
+
+ // Further deref pointer value.
+ s.P = *(*unsafe.Pointer)(s.P)
+ }
+
+ if s.P == nil {
+ // Final nil value check.
+ appendNilType(s, typestr)
+ return
+ }
+
+ // Format
+ // final
+ // value.
+ fn(s)
+ }
+}