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/util.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/util.go')
| -rw-r--r-- | vendor/codeberg.org/gruf/go-kv/v2/util.go | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-kv/v2/util.go b/vendor/codeberg.org/gruf/go-kv/v2/util.go new file mode 100644 index 000000000..90fba59de --- /dev/null +++ b/vendor/codeberg.org/gruf/go-kv/v2/util.go @@ -0,0 +1,147 @@ +package kv + +import ( + "strconv" + "strings" + + "codeberg.org/gruf/go-byteutil" + "codeberg.org/gruf/go-kv/v2/format" +) + +// AppendQuoteString will append (and escape/quote where necessary) a field string. +func AppendQuoteString(buf *byteutil.Buffer, str string) { + switch { + case len(str) == 0: + // Append empty quotes. + buf.B = append(buf.B, `""`...) + return + + case len(str) == 1: + // Append escaped single byte. + buf.B = format.AppendEscapeByte(buf.B, str[0]) + return + + case len(str) > format.SingleTermLine || !format.IsSafeASCII(str): + // Long line or contains non-ascii chars. + buf.B = strconv.AppendQuote(buf.B, str) + return + + case !isQuoted(str): + // Not single/double quoted already. + + if format.ContainsSpaceOrTab(str) { + // Quote un-enclosed spaces. + buf.B = append(buf.B, '"') + buf.B = append(buf.B, str...) + buf.B = append(buf.B, '"') + return + } + + if format.ContainsDoubleQuote(str) { + // Contains double quote, double quote + // and append escaped existing. + buf.B = append(buf.B, '"') + buf.B = format.AppendEscape(buf.B, str) + buf.B = append(buf.B, '"') + return + } + } + + // Double quoted, enclosed in braces, or + // literally anything else: append as-is. + buf.B = append(buf.B, str...) + return +} + +// AppendQuoteValue will append (and escape/quote where necessary) a formatted value string. +func AppendQuoteValue(buf *byteutil.Buffer, str string) { + switch { + case len(str) == 0: + // Append empty quotes. + buf.B = append(buf.B, `""`...) + return + + case len(str) == 1: + // Append quoted single byte. + buf.B = format.AppendQuoteByte(buf.B, str[0]) + return + + case len(str) > format.SingleTermLine || !format.IsSafeASCII(str): + // Long line or contains non-ascii chars. + buf.B = strconv.AppendQuote(buf.B, str) + return + + case !isQuoted(str): + // Not single/double quoted already. + + // Get space / tab indices (if any). + s := strings.IndexByte(str, ' ') + t := strings.IndexByte(str, '\t') + + // Find first whitespace. + sp0 := smallest(s, t) + if sp0 < 0 { + break + } + + // Check if str is enclosed by braces. + // (but without any key-value separator). + if (enclosedBy(str, sp0, '{', '}') || + enclosedBy(str, sp0, '[', ']') || + enclosedBy(str, sp0, '(', ')')) && + strings.IndexByte(str, '=') < 0 { + break + } + + if format.ContainsDoubleQuote(str) { + // Contains double quote, double quote + // and append escaped existing. + buf.B = append(buf.B, '"') + buf.B = format.AppendEscape(buf.B, str) + buf.B = append(buf.B, '"') + return + } + + // Quote un-enclosed spaces. + buf.B = append(buf.B, '"') + buf.B = append(buf.B, str...) + buf.B = append(buf.B, '"') + return + } + + // Double quoted, enclosed in braces, or + // literally anything else: append as-is. + buf.B = append(buf.B, str...) + return +} + +// isQuoted checks if string is single or double quoted. +func isQuoted(str string) bool { + return (str[0] == '"' && str[len(str)-1] == '"') || + (str[0] == '\'' && str[len(str)-1] == '\'') +} + +// smallest attempts to return the smallest positive value of those given. +func smallest(i1, i2 int) int { + if i1 >= 0 && (i2 < 0 || i1 < i2) { + return i1 + } + return i2 +} + +// enclosedBy will check if given string is enclosed by end, and at least non-whitespace up to start. +func enclosedBy(str string, sp int, start, end byte) bool { + // Check for ending char in string. + if str[len(str)-1] != end { + return false + } + + // Check for starting char in string. + i := strings.IndexByte(str, start) + if i < 0 { + return false + } + + // Check before space. + return i < sp +} |
