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.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.go')
| -rw-r--r-- | vendor/codeberg.org/gruf/go-kv/v2/field.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-kv/v2/field.go b/vendor/codeberg.org/gruf/go-kv/v2/field.go new file mode 100644 index 000000000..58399e4a9 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-kv/v2/field.go @@ -0,0 +1,103 @@ +package kv + +import ( + "codeberg.org/gruf/go-byteutil" +) + +// bufsize is the default buffer size per field to alloc +// when calling .AppendFormat() from within .String(). +const bufsize = 64 + +// Fields is a typedef for a []Field slice to provide +// slightly more performant string formatting for multiples. +type Fields []Field + +// Get will return the field with given 'key'. +func (f Fields) Get(key string) (*Field, bool) { + for i := 0; i < len(f); i++ { + if f[i].K == key { + return &f[i], true + } + } + return nil, false +} + +// Set will set an existing field with 'key' to 'value', or append new. +func (f *Fields) Set(key string, value interface{}) { + for i := 0; i < len(*f); i++ { + // Update existing value + if (*f)[i].K == key { + (*f)[i].V = value + return + } + } + + // Append new field + *f = append(*f, Field{ + K: key, + V: value, + }) +} + +// AppendFormat appends a string representation of receiving Field(s) to 'b'. +func (f Fields) AppendFormat(buf *byteutil.Buffer, vbose bool) { + for i := 0; i < len(f); i++ { + f[i].AppendFormat(buf, vbose) + buf.WriteByte(' ') + } + if len(f) > 0 { + buf.Truncate(1) + } +} + +// String returns a string representation of receiving Field(s). +func (f Fields) String() string { + b := make([]byte, 0, bufsize*len(f)) + buf := byteutil.Buffer{B: b} + f.AppendFormat(&buf, false) + return buf.String() +} + +// GoString performs .String() but with type prefix. +func (f Fields) GoString() string { + b := make([]byte, 0, bufsize*len(f)) + buf := byteutil.Buffer{B: b} + f.AppendFormat(&buf, true) + return "kv.Fields{" + buf.String() + "}" +} + +// Field represents an individual key-value field. +type Field struct { + K string // Field key + V interface{} // Field value +} + +// Key returns the formatted key string of this Field. +func (f Field) Key() string { + buf := byteutil.Buffer{B: make([]byte, 0, bufsize/2)} + AppendQuoteString(&buf, f.K) + return buf.String() +} + +// String will return a string representation of this Field +// of the form `key=value` where `value` is formatted using +// fmt package's `%+v` directive. If the .X = true (verbose), +// then it uses '%#v'. Both key and value are escaped and +// quoted if necessary to fit on single line. +// +// If the `kvformat` build tag is provided, the formatting +// will be performed by the `kv/format` package. +func (f Field) String() string { + b := make([]byte, 0, bufsize) + buf := byteutil.Buffer{B: b} + f.AppendFormat(&buf, false) + return buf.String() +} + +// GoString performs .String() but with verbose always enabled. +func (f Field) GoString() string { + b := make([]byte, 0, bufsize) + buf := byteutil.Buffer{B: b} + f.AppendFormat(&buf, true) + return buf.String() +} |
