diff --git a/go.mod b/go.mod index e54b38cbe..b5348d233 100644 --- a/go.mod +++ b/go.mod @@ -5,11 +5,11 @@ go 1.19 require ( codeberg.org/gruf/go-bytesize v1.0.2 codeberg.org/gruf/go-byteutil v1.1.2 - codeberg.org/gruf/go-cache/v3 v3.2.2 + codeberg.org/gruf/go-cache/v3 v3.2.3 codeberg.org/gruf/go-debug v1.3.0 codeberg.org/gruf/go-errors/v2 v2.1.1 codeberg.org/gruf/go-fastcopy v1.1.2 - codeberg.org/gruf/go-kv v1.5.2 + codeberg.org/gruf/go-kv v1.6.0 codeberg.org/gruf/go-logger/v2 v2.2.1 codeberg.org/gruf/go-mutexes v1.1.5 codeberg.org/gruf/go-runners v1.6.0 @@ -54,8 +54,8 @@ require ( go.uber.org/automaxprocs v1.5.1 golang.org/x/crypto v0.5.0 golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d - golang.org/x/net v0.7.0 golang.org/x/image v0.5.0 + golang.org/x/net v0.7.0 golang.org/x/oauth2 v0.4.0 golang.org/x/text v0.7.0 gopkg.in/mcuadros/go-syslog.v2 v2.3.0 @@ -72,7 +72,7 @@ require ( codeberg.org/gruf/go-fastpath/v2 v2.0.0 // indirect codeberg.org/gruf/go-hashenc v1.0.2 // indirect codeberg.org/gruf/go-iotools v0.0.0-20221224124424-3386841cb225 // indirect - codeberg.org/gruf/go-mangler v1.2.2 // indirect + codeberg.org/gruf/go-mangler v1.2.3 // indirect codeberg.org/gruf/go-maps v1.0.3 // indirect codeberg.org/gruf/go-pools v1.1.0 // indirect github.com/aymerick/douceur v0.2.0 // indirect diff --git a/go.sum b/go.sum index a8302697a..28fd5657e 100644 --- a/go.sum +++ b/go.sum @@ -49,8 +49,8 @@ codeberg.org/gruf/go-bytesize v1.0.2/go.mod h1:n/GU8HzL9f3UNp/mUKyr1qVmTlj7+xacp codeberg.org/gruf/go-byteutil v1.0.0/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU= codeberg.org/gruf/go-byteutil v1.1.2 h1:TQLZtTxTNca9xEfDIndmo7nBYxeS94nrv/9DS3Nk5Tw= codeberg.org/gruf/go-byteutil v1.1.2/go.mod h1:cWM3tgMCroSzqoBXUXMhvxTxYJp+TbCr6ioISRY5vSU= -codeberg.org/gruf/go-cache/v3 v3.2.2 h1:hq6/RITgpcArjzbYSyo3uFxfIw7wW3KqAQjEaN7dj58= -codeberg.org/gruf/go-cache/v3 v3.2.2/go.mod h1:+Eje6nCvN8QF71VyYjMWMnkdv6t1kHnCO/SvyC4K12Q= +codeberg.org/gruf/go-cache/v3 v3.2.3 h1:HL7UZN9zks2lsYdGTAZONJqVd2jZtj4NgG/1qi8EcJk= +codeberg.org/gruf/go-cache/v3 v3.2.3/go.mod h1:up7za8FtNdtttcx6AJ8ffqkrSkXDGTilsd9yJ0IyhfM= codeberg.org/gruf/go-debug v1.3.0 h1:PIRxQiWUFKtGOGZFdZ3Y0pqyfI0Xr87j224IYe2snZs= codeberg.org/gruf/go-debug v1.3.0/go.mod h1:N+vSy9uJBQgpQcJUqjctvqFz7tBHJf+S/PIjLILzpLg= codeberg.org/gruf/go-errors/v2 v2.0.0/go.mod h1:ZRhbdhvgoUA3Yw6e56kd9Ox984RrvbEFC2pOXyHDJP4= @@ -67,12 +67,12 @@ codeberg.org/gruf/go-hashenc v1.0.2 h1:U3jH6zMXZiL96czD/qaJd8OR2h7LlBzGv/2WxnMHI codeberg.org/gruf/go-hashenc v1.0.2/go.mod h1:eK+A8clLcEN/m1nftNsRId0kfYDQnETnuIfBGZ8Gvsg= codeberg.org/gruf/go-iotools v0.0.0-20221224124424-3386841cb225 h1:tP9YvEBfADGG3mXkfrALLadlcbrZsFsWKZvFtUZtrt8= codeberg.org/gruf/go-iotools v0.0.0-20221224124424-3386841cb225/go.mod h1:B8uq4yHtIcKXhBZT9C/SYisz25lldLHMVpwZPz4ADLQ= -codeberg.org/gruf/go-kv v1.5.2 h1:B0RkAXLUXYn3Za1NzTXOcUvAc+JUC2ZadTMkCUDa0mc= -codeberg.org/gruf/go-kv v1.5.2/go.mod h1:al6ASW/2CbGqz2YcM8B00tvWnVi1bU1CH3HYs5tZxo4= +codeberg.org/gruf/go-kv v1.6.0 h1:mCsERVt1Izsb0ZB7r7y/I6J/5sQhuL2Gmh/SqHWBJUY= +codeberg.org/gruf/go-kv v1.6.0/go.mod h1:O/YkSvKiS9XsRolM3rqCd9YJmND7dAXu9z+PrlYO4bc= codeberg.org/gruf/go-logger/v2 v2.2.1 h1:RP2u059EQKTBFV3cN8X6xDxNk2RkzqdgXGKflKqB7Oc= codeberg.org/gruf/go-logger/v2 v2.2.1/go.mod h1:m/vBfG5jNUmYXI8Hg9aVSk7Pn8YgEBITQB/B/CzdRss= -codeberg.org/gruf/go-mangler v1.2.2 h1:fisdWXa6dW4p1uYdbz5Of3R4lDDFPuRqKavGI9O03Rc= -codeberg.org/gruf/go-mangler v1.2.2/go.mod h1:X/7URkFhLBAVKkTxmqF11Oxw3A6pSSxgPeHssQaiq28= +codeberg.org/gruf/go-mangler v1.2.3 h1:sj0dey2lF5GRQL7fXmCY0wPNaI5JrROiThb0VDbzF8A= +codeberg.org/gruf/go-mangler v1.2.3/go.mod h1:X/7URkFhLBAVKkTxmqF11Oxw3A6pSSxgPeHssQaiq28= codeberg.org/gruf/go-maps v1.0.3 h1:VDwhnnaVNUIy5O93CvkcE2IZXnMB1+IJjzfop9V12es= codeberg.org/gruf/go-maps v1.0.3/go.mod h1:D5LNDxlC9rsDuVQVM6JObaVGAdHB6g2dTdOdkh1aXWA= codeberg.org/gruf/go-mutexes v1.1.5 h1:8Y8DwCGf24MyzOSaPvLrtk/B4ecVx4z+fppL6dY+PG8= diff --git a/vendor/codeberg.org/gruf/go-kv/field.go b/vendor/codeberg.org/gruf/go-kv/field.go index 86a53b173..79c43822a 100644 --- a/vendor/codeberg.org/gruf/go-kv/field.go +++ b/vendor/codeberg.org/gruf/go-kv/field.go @@ -75,7 +75,7 @@ type Field struct { // Key returns the formatted key string of this Field. func (f Field) Key() string { buf := byteutil.Buffer{B: make([]byte, 0, bufsize/2)} - AppendQuoteKey(&buf, f.K) + AppendQuote(&buf, f.K) return buf.String() } diff --git a/vendor/codeberg.org/gruf/go-kv/field_fmt.go b/vendor/codeberg.org/gruf/go-kv/field_fmt.go index fcdcc5e17..33cec482b 100644 --- a/vendor/codeberg.org/gruf/go-kv/field_fmt.go +++ b/vendor/codeberg.org/gruf/go-kv/field_fmt.go @@ -25,7 +25,7 @@ func (f Field) AppendFormat(buf *byteutil.Buffer, vbose bool) { } else /* regular */ { fmtstr = `%+v` } - AppendQuoteKey(buf, f.K) + AppendQuote(buf, f.K) buf.WriteByte('=') appendValuef(buf, fmtstr, f.V) } @@ -50,7 +50,7 @@ func appendValuef(buf *byteutil.Buffer, format string, args ...interface{}) { fmtbuf.B = fmt.Appendf(fmtbuf.B, format, args...) // Append quoted value to dst buffer - AppendQuoteValue(buf, fmtbuf.String()) + AppendQuote(buf, fmtbuf.String()) // Drop overly large capacity buffers if fmtbuf.Cap() > int(^uint16(0)) { diff --git a/vendor/codeberg.org/gruf/go-kv/field_format.go b/vendor/codeberg.org/gruf/go-kv/field_format.go index 4fa7a8dcf..18f35a6b9 100644 --- a/vendor/codeberg.org/gruf/go-kv/field_format.go +++ b/vendor/codeberg.org/gruf/go-kv/field_format.go @@ -16,7 +16,7 @@ func (f Field) AppendFormat(buf *byteutil.Buffer, vbose bool) { } else /* regular */ { fmtstr = "{:v}" } - AppendQuoteKey(buf, f.K) + AppendQuote(buf, f.K) buf.WriteByte('=') format.Appendf(buf, fmtstr, f.V) } diff --git a/vendor/codeberg.org/gruf/go-kv/format/format.go b/vendor/codeberg.org/gruf/go-kv/format/format.go index 7edc4475f..ab20e01f4 100644 --- a/vendor/codeberg.org/gruf/go-kv/format/format.go +++ b/vendor/codeberg.org/gruf/go-kv/format/format.go @@ -166,21 +166,21 @@ func (f format) AppendByte(b byte) { // Always quoted case f.Key(): f.Buffer.B = append(f.Buffer.B, '\'') - f.Buffer.B = append(f.Buffer.B, byte2str(b)...) + f.Buffer.B = append(f.Buffer.B, Byte2Str(b)...) f.Buffer.B = append(f.Buffer.B, '\'') // Always quoted ASCII with type case f.Verbose(): f._AppendPrimitiveTyped(func(f format) { f.Buffer.B = append(f.Buffer.B, '\'') - f.Buffer.B = append(f.Buffer.B, byte2str(b)...) + f.Buffer.B = append(f.Buffer.B, Byte2Str(b)...) f.Buffer.B = append(f.Buffer.B, '\'') }) // Always quoted case f.Value(): f.Buffer.B = append(f.Buffer.B, '\'') - f.Buffer.B = append(f.Buffer.B, byte2str(b)...) + f.Buffer.B = append(f.Buffer.B, Byte2Str(b)...) f.Buffer.B = append(f.Buffer.B, '\'') // Append as raw byte @@ -195,16 +195,16 @@ func (f format) AppendBytes(b []byte) { case b == nil: f.AppendNil() - // Handle bytes as string key + // Quoted only if spaces/requires escaping case f.Key(): - f.AppendStringKey(b2s(b)) + f.AppendStringSafe(b2s(b)) // Append as separate ASCII quoted bytes in slice case f.Verbose(): f._AppendArrayTyped(func(f format) { for i := 0; i < len(b); i++ { f.Buffer.B = append(f.Buffer.B, '\'') - f.Buffer.B = append(f.Buffer.B, byte2str(b[i])...) + f.Buffer.B = append(f.Buffer.B, Byte2Str(b[i])...) f.Buffer.B = append(f.Buffer.B, `',`...) } if len(b) > 0 { @@ -212,9 +212,9 @@ func (f format) AppendBytes(b []byte) { } }) - // Append as quoted string + // Quoted only if spaces/requires escaping case f.Value(): - f.AppendStringQuoted(b2s(b)) + f.AppendStringSafe(b2s(b)) // Append as raw bytes default: @@ -260,9 +260,9 @@ func (f format) AppendRunes(r []rune) { case r == nil: f.AppendNil() - // Handle bytes as string key + // Quoted only if spaces/requires escaping case f.Key(): - f.AppendStringKey(string(r)) + f.AppendStringSafe(string(r)) // Append as separate ASCII quoted bytes in slice case f.Verbose(): @@ -276,9 +276,9 @@ func (f format) AppendRunes(r []rune) { } }) - // Append as quoted string + // Quoted only if spaces/requires escaping case f.Value(): - f.AppendStringQuoted(string(r)) + f.AppendStringSafe(string(r)) // Append as raw bytes default: @@ -292,7 +292,7 @@ func (f format) AppendString(s string) { switch { // Quoted only if spaces/requires escaping case f.Key(): - f.AppendStringKey(s) + f.AppendStringSafe(s) // Always quoted with type case f.Verbose(): @@ -300,9 +300,9 @@ func (f format) AppendString(s string) { f.AppendStringQuoted(s) }) - // Always quoted string + // Quoted only if spaces/requires escaping case f.Value(): - f.AppendStringQuoted(s) + f.AppendStringSafe(s) // All else default: @@ -310,15 +310,15 @@ func (f format) AppendString(s string) { } } -func (f format) AppendStringKey(s string) { - if len(s) > SingleTermLine || !strconv.CanBackquote(s) { +func (f format) AppendStringSafe(s string) { + if len(s) > SingleTermLine || !IsSafeASCII(s) { // Requires quoting AND escaping f.Buffer.B = strconv.AppendQuote(f.Buffer.B, s) } else if ContainsDoubleQuote(s) { // Contains double quotes, needs escaping f.Buffer.B = AppendEscape(f.Buffer.B, s) - } else if len(s) < 1 || ContainsSpaceOrTab(s) { - // Contains space, needs quotes + } else if len(s) == 0 || ContainsSpaceOrTab(s) { + // Contains space / empty, needs quotes f.Buffer.B = append(f.Buffer.B, '"') f.Buffer.B = append(f.Buffer.B, s...) f.Buffer.B = append(f.Buffer.B, '"') @@ -329,7 +329,7 @@ func (f format) AppendStringKey(s string) { } func (f format) AppendStringQuoted(s string) { - if len(s) > SingleTermLine || !strconv.CanBackquote(s) { + if len(s) > SingleTermLine || !IsSafeASCII(s) { // Requires quoting AND escaping f.Buffer.B = strconv.AppendQuote(f.Buffer.B, s) } else if ContainsDoubleQuote(s) { @@ -760,7 +760,7 @@ func (f format) AppendStructFields(v reflect.Value, t reflect.Type) { tfield := t.Field(i) // Append field name - f.AppendStringKey(tfield.Name) + f.AppendStringSafe(tfield.Name) f.Buffer.B = append(f.Buffer.B, '=') f.SetValue().AppendInterfaceOrReflectNext(vfield, tfield.Type) @@ -811,7 +811,7 @@ func (f format) _AppendMethodType(method func() string, i interface{}) (ok bool) switch { // Append as key formatted case f.Key(): - f.AppendStringKey(result) + f.AppendStringSafe(result) // Append as always quoted case f.Value(): @@ -890,33 +890,3 @@ func (f format) _AppendFieldType(appendFields func(format)) { appendFields(f) f.Buffer.B = append(f.Buffer.B, '}') } - -// byte2str returns 'c' as a string, escaping if necessary. -func byte2str(c byte) string { - switch c { - case '\a': - return `\a` - case '\b': - return `\b` - case '\f': - return `\f` - case '\n': - return `\n` - case '\r': - return `\r` - case '\t': - return `\t` - case '\v': - return `\v` - case '\'': - return `\\` - default: - if c < ' ' { - const hex = "0123456789abcdef" - return `\x` + - string(hex[c>>4]) + - string(hex[c&0xF]) - } - return string(c) - } -} diff --git a/vendor/codeberg.org/gruf/go-kv/format/util.go b/vendor/codeberg.org/gruf/go-kv/format/util.go index 674f81be5..b5c672ecb 100644 --- a/vendor/codeberg.org/gruf/go-kv/format/util.go +++ b/vendor/codeberg.org/gruf/go-kv/format/util.go @@ -18,11 +18,22 @@ const ( SingleTermLine = 256 ) +// IsSafeASCII checks whether string is printable (i.e. non-control char) ASCII text. +func IsSafeASCII(str string) bool { + for _, r := range str { + if (r < ' ' && r != '\t') || + r >= 0x7f { + return false + } + } + return true +} + // ContainsSpaceOrTab checks if "s" contains space or tabs. func ContainsSpaceOrTab(s string) bool { - if i := strings.IndexByte(s, ' '); i != -1 { + if i := strings.IndexByte(s, ' '); i >= 0 { return true // note using indexbyte as it is ASM. - } else if i := strings.IndexByte(s, '\t'); i != -1 { + } else if i := strings.IndexByte(s, '\t'); i >= 0 { return true } return false @@ -30,7 +41,7 @@ func ContainsSpaceOrTab(s string) bool { // ContainsDoubleQuote checks if "s" contains a double quote. func ContainsDoubleQuote(s string) bool { - return (strings.IndexByte(s, '"') != -1) + return (strings.IndexByte(s, '"') >= 0) } // AppendEscape will append 's' to 'dst' and escape any double quotes. @@ -57,6 +68,36 @@ func AppendEscape(dst []byte, str string) []byte { return dst } +// Byte2Str returns 'c' as a string, escaping if necessary. +func Byte2Str(c byte) string { + switch c { + case '\a': + return `\a` + case '\b': + return `\b` + case '\f': + return `\f` + case '\n': + return `\n` + case '\r': + return `\r` + case '\t': + return `\t` + case '\v': + return `\v` + case '\'': + return `\\` + default: + if c < ' ' { + const hex = "0123456789abcdef" + return `\x` + + string(hex[c>>4]) + + string(hex[c&0xF]) + } + return string(c) + } +} + // isNil will safely check if 'v' is nil without dealing with weird Go interface nil bullshit. func isNil(i interface{}) bool { type eface struct{ _type, data unsafe.Pointer } //nolint diff --git a/vendor/codeberg.org/gruf/go-kv/util.go b/vendor/codeberg.org/gruf/go-kv/util.go index 3d249e5d4..dfd91ad9d 100644 --- a/vendor/codeberg.org/gruf/go-kv/util.go +++ b/vendor/codeberg.org/gruf/go-kv/util.go @@ -2,70 +2,130 @@ package kv import ( "strconv" + "strings" "codeberg.org/gruf/go-byteutil" "codeberg.org/gruf/go-kv/format" ) -// AppendQuoteKey will append and escape/quote a formatted key string. -func AppendQuoteKey(buf *byteutil.Buffer, str string) { +// AppendQuote will append (and escape/quote where necessary) a formatted field string. +func AppendQuote(buf *byteutil.Buffer, str string) { switch { - case len(str) > format.SingleTermLine || !strconv.CanBackquote(str): - // Append quoted and escaped string - buf.B = strconv.AppendQuote(buf.B, str) - case format.ContainsDoubleQuote(str): - // Double quote and escape string - buf.B = append(buf.B, '"') - buf.B = format.AppendEscape(buf.B, str) - buf.B = append(buf.B, '"') - case len(str) < 1 || format.ContainsSpaceOrTab(str): - // Double quote this string as-is - buf.WriteString(`"` + str + `"`) - default: - // Append string as-is - buf.WriteString(str) - } -} + case len(str) == 0: + // Append empty quotes. + buf.B = append(buf.B, `""`...) + return -// AppendQuoteValue will append and escape/quote a formatted value string. -func AppendQuoteValue(buf *byteutil.Buffer, str string) { - switch { - case len(str) > format.SingleTermLine || !strconv.CanBackquote(str): - // Append quoted and escaped string + case len(str) == 1: + // Append quote single byte. + appendQuoteByte(buf, 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 !doubleQuoted(str): + + case !isQuoted(str): + // 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) { - // Double quote and escape string + // 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 - } else if format.ContainsSpaceOrTab(str) { - // Double quote this string as-is - buf.WriteString(`"` + str + `"`) - return } + + // Quote un-enclosed spaces. + buf.B = append(buf.B, '"') + buf.B = append(buf.B, str...) + buf.B = append(buf.B, '"') + return } - // Append string as-is - buf.WriteString(str) + // Double quoted, enclosed in braces, or + // literally anything else: append as-is. + buf.B = append(buf.B, str...) + return } -// doubleQuoted will return whether 'str' is double quoted. -func doubleQuoted(str string) bool { - if len(str) < 2 || - str[0] != '"' || str[len(str)-1] != '"' { +// appendEscapeByte will append byte to buffer, quoting and escaping where necessary. +func appendQuoteByte(buf *byteutil.Buffer, c byte) { + switch c { + // Double quote space. + case ' ': + buf.B = append(buf.B, '"', c, '"') + + // Escape + double quote. + case '\a': + buf.B = append(buf.B, '"', '\\', 'a', '"') + case '\b': + buf.B = append(buf.B, '"', '\\', 'b', '"') + case '\f': + buf.B = append(buf.B, '"', '\\', 'f', '"') + case '\n': + buf.B = append(buf.B, '"', '\\', 'n', '"') + case '\r': + buf.B = append(buf.B, '"', '\\', 'r', '"') + case '\t': + buf.B = append(buf.B, '"', '\\', 't', '"') + case '\v': + buf.B = append(buf.B, '"', '\\', 'v', '"') + + // Append as-is. + default: + buf.B = append(buf.B, c) + } +} + +// 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 } - var delim bool - for i := len(str) - 2; i >= 0; i-- { - switch str[i] { - case '\\': - delim = !delim - default: - return !delim - } + + // Check for starting char in string. + i := strings.IndexByte(str, start) + if i < 0 { + return false } - return !delim + + // Check before space. + return i < sp } diff --git a/vendor/codeberg.org/gruf/go-mangler/LICENSE b/vendor/codeberg.org/gruf/go-mangler/LICENSE index e4163ae35..dffbdf0c9 100644 --- a/vendor/codeberg.org/gruf/go-mangler/LICENSE +++ b/vendor/codeberg.org/gruf/go-mangler/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 gruf +Copyright (c) 2023 gruf Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/vendor/codeberg.org/gruf/go-mangler/README.md b/vendor/codeberg.org/gruf/go-mangler/README.md index c79b87546..d0de88a43 100644 --- a/vendor/codeberg.org/gruf/go-mangler/README.md +++ b/vendor/codeberg.org/gruf/go-mangler/README.md @@ -18,21 +18,19 @@ goarch: amd64 pkg: codeberg.org/gruf/go-mangler cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz BenchmarkMangle -BenchmarkMangle-8 723278 1593 ns/op 1168 B/op 120 allocs/op +BenchmarkMangle-8 533011 2003 ns/op 1168 B/op 120 allocs/op +BenchmarkMangleKnown +BenchmarkMangleKnown-8 817060 1458 ns/op 1168 B/op 120 allocs/op BenchmarkJSON -BenchmarkJSON-8 199360 6116 ns/op 4243 B/op 142 allocs/op -BenchmarkBinary -BenchmarkBinary-8 ------ ---- ns/op ---- B/op --- allocs/op +BenchmarkJSON-8 188637 5899 ns/op 4211 B/op 142 allocs/op BenchmarkFmt -BenchmarkFmt-8 168500 7111 ns/op 2256 B/op 161 allocs/op -BenchmarkKelindarBinary -BenchmarkKelindarBinary-8 ------ ---- ns/op ---- B/op --- allocs/op +BenchmarkFmt-8 162735 7053 ns/op 2257 B/op 161 allocs/op BenchmarkFxmackerCbor -BenchmarkFxmackerCbor-8 361416 3255 ns/op 1495 B/op 122 allocs/op +BenchmarkFxmackerCbor-8 362403 3336 ns/op 1496 B/op 122 allocs/op BenchmarkMitchellhHashStructure -BenchmarkMitchellhHashStructure-8 117672 10493 ns/op 8443 B/op 961 allocs/op +BenchmarkMitchellhHashStructure-8 113982 10079 ns/op 8443 B/op 961 allocs/op BenchmarkCnfStructhash -BenchmarkCnfStructhash-8 7078 161926 ns/op 288644 B/op 5843 allocs/op +BenchmarkCnfStructhash-8 7162 167613 ns/op 288619 B/op 5841 allocs/op PASS -ok codeberg.org/gruf/go-mangler 14.377s +ok codeberg.org/gruf/go-mangler 11.352s ``` \ No newline at end of file diff --git a/vendor/codeberg.org/gruf/go-mangler/helpers.go b/vendor/codeberg.org/gruf/go-mangler/helpers.go index 0ca4498af..6658fbef4 100644 --- a/vendor/codeberg.org/gruf/go-mangler/helpers.go +++ b/vendor/codeberg.org/gruf/go-mangler/helpers.go @@ -5,6 +5,43 @@ import ( "unsafe" ) +type ( + // serializing interfacing types. + stringer interface{ String() string } + binarymarshaler interface{ MarshalBinary() ([]byte, error) } + textmarshaler interface{ MarshalText() ([]byte, error) } + jsonmarshaler interface{ MarshalJSON() ([]byte, error) } +) + +func append_uint16(b []byte, u uint16) []byte { + return append(b, // LE + byte(u), + byte(u>>8), + ) +} + +func append_uint32(b []byte, u uint32) []byte { + return append(b, // LE + byte(u), + byte(u>>8), + byte(u>>16), + byte(u>>24), + ) +} + +func append_uint64(b []byte, u uint64) []byte { + return append(b, // LE + byte(u), + byte(u>>8), + byte(u>>16), + byte(u>>24), + byte(u>>32), + byte(u>>40), + byte(u>>48), + byte(u>>56), + ) +} + func deref_ptr_mangler(mangle Mangler, count int) rMangler { return func(buf []byte, v reflect.Value) []byte { for i := 0; i < count; i++ { @@ -43,6 +80,16 @@ func deref_ptr_rmangler(mangle rMangler, count int) rMangler { } } +func array_to_slice_mangler(mangle Mangler) rMangler { + return func(buf []byte, v reflect.Value) []byte { + // Get slice of whole array + v = v.Slice(0, v.Len()) + + // Mangle as known slice type + return mangle(buf, v.Interface()) + } +} + func iter_array_mangler(mangle Mangler) rMangler { return func(buf []byte, v reflect.Value) []byte { n := v.Len() @@ -78,7 +125,7 @@ func iter_map_rmangler(kMangle, vMangle rMangler) rMangler { buf = kMangle(buf, r.Key()) buf = append(buf, ':') buf = vMangle(buf, r.Value()) - buf = append(buf, '.') + buf = append(buf, ',') } if v.Len() > 0 { buf = buf[:len(buf)-1] diff --git a/vendor/codeberg.org/gruf/go-mangler/init.go b/vendor/codeberg.org/gruf/go-mangler/init.go new file mode 100644 index 000000000..2c20e6761 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-mangler/init.go @@ -0,0 +1,119 @@ +package mangler + +import ( + "net" + "net/netip" + "net/url" + "reflect" + "time" + _ "unsafe" +) + +func init() { + // Register standard library performant manglers. + Register(reflect.TypeOf(net.IPAddr{}), mangle_ipaddr) + Register(reflect.TypeOf(&net.IPAddr{}), mangle_ipaddr_ptr) + Register(reflect.TypeOf(netip.Addr{}), mangle_addr) + Register(reflect.TypeOf(&netip.Addr{}), mangle_addr_ptr) + Register(reflect.TypeOf(netip.AddrPort{}), mangle_addrport) + Register(reflect.TypeOf(&netip.AddrPort{}), mangle_addrport_ptr) + Register(reflect.TypeOf(time.Time{}), mangle_time) + Register(reflect.TypeOf(&time.Time{}), mangle_time_ptr) + Register(reflect.TypeOf(url.URL{}), mangle_url) + Register(reflect.TypeOf(&url.URL{}), mangle_url_ptr) +} + +//go:linkname time_sec time.(*Time).sec +func time_sec(*time.Time) int64 + +//go:linkname time_nsec time.(*Time).nsec +func time_nsec(*time.Time) int32 + +//go:linkname time_stripMono time.(*Time).stripMono +func time_stripMono(*time.Time) + +func mangle_url(buf []byte, a any) []byte { + u := (*url.URL)(iface_value(a)) + return append(buf, u.String()...) +} + +func mangle_url_ptr(buf []byte, a any) []byte { + if ptr := (*url.URL)(iface_value(a)); ptr != nil { + s := ptr.String() + buf = append(buf, '1') + return append(buf, s...) + } + buf = append(buf, '0') + return buf +} + +func mangle_time(buf []byte, a any) []byte { + t := *(*time.Time)(iface_value(a)) + time_stripMono(&t) // force non-monotonic time value. + buf = append_uint64(buf, uint64(time_sec(&t))) + buf = append_uint32(buf, uint32(time_nsec(&t))) + return buf +} + +func mangle_time_ptr(buf []byte, a any) []byte { + if ptr := (*time.Time)(iface_value(a)); ptr != nil { + t := *ptr + buf = append(buf, '1') + time_stripMono(&t) // force non-monotonic time value. + buf = append_uint64(buf, uint64(time_sec(&t))) + buf = append_uint32(buf, uint32(time_nsec(&t))) + return buf + } + buf = append(buf, '0') + return buf +} + +func mangle_ipaddr(buf []byte, a any) []byte { + i := *(*net.IPAddr)(iface_value(a)) + buf = append(buf, i.IP...) + buf = append(buf, i.Zone...) + return buf +} + +func mangle_ipaddr_ptr(buf []byte, a any) []byte { + if ptr := (*net.IPAddr)(iface_value(a)); ptr != nil { + buf = append(buf, '1') + buf = append(buf, ptr.IP...) + buf = append(buf, ptr.Zone...) + return buf + } + buf = append(buf, '0') + return buf +} + +func mangle_addr(buf []byte, a any) []byte { + i := (*netip.Addr)(iface_value(a)) + b, _ := i.MarshalBinary() + return append(buf, b...) +} + +func mangle_addr_ptr(buf []byte, a any) []byte { + if ptr := (*netip.Addr)(iface_value(a)); ptr != nil { + buf = append(buf, '1') + b, _ := ptr.MarshalBinary() + return append(buf, b...) + } + buf = append(buf, '0') + return buf +} + +func mangle_addrport(buf []byte, a any) []byte { + i := (*netip.AddrPort)(iface_value(a)) + b, _ := i.MarshalBinary() + return append(buf, b...) +} + +func mangle_addrport_ptr(buf []byte, a any) []byte { + if ptr := (*netip.AddrPort)(iface_value(a)); ptr != nil { + buf = append(buf, '1') + b, _ := ptr.MarshalBinary() + return append(buf, b...) + } + buf = append(buf, '0') + return buf +} diff --git a/vendor/codeberg.org/gruf/go-mangler/load.go b/vendor/codeberg.org/gruf/go-mangler/load.go index 20850f55c..752e5d337 100644 --- a/vendor/codeberg.org/gruf/go-mangler/load.go +++ b/vendor/codeberg.org/gruf/go-mangler/load.go @@ -1,10 +1,7 @@ package mangler import ( - "encoding" - "net/url" "reflect" - "time" ) // loadMangler is the top-most Mangler load function. It guarantees that a Mangler @@ -13,6 +10,11 @@ func loadMangler(a any, t reflect.Type) Mangler { // Load mangler function mng, rmng := load(a, t) + if mng != nil { + // Use preferred mangler. + return mng + } + if rmng != nil { // Wrap reflect mangler to handle iface return func(buf []byte, a any) []byte { @@ -20,12 +22,8 @@ func loadMangler(a any, t reflect.Type) Mangler { } } - if mng == nil { - // No mangler function could be determined - panic("cannot mangle type: " + t.String()) - } - - return mng + // No mangler function could be determined + panic("cannot mangle type: " + t.String()) } // load will load a Mangler or reflect Mangler for given type and iface 'a'. @@ -42,41 +40,33 @@ func load(a any, t reflect.Type) (Mangler, rMangler) { a = v.Interface() } - // Check in fast iface type switch - if mng := loadIface(a); mng != nil { + // Check for Mangled implementation. + if _, ok := a.(Mangled); ok { + return mangle_mangled, nil + } + + // Search mangler by reflection. + mng, rmng := loadReflect(t) + if mng != nil { return mng, nil } - // Search by reflection - return loadReflect(t) + // Prefer iface mangler, else, reflected. + return loadIface(a), rmng } -// loadIface is used as a first-resort interface{} type switcher loader -// for types implementing Mangled and providing performant alternative -// Mangler functions for standard library types to avoid reflection. +// loadIface is used as a near-last-resort interface{} type switch +// loader for types implementating other known (slower) functions. func loadIface(a any) Mangler { switch a.(type) { - case Mangled: - return mangle_mangled - - case time.Time: - return mangle_time - - case *time.Time: - return mangle_time_ptr - - case *url.URL: - return mangle_stringer - - case encoding.BinaryMarshaler: + case binarymarshaler: return mangle_binary - - // NOTE: - // we don't just handle ALL fmt.Stringer types as often - // the output is large and unwieldy and this interface - // switch is for types it would be faster to avoid reflection. - // If they want better performance they can implement Mangled{}. - + case stringer: + return mangle_stringer + case textmarshaler: + return mangle_text + case jsonmarshaler: + return mangle_json default: return nil } @@ -159,18 +149,19 @@ func loadReflectPtr(et reflect.Type) (Mangler, rMangler) { } if et.Kind() == reflect.Array { + // Array elem type + at := et.Elem() + // Special case of addressable (sliceable) array - if mng := loadReflectKnownSlice(et); mng != nil { - if count == 1 { - return mng, nil - } - return nil, deref_ptr_mangler(mng, count-1) + if mng := loadReflectKnownSlice(at); mng != nil { + rmng := array_to_slice_mangler(mng) + return nil, deref_ptr_rmangler(rmng, count) } // Look for an array mangler function, this will // access elements by index using reflect.Value and // pass each one to a separate mangler function. - if rmng := loadReflectArray(et); rmng != nil { + if rmng := loadReflectArray(at); rmng != nil { return nil, deref_ptr_rmangler(rmng, count) } @@ -321,14 +312,14 @@ func loadReflectMap(kt, vt reflect.Type) rMangler { mng, rmng = load(nil, vt) switch { - // Wrap key mangler to reflect + // Wrap value mangler to reflect case mng != nil: mng := mng // take our own ptr vmng = func(buf []byte, v reflect.Value) []byte { return mng(buf, v.Interface()) } - // Use reflect key mangler as-is + // Use reflect value mangler as-is case rmng != nil: vmng = rmng diff --git a/vendor/codeberg.org/gruf/go-mangler/mangle.go b/vendor/codeberg.org/gruf/go-mangler/mangle.go index 983216003..e12748e67 100644 --- a/vendor/codeberg.org/gruf/go-mangler/mangle.go +++ b/vendor/codeberg.org/gruf/go-mangler/mangle.go @@ -1,19 +1,13 @@ package mangler import ( - "encoding/binary" "reflect" "sync" "unsafe" ) -var ( - // manglers is a map of runtime type ptrs => Mangler functions. - manglers = sync.Map{} - - // bin is a short-hand for our chosen byteorder encoding. - bin = binary.LittleEndian -) +// manglers is a map of runtime type ptrs => Mangler functions. +var manglers sync.Map // Mangled is an interface that allows any type to implement a custom // Mangler function to improve performance when mangling this type. @@ -142,9 +136,15 @@ func Append(b []byte, a any) []byte { // - float32,float64 // - complex64,complex128 // - all type aliases of above -// - time.Time{}, *url.URL{} +// - time.Time{} +// - url.URL{} +// - net.IPAddr{} +// - netip.Addr{}, netip.AddrPort{} // - mangler.Mangled{} +// - fmt.Stringer{} +// - json.Marshaler{} // - encoding.BinaryMarshaler{} +// - encoding.TextMarshaler{} // - all pointers to the above // - all slices / arrays of the above // - all map keys / values of the above diff --git a/vendor/codeberg.org/gruf/go-mangler/manglers.go b/vendor/codeberg.org/gruf/go-mangler/manglers.go index 52f9f0082..b9ba81705 100644 --- a/vendor/codeberg.org/gruf/go-mangler/manglers.go +++ b/vendor/codeberg.org/gruf/go-mangler/manglers.go @@ -1,10 +1,7 @@ package mangler import ( - "encoding" - "fmt" "math/bits" - "time" _ "unsafe" ) @@ -87,13 +84,13 @@ func mangle_8bit_slice(buf []byte, a any) []byte { } func mangle_16bit(buf []byte, a any) []byte { - return bin.AppendUint16(buf, *(*uint16)(iface_value(a))) + return append_uint16(buf, *(*uint16)(iface_value(a))) } func mangle_16bit_ptr(buf []byte, a any) []byte { if ptr := (*uint16)(iface_value(a)); ptr != nil { buf = append(buf, '1') - return bin.AppendUint16(buf, *ptr) + return append_uint16(buf, *ptr) } buf = append(buf, '0') return buf @@ -101,19 +98,19 @@ func mangle_16bit_ptr(buf []byte, a any) []byte { func mangle_16bit_slice(buf []byte, a any) []byte { for _, u := range *(*[]uint16)(iface_value(a)) { - buf = bin.AppendUint16(buf, u) + buf = append_uint16(buf, u) } return buf } func mangle_32bit(buf []byte, a any) []byte { - return bin.AppendUint32(buf, *(*uint32)(iface_value(a))) + return append_uint32(buf, *(*uint32)(iface_value(a))) } func mangle_32bit_ptr(buf []byte, a any) []byte { if ptr := (*uint32)(iface_value(a)); ptr != nil { buf = append(buf, '1') - return bin.AppendUint32(buf, *ptr) + return append_uint32(buf, *ptr) } buf = append(buf, '0') return buf @@ -121,19 +118,19 @@ func mangle_32bit_ptr(buf []byte, a any) []byte { func mangle_32bit_slice(buf []byte, a any) []byte { for _, u := range *(*[]uint32)(iface_value(a)) { - buf = bin.AppendUint32(buf, u) + buf = append_uint32(buf, u) } return buf } func mangle_64bit(buf []byte, a any) []byte { - return bin.AppendUint64(buf, *(*uint64)(iface_value(a))) + return append_uint64(buf, *(*uint64)(iface_value(a))) } func mangle_64bit_ptr(buf []byte, a any) []byte { if ptr := (*uint64)(iface_value(a)); ptr != nil { buf = append(buf, '1') - return bin.AppendUint64(buf, *ptr) + return append_uint64(buf, *ptr) } buf = append(buf, '0') return buf @@ -141,7 +138,7 @@ func mangle_64bit_ptr(buf []byte, a any) []byte { func mangle_64bit_slice(buf []byte, a any) []byte { for _, u := range *(*[]uint64)(iface_value(a)) { - buf = bin.AppendUint64(buf, u) + buf = append_uint64(buf, u) } return buf } @@ -187,16 +184,16 @@ type uint128 [2]uint64 func mangle_128bit(buf []byte, a any) []byte { u2 := *(*uint128)(iface_value(a)) - buf = bin.AppendUint64(buf, u2[0]) - buf = bin.AppendUint64(buf, u2[1]) + buf = append_uint64(buf, u2[0]) + buf = append_uint64(buf, u2[1]) return buf } func mangle_128bit_ptr(buf []byte, a any) []byte { if ptr := (*uint128)(iface_value(a)); ptr != nil { buf = append(buf, '1') - buf = bin.AppendUint64(buf, (*ptr)[0]) - buf = bin.AppendUint64(buf, (*ptr)[1]) + buf = append_uint64(buf, (*ptr)[0]) + buf = append_uint64(buf, (*ptr)[1]) } buf = append(buf, '0') return buf @@ -204,34 +201,12 @@ func mangle_128bit_ptr(buf []byte, a any) []byte { func mangle_128bit_slice(buf []byte, a any) []byte { for _, u2 := range *(*[]uint128)(iface_value(a)) { - buf = bin.AppendUint64(buf, u2[0]) - buf = bin.AppendUint64(buf, u2[1]) + buf = append_uint64(buf, u2[0]) + buf = append_uint64(buf, u2[1]) } return buf } -func mangle_time(buf []byte, a any) []byte { - t := *(*time.Time)(iface_value(a)) - b, err := t.MarshalBinary() - if err != nil { - panic("marshal_time: " + err.Error()) - } - return append(buf, b...) -} - -func mangle_time_ptr(buf []byte, a any) []byte { - if ptr := (*time.Time)(iface_value(a)); ptr != nil { - b, err := ptr.MarshalBinary() - if err != nil { - panic("marshal_time: " + err.Error()) - } - buf = append(buf, '1') - return append(buf, b...) - } - buf = append(buf, '0') - return buf -} - func mangle_mangled(buf []byte, a any) []byte { if v := a.(Mangled); v != nil { buf = append(buf, '1') @@ -242,7 +217,7 @@ func mangle_mangled(buf []byte, a any) []byte { } func mangle_binary(buf []byte, a any) []byte { - if v := a.(encoding.BinaryMarshaler); v != nil { + if v := a.(binarymarshaler); v != nil { b, err := v.MarshalBinary() if err != nil { panic("mangle_binary: " + err.Error()) @@ -255,10 +230,36 @@ func mangle_binary(buf []byte, a any) []byte { } func mangle_stringer(buf []byte, a any) []byte { - if v := a.(fmt.Stringer); v != nil { + if v := a.(stringer); v != nil { buf = append(buf, '1') return append(buf, v.String()...) } buf = append(buf, '0') return buf } + +func mangle_text(buf []byte, a any) []byte { + if v := a.(textmarshaler); v != nil { + b, err := v.MarshalText() + if err != nil { + panic("mangle_text: " + err.Error()) + } + buf = append(buf, '1') + return append(buf, b...) + } + buf = append(buf, '0') + return buf +} + +func mangle_json(buf []byte, a any) []byte { + if v := a.(jsonmarshaler); v != nil { + b, err := v.MarshalJSON() + if err != nil { + panic("mangle_json: " + err.Error()) + } + buf = append(buf, '1') + return append(buf, b...) + } + buf = append(buf, '0') + return buf +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 99814c157..f109c382b 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -13,7 +13,7 @@ codeberg.org/gruf/go-bytesize # codeberg.org/gruf/go-byteutil v1.1.2 ## explicit; go 1.16 codeberg.org/gruf/go-byteutil -# codeberg.org/gruf/go-cache/v3 v3.2.2 +# codeberg.org/gruf/go-cache/v3 v3.2.3 ## explicit; go 1.19 codeberg.org/gruf/go-cache/v3 codeberg.org/gruf/go-cache/v3/result @@ -39,14 +39,14 @@ codeberg.org/gruf/go-hashenc # codeberg.org/gruf/go-iotools v0.0.0-20221224124424-3386841cb225 ## explicit; go 1.19 codeberg.org/gruf/go-iotools -# codeberg.org/gruf/go-kv v1.5.2 +# codeberg.org/gruf/go-kv v1.6.0 ## explicit; go 1.19 codeberg.org/gruf/go-kv codeberg.org/gruf/go-kv/format # codeberg.org/gruf/go-logger/v2 v2.2.1 ## explicit; go 1.19 codeberg.org/gruf/go-logger/v2/level -# codeberg.org/gruf/go-mangler v1.2.2 +# codeberg.org/gruf/go-mangler v1.2.3 ## explicit; go 1.19 codeberg.org/gruf/go-mangler # codeberg.org/gruf/go-maps v1.0.3