diff options
Diffstat (limited to 'vendor/codeberg.org/gruf/go-kv/field_fmt.go')
-rw-r--r-- | vendor/codeberg.org/gruf/go-kv/field_fmt.go | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/vendor/codeberg.org/gruf/go-kv/field_fmt.go b/vendor/codeberg.org/gruf/go-kv/field_fmt.go index c62393fe0..fcdcc5e17 100644 --- a/vendor/codeberg.org/gruf/go-kv/field_fmt.go +++ b/vendor/codeberg.org/gruf/go-kv/field_fmt.go @@ -5,10 +5,18 @@ 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 @@ -17,9 +25,9 @@ func (f Field) AppendFormat(buf *byteutil.Buffer, vbose bool) { } else /* regular */ { fmtstr = `%+v` } - appendQuoteKey(buf, f.K) + AppendQuoteKey(buf, f.K) buf.WriteByte('=') - appendQuoteValue(buf, fmt.Sprintf(fmtstr, f.V)) + appendValuef(buf, fmtstr, f.V) } // Value returns the formatted value string of this Field. @@ -31,6 +39,25 @@ func (f Field) Value(vbose bool) string { fmtstr = `%+v` } buf := byteutil.Buffer{B: make([]byte, 0, bufsize/2)} - appendQuoteValue(&buf, fmt.Sprintf(fmtstr, f.V)) + 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) +} |