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/field_fmt.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/field_fmt.go')
| -rw-r--r-- | vendor/codeberg.org/gruf/go-kv/v2/field_fmt.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-kv/v2/field_fmt.go b/vendor/codeberg.org/gruf/go-kv/v2/field_fmt.go new file mode 100644 index 000000000..5d6e77f4b --- /dev/null +++ b/vendor/codeberg.org/gruf/go-kv/v2/field_fmt.go @@ -0,0 +1,63 @@ +//go:build !kvformat +// +build !kvformat + +package kv + +import ( + "fmt" + "sync" + + "codeberg.org/gruf/go-byteutil" +) + +// bufPool is a memory pool of byte buffers. +var bufPool = sync.Pool{ + New: func() interface{} { + return &byteutil.Buffer{B: make([]byte, 0, 512)} + }, +} + +// AppendFormat will append formatted format of Field to 'buf'. See .String() for details. +func (f Field) AppendFormat(buf *byteutil.Buffer, vbose bool) { + var fmtstr string + if vbose /* verbose */ { + fmtstr = `%#v` + } else /* regular */ { + fmtstr = `%+v` + } + AppendQuoteString(buf, f.K) + buf.WriteByte('=') + appendValuef(buf, fmtstr, f.V) +} + +// Value returns the formatted value string of this Field. +func (f Field) Value(vbose bool) string { + var fmtstr string + if vbose /* verbose */ { + fmtstr = `%#v` + } else /* regular */ { + fmtstr = `%+v` + } + buf := byteutil.Buffer{B: make([]byte, 0, bufsize/2)} + appendValuef(&buf, fmtstr, f.V) + return buf.String() +} + +// appendValuef appends a quoted value string (formatted by fmt.Appendf) to 'buf'. +func appendValuef(buf *byteutil.Buffer, format string, args ...interface{}) { + // Write format string to a byte buffer + fmtbuf := bufPool.Get().(*byteutil.Buffer) + fmtbuf.B = fmt.Appendf(fmtbuf.B, format, args...) + + // Append quoted value to dst buffer + AppendQuoteValue(buf, fmtbuf.String()) + + // Drop overly large capacity buffers + if fmtbuf.Cap() > int(^uint16(0)) { + return + } + + // Replace buffer in pool + fmtbuf.Reset() + bufPool.Put(fmtbuf) +} |
