Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(uri): preallocate encodeObject buffer #719

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion uri/header_param_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (e *headerParamEncoder) serialize() {
e.header.Set(e.paramName, strings.Join(e.items, ","))
return
case typeObject:
kvSep, fieldSep := ',', ','
var kvSep, fieldSep byte = ',', ','
if e.explode {
kvSep = '='
}
Expand Down
22 changes: 18 additions & 4 deletions uri/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@ type Field struct {
Value string
}

func encodeObject(kvSep, fieldSep rune, fields []Field) string {
var sb strings.Builder
func encodeObject(kvSep, fieldSep byte, fields []Field) string {
var (
sb strings.Builder
size int
)
// Preallocate the buffer.
{
for _, f := range fields {
size += len(f.Name) + len(f.Value) + 2
}
if len(fields) == 1 {
// If there are less than 2 fields, we don't need to add the field separator.
size--
}
}
sb.Grow(size)
for i, f := range fields {
if i > 0 {
sb.WriteRune(fieldSep)
sb.WriteByte(fieldSep)
}
sb.WriteString(f.Name)
sb.WriteRune(kvSep)
sb.WriteByte(kvSep)
sb.WriteString(f.Value)
}
return sb.String()
Expand Down
2 changes: 1 addition & 1 deletion uri/path_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (e *PathEncoder) object() string {
return encodeObject(kvSep, fieldSep, e.fields)

case PathStyleLabel:
kvSep, fieldSep := ',', ','
var kvSep, fieldSep byte = ',', ','
if e.explode {
kvSep, fieldSep = '=', '.'
}
Expand Down