Skip to content
Open
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
10 changes: 8 additions & 2 deletions src/net/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package url
import (
"errors"
"fmt"
"maps"
"net/netip"
"path"
"slices"
Expand Down Expand Up @@ -1048,7 +1047,14 @@ func (v Values) Encode() string {
return ""
}
var buf strings.Builder
for _, k := range slices.Sorted(maps.Keys(v)) {
// To minimize allocations, instead of using slices.Sorted(maps.Keys(v)),
// we pre-size the slice in which we collect v's keys and then sort it.
keys := make([]string, 0, len(v))
for k := range v {
keys = append(keys, k)
}
slices.Sort(keys)
for _, k := range keys {
vs := v[k]
keyEscaped := QueryEscape(k)
for _, v := range vs {
Expand Down
9 changes: 9 additions & 0 deletions src/net/url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,15 @@ func TestEncodeQuery(t *testing.T) {
}
}

func BenchmarkEncodeQuery(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
for _, tt := range encodeQueryTests {
tt.m.Encode()
}
}
}

var resolvePathTests = []struct {
base, ref, expected string
}{
Expand Down