diff options
| author | 2025-07-29 09:23:20 +0200 | |
|---|---|---|
| committer | 2025-07-29 09:23:20 +0200 | |
| commit | e3dfd8889315af38c4eef1eb4247dc07a51899c7 (patch) | |
| tree | 5841d80475be7a7336ae968057f3d4ffdd2e3627 /vendor/codeberg.org/gruf/go-kv/v2/format/slice.go | |
| parent | [chore] Only display report comment forwarding notice when reporting account ... (diff) | |
| download | gotosocial-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/slice.go')
| -rw-r--r-- | vendor/codeberg.org/gruf/go-kv/v2/format/slice.go | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-kv/v2/format/slice.go b/vendor/codeberg.org/gruf/go-kv/v2/format/slice.go new file mode 100644 index 000000000..f76e85410 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-kv/v2/format/slice.go @@ -0,0 +1,158 @@ +package format + +// iterSliceType returns a FormatFunc capable of iterating +// and formatting the given slice type currently in typenode{}. +// note this will fetch a sub-FormatFunc for the slice element +// type, and also handle special cases of []byte, []rune slices. +func (fmt *Formatter) iterSliceType(t typenode) FormatFunc { + + // Get nested element type. + elem := t.rtype.Elem() + esz := elem.Size() + + // Get nested elem typenode with flags. + flags := reflect_slice_elem_flags(elem) + et := t.next(elem, flags) + + // Get elem format func. + fn := fmt.loadOrGet(et) + if fn == nil { + panic("unreachable") + } + + if !t.needs_typestr() { + return func(s *State) { + ptr := s.P + + // Get data as unsafe slice header. + hdr := (*unsafeheader_Slice)(ptr) + if hdr == nil || hdr.Data == nil { + + // Append nil. + appendNil(s) + return + } + + // Prepend array brace. + s.B = append(s.B, '[') + + if hdr.Len > 0 { + for i := 0; i < hdr.Len; i++ { + // Format at array index. + offset := esz * uintptr(i) + s.P = add(hdr.Data, offset) + fn(s) + + // Append separator. + s.B = append(s.B, ',') + } + + // Drop final comma. + s.B = s.B[:len(s.B)-1] + } + + // Append array brace. + s.B = append(s.B, ']') + } + } + + // Slice type string with ptrs / refs. + typestrPtrs := t.typestr_with_ptrs() + typestrRefs := t.typestr_with_refs() + + return func(s *State) { + ptr := s.P + + // Get data as unsafe slice header. + hdr := (*unsafeheader_Slice)(ptr) + if hdr == nil || hdr.Data == nil { + + // Append nil value with type. + appendNilType(s, typestrPtrs) + return + } + + // Open / close braces. + var open, close uint8 + open, close = '[', ']' + + // Include type info. + if s.A.WithType() { + s.B = append(s.B, typestrRefs...) + open, close = '{', '}' + } + + // Prepend array brace. + s.B = append(s.B, open) + + if hdr.Len > 0 { + for i := 0; i < hdr.Len; i++ { + // Format at array index. + offset := esz * uintptr(i) + s.P = add(hdr.Data, offset) + fn(s) + + // Append separator. + s.B = append(s.B, ',') + } + + // Drop final comma. + s.B = s.B[:len(s.B)-1] + } + + // Append array brace. + s.B = append(s.B, close) + } +} + +func wrapByteSlice(t typenode, fn FormatFunc) FormatFunc { + if !t.needs_typestr() { + return func(s *State) { + if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { + appendString(s, *(*string)(s.P)) + } else { + fn(s) + } + } + } + typestr := t.typestr_with_ptrs() + return func(s *State) { + if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { + if s.A.WithType() { + s.B = append(s.B, "("+typestr+")("...) + appendString(s, *(*string)(s.P)) + s.B = append(s.B, ")"...) + } else { + appendString(s, *(*string)(s.P)) + } + } else { + fn(s) + } + } +} + +func wrapRuneSlice(t typenode, fn FormatFunc) FormatFunc { + if !t.needs_typestr() { + return func(s *State) { + if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { + appendString(s, string(*(*[]rune)(s.P))) + } else { + fn(s) + } + } + } + typestr := t.typestr_with_ptrs() + return func(s *State) { + if s.A.AsText() || s.A.AsQuotedText() || s.A.AsQuotedASCII() { + if s.A.WithType() { + s.B = append(s.B, "("+typestr+")("...) + appendString(s, string(*(*[]rune)(s.P))) + s.B = append(s.B, ")"...) + } else { + appendString(s, string(*(*[]rune)(s.P))) + } + } else { + fn(s) + } + } +} |
