Skip to content

Commit 3475f2e

Browse files
committed
net/url: eschew iterators in Encode
1 parent 28c93a7 commit 3475f2e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/net/url/url.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package url
1515
import (
1616
"errors"
1717
"fmt"
18-
"maps"
1918
"net/netip"
2019
"path"
2120
"slices"
@@ -1046,7 +1045,16 @@ func (v Values) Encode() string {
10461045
return ""
10471046
}
10481047
var buf strings.Builder
1049-
for _, k := range slices.Sorted(maps.Keys(v)) {
1048+
// To minimize allocations, we eschew iterators and pre-size the slice in
1049+
// which we collect v's keys.
1050+
keys := make([]string, len(v))
1051+
var i int
1052+
for k := range v {
1053+
keys[i] = k
1054+
i++
1055+
}
1056+
slices.Sort(keys)
1057+
for _, k := range keys {
10501058
vs := v[k]
10511059
keyEscaped := QueryEscape(k)
10521060
for _, v := range vs {

0 commit comments

Comments
 (0)