File tree Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ package url
1515import (
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 {
Original file line number Diff line number Diff line change @@ -1108,6 +1108,17 @@ var encodeQueryTests = []EncodeQueryTest{
11081108 "b" : {"b1" , "b2" , "b3" },
11091109 "c" : {"c1" , "c2" , "c3" },
11101110 }, "a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3" },
1111+ {Values {
1112+ "a" : {"a" },
1113+ "b" : {"b" },
1114+ "c" : {"c" },
1115+ "d" : {"d" },
1116+ "e" : {"e" },
1117+ "f" : {"f" },
1118+ "g" : {"g" },
1119+ "h" : {"h" },
1120+ "i" : {"i" },
1121+ }, "a=a&b=b&c=c&d=d&e=e&f=f&g=g&h=h&i=i" },
11111122}
11121123
11131124func TestEncodeQuery (t * testing.T ) {
@@ -1118,6 +1129,17 @@ func TestEncodeQuery(t *testing.T) {
11181129 }
11191130}
11201131
1132+ func BenchmarkEncodeQuery (b * testing.B ) {
1133+ for _ , tt := range encodeQueryTests {
1134+ b .Run (tt .expected , func (b * testing.B ) {
1135+ b .ReportAllocs ()
1136+ for b .Loop () {
1137+ tt .m .Encode ()
1138+ }
1139+ })
1140+ }
1141+ }
1142+
11211143var resolvePathTests = []struct {
11221144 base , ref , expected string
11231145}{
You can’t perform that action at this time.
0 commit comments