[go] net/url: reduce allocs in Encode

3 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Oct 13, 2025, 11:13:49 AM (3 days ago) Oct 13
to goph...@pubsubhelper.golang.org, jub0bs, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

net/url: reduce allocs in Encode

This change adds benchmarks for Encode and reverts what CL 617356 did in
this package. In this case, pre-sizing the slice in which we collect the
keys and then sorting it is indeed more performant than relying on a
combination of maps.Keys and slices.Sorted.

Here are some benchmark results:

goos: darwin
goarch: amd64
pkg: net/url
cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
│ old │ new │
│ sec/op │ sec/op vs base │
EncodeQuery-8 1.719µ ± 1% 1.055µ ± 0% -38.63% (p=0.000 n=20)

│ old │ new │
│ B/op │ B/op vs base │
EncodeQuery-8 576.0 ± 0% 256.0 ± 0% -55.56% (p=0.000 n=20)

│ old │ new │
│ allocs/op │ allocs/op vs base │
EncodeQuery-8 25.00 ± 0% 11.00 ± 0% -56.00% (p=0.000 n=20)
Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
GitHub-Last-Rev: 38a1551bf5acdf7214c156272d5d9debdb62d969
GitHub-Pull-Request: golang/go#75874

Change diff

diff --git a/src/net/url/url.go b/src/net/url/url.go
index a697548..e96542d 100644
--- a/src/net/url/url.go
+++ b/src/net/url/url.go
@@ -15,7 +15,6 @@
import (
"errors"
"fmt"
- "maps"
"net/netip"
"path"
"slices"
@@ -1048,7 +1047,14 @@
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 {
diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go
index a7543d6..bb9811d 100644
--- a/src/net/url/url_test.go
+++ b/src/net/url/url_test.go
@@ -1101,6 +1101,17 @@
}
}

+func BenchmarkEncodeQuery(b *testing.B) {
+ b.ReportAllocs()
+ for range b.N {
+ for _, tt := range encodeQueryTests {
+ strSink = tt.m.Encode()
+ }
+ }
+}
+
+var strSink string
+
var resolvePathTests = []struct {
base, ref, expected string
}{

Change information

Files:
  • M src/net/url/url.go
  • M src/net/url/url_test.go
Change size: S
Delta: 2 files changed, 19 insertions(+), 2 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
Gerrit-Change-Number: 711280
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Oct 13, 2025, 11:13:51 AM (3 days ago) Oct 13
to jub0bs, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems with your PR:

  1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?

Please address any problems by updating the GitHub PR.

When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

For more details, see:

(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
    Gerrit-Change-Number: 711280
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
    Gerrit-Comment-Date: Mon, 13 Oct 2025 15:13:46 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    jub0bs (Gerrit)

    unread,
    Oct 13, 2025, 11:15:08 AM (3 days ago) Oct 13
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com

    jub0bs added 1 comment

    Patchset-level comments
    Gopher Robot . resolved

    I spotted some possible problems with your PR:

      1. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?

    Please address any problems by updating the GitHub PR.

    When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

    To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

    For more details, see:

    (In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

    jub0bs

    Acknowledged

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
      Gerrit-Change-Number: 711280
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
      Gerrit-Comment-Date: Mon, 13 Oct 2025 15:15:00 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Gopher Robot <go...@golang.org>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Jes Cok (Gerrit)

      unread,
      Oct 13, 2025, 11:28:00 AM (3 days ago) Oct 13
      to jub0bs, Gerrit Bot, goph...@pubsubhelper.golang.org, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Damien Neil and Russ Cox

      Jes Cok added 1 comment

      File src/net/url/url.go
      Line 1050, Patchset 1 (Latest): // To minimize allocations, instead of using slices.Sorted(maps.Keys(v)),
      Jes Cok . unresolved

      Looks like it's reverting https://go-review.googlesource.com/c/go/+/617356/3/src/net/url/url.go#b1007.

      Just curious once it's submitted for performance, should the other files also be reverted?

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Damien Neil
      • Russ Cox
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
        Gerrit-Change-Number: 711280
        Gerrit-PatchSet: 1
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Jes Cok <xigua...@gmail.com>
        Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
        Gerrit-Attention: Russ Cox <r...@golang.org>
        Gerrit-Attention: Damien Neil <dn...@google.com>
        Gerrit-Comment-Date: Mon, 13 Oct 2025 15:27:52 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        unsatisfied_requirement
        open
        diffy

        jub0bs (Gerrit)

        unread,
        Oct 13, 2025, 11:44:26 AM (3 days ago) Oct 13
        to Gerrit Bot, goph...@pubsubhelper.golang.org, Jes Cok, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Damien Neil and Russ Cox

        jub0bs added 1 comment

        File src/net/url/url.go
        Line 1050, Patchset 1 (Latest): // To minimize allocations, instead of using slices.Sorted(maps.Keys(v)),
        Jes Cok . unresolved

        Looks like it's reverting https://go-review.googlesource.com/c/go/+/617356/3/src/net/url/url.go#b1007.

        Just curious once it's submitted for performance, should the other files also be reverted?

        jub0bs

        I must admit I was focused on net/url and I haven't studied the rest of CL 617356 yet. I do suspect that some of those other changes should also be reverted. Hmmm... Give me some time; I might broaden the scope of this CL to revert what should be.

        Gerrit-Comment-Date: Mon, 13 Oct 2025 15:44:18 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        Comment-In-Reply-To: Jes Cok <xigua...@gmail.com>
        unsatisfied_requirement
        open
        diffy

        Sean Liao (Gerrit)

        unread,
        Oct 13, 2025, 1:22:51 PM (2 days ago) Oct 13
        to jub0bs, Gerrit Bot, goph...@pubsubhelper.golang.org, Jes Cok, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from jub0bs

        Sean Liao added 1 comment

        File src/net/url/url_test.go
        Line 1108, Patchset 1 (Latest): strSink = tt.m.Encode()
        Sean Liao . unresolved

        do you need the sink if you switch to b.Loop() ?

        Open in Gerrit

        Related details

        Attention is currently required from:
        • jub0bs
        Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
        Gerrit-Change-Number: 711280
        Gerrit-PatchSet: 1
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Jes Cok <xigua...@gmail.com>
        Gerrit-CC: Sean Liao <se...@liao.dev>
        Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
        Gerrit-Attention: jub0bs <jub0bsin...@gmail.com>
        Gerrit-Comment-Date: Mon, 13 Oct 2025 17:22:43 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        unsatisfied_requirement
        open
        diffy

        jub0bs (Gerrit)

        unread,
        Oct 13, 2025, 2:33:24 PM (2 days ago) Oct 13
        to Gerrit Bot, goph...@pubsubhelper.golang.org, Jes Cok, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Sean Liao

        jub0bs added 1 comment

        File src/net/url/url_test.go
        Line 1108, Patchset 1 (Latest): strSink = tt.m.Encode()
        Sean Liao . resolved

        do you need the sink if you switch to b.Loop() ?

        jub0bs

        I'm wary of relying on `b.Loop` before #73137 gets resolved. You're right in this case, though: switching to `b.Loop` doesn't seem detrimental. Thanks!

        Open in Gerrit

        Related details

        Attention is currently required from:
        • Sean Liao
        Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
        Gerrit-Change-Number: 711280
        Gerrit-PatchSet: 1
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Jes Cok <xigua...@gmail.com>
        Gerrit-CC: Sean Liao <se...@liao.dev>
        Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
        Gerrit-Attention: Sean Liao <se...@liao.dev>
        Gerrit-Comment-Date: Mon, 13 Oct 2025 18:33:16 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        Comment-In-Reply-To: Sean Liao <se...@liao.dev>
        unsatisfied_requirement
        open
        diffy

        Gerrit Bot (Gerrit)

        unread,
        Oct 13, 2025, 2:35:32 PM (2 days ago) Oct 13
        to jub0bs, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
        Attention needed from Damien Neil, Russ Cox and Sean Liao

        Gerrit Bot uploaded new patchset

        Gerrit Bot uploaded patch set #2 to this change.
        Open in Gerrit

        Related details

        Attention is currently required from:
        • Damien Neil
        • Russ Cox
        • Sean Liao
        Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: newpatchset
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
        Gerrit-Change-Number: 711280
        Gerrit-PatchSet: 2
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Jes Cok <xigua...@gmail.com>
        Gerrit-CC: Sean Liao <se...@liao.dev>
        Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
        Gerrit-Attention: Russ Cox <r...@golang.org>
        Gerrit-Attention: Sean Liao <se...@liao.dev>
        Gerrit-Attention: Damien Neil <dn...@google.com>
        unsatisfied_requirement
        open
        diffy

        jub0bs (Gerrit)

        unread,
        Oct 14, 2025, 2:31:54 PM (yesterday) Oct 14
        to Gerrit Bot, goph...@pubsubhelper.golang.org, Jes Cok, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Damien Neil and Russ Cox

        jub0bs added 1 comment

        File src/net/url/url.go
        Line 1050, Patchset 1: // To minimize allocations, instead of using slices.Sorted(maps.Keys(v)),
        Jes Cok . unresolved

        Looks like it's reverting https://go-review.googlesource.com/c/go/+/617356/3/src/net/url/url.go#b1007.

        Just curious once it's submitted for performance, should the other files also be reverted?

        jub0bs

        I must admit I was focused on net/url and I haven't studied the rest of CL 617356 yet. I do suspect that some of those other changes should also be reverted. Hmmm... Give me some time; I might broaden the scope of this CL to revert what should be.

        jub0bs

        IMO, all but one of the instances of `slices.Sorted(maps.Keys(...))` introduced by CL 617356 should be reverted. The one exception is in a test, where readability matters more than performance.

        However, new instances of `slices.Sorted(maps.Keys(...))` have since crept in the code base, e.g. in [net/http/server.go](https://cs.opensource.google/go/go/+/refs/tags/go1.25.3:src/net/http/server.go;l=2842).

        I'm not sure what the best course of action is. Perhaps this CL should only revert the instances added by CL 617356 and let the owners of the other affected packages decide what they want to do. What do you think?

        Open in Gerrit

        Related details

        Attention is currently required from:
        • Damien Neil
        • Russ Cox
        Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
        Gerrit-Change-Number: 711280
        Gerrit-PatchSet: 2
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Jes Cok <xigua...@gmail.com>
        Gerrit-CC: Sean Liao <se...@liao.dev>
        Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
        Gerrit-Attention: Russ Cox <r...@golang.org>
        Gerrit-Attention: Damien Neil <dn...@google.com>
        Gerrit-Comment-Date: Tue, 14 Oct 2025 18:31:45 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        Comment-In-Reply-To: jub0bs <jub0bsin...@gmail.com>
        Comment-In-Reply-To: Jes Cok <xigua...@gmail.com>
        unsatisfied_requirement
        open
        diffy

        Sean Liao (Gerrit)

        unread,
        Oct 14, 2025, 2:42:30 PM (yesterday) Oct 14
        to jub0bs, Gerrit Bot, goph...@pubsubhelper.golang.org, Jes Cok, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Damien Neil and Russ Cox

        Sean Liao added 1 comment

        File src/net/url/url.go
        Line 1050, Patchset 1: // To minimize allocations, instead of using slices.Sorted(maps.Keys(v)),
        Jes Cok . unresolved

        Looks like it's reverting https://go-review.googlesource.com/c/go/+/617356/3/src/net/url/url.go#b1007.

        Just curious once it's submitted for performance, should the other files also be reverted?

        jub0bs

        I must admit I was focused on net/url and I haven't studied the rest of CL 617356 yet. I do suspect that some of those other changes should also be reverted. Hmmm... Give me some time; I might broaden the scope of this CL to revert what should be.

        jub0bs

        IMO, all but one of the instances of `slices.Sorted(maps.Keys(...))` introduced by CL 617356 should be reverted. The one exception is in a test, where readability matters more than performance.

        However, new instances of `slices.Sorted(maps.Keys(...))` have since crept in the code base, e.g. in [net/http/server.go](https://cs.opensource.google/go/go/+/refs/tags/go1.25.3:src/net/http/server.go;l=2842).

        I'm not sure what the best course of action is. Perhaps this CL should only revert the instances added by CL 617356 and let the owners of the other affected packages decide what they want to do. What do you think?

        Sean Liao

        I think they can all stay, they're not particularly performance critical code or executed a lot of times in a regular program.

        Gerrit-Comment-Date: Tue, 14 Oct 2025 18:42:22 +0000
        unsatisfied_requirement
        open
        diffy

        jub0bs (Gerrit)

        unread,
        3:27 PM (8 hours ago) 3:27 PM
        to Gerrit Bot, goph...@pubsubhelper.golang.org, Jes Cok, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Damien Neil and Russ Cox

        jub0bs added 1 comment

        File src/net/url/url.go
        Line 1050, Patchset 1: // To minimize allocations, instead of using slices.Sorted(maps.Keys(v)),
        Jes Cok . unresolved

        Looks like it's reverting https://go-review.googlesource.com/c/go/+/617356/3/src/net/url/url.go#b1007.

        Just curious once it's submitted for performance, should the other files also be reverted?

        jub0bs

        I must admit I was focused on net/url and I haven't studied the rest of CL 617356 yet. I do suspect that some of those other changes should also be reverted. Hmmm... Give me some time; I might broaden the scope of this CL to revert what should be.

        jub0bs

        IMO, all but one of the instances of `slices.Sorted(maps.Keys(...))` introduced by CL 617356 should be reverted. The one exception is in a test, where readability matters more than performance.

        However, new instances of `slices.Sorted(maps.Keys(...))` have since crept in the code base, e.g. in [net/http/server.go](https://cs.opensource.google/go/go/+/refs/tags/go1.25.3:src/net/http/server.go;l=2842).

        I'm not sure what the best course of action is. Perhaps this CL should only revert the instances added by CL 617356 and let the owners of the other affected packages decide what they want to do. What do you think?

        Sean Liao

        I think they can all stay, they're not particularly performance critical code or executed a lot of times in a regular program.

        jub0bs

        I think they can all stay [...]

        Even the one in the `Encode` method? That method tends to be invoked on the server side, to modify/tidy a query string. See net/http/httputil.ReverseProxy, for instance.

        Gerrit-Comment-Date: Wed, 15 Oct 2025 19:26:57 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        Comment-In-Reply-To: Sean Liao <se...@liao.dev>
        unsatisfied_requirement
        open
        diffy

        Sean Liao (Gerrit)

        unread,
        3:58 PM (8 hours ago) 3:58 PM
        to jub0bs, Gerrit Bot, goph...@pubsubhelper.golang.org, Jes Cok, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Damien Neil and Russ Cox

        Sean Liao added 1 comment

        File src/net/url/url.go
        Line 1050, Patchset 1: // To minimize allocations, instead of using slices.Sorted(maps.Keys(v)),
        Jes Cok . unresolved

        Looks like it's reverting https://go-review.googlesource.com/c/go/+/617356/3/src/net/url/url.go#b1007.

        Just curious once it's submitted for performance, should the other files also be reverted?

        jub0bs

        I must admit I was focused on net/url and I haven't studied the rest of CL 617356 yet. I do suspect that some of those other changes should also be reverted. Hmmm... Give me some time; I might broaden the scope of this CL to revert what should be.

        jub0bs

        IMO, all but one of the instances of `slices.Sorted(maps.Keys(...))` introduced by CL 617356 should be reverted. The one exception is in a test, where readability matters more than performance.

        However, new instances of `slices.Sorted(maps.Keys(...))` have since crept in the code base, e.g. in [net/http/server.go](https://cs.opensource.google/go/go/+/refs/tags/go1.25.3:src/net/http/server.go;l=2842).

        I'm not sure what the best course of action is. Perhaps this CL should only revert the instances added by CL 617356 and let the owners of the other affected packages decide what they want to do. What do you think?

        Sean Liao

        I think they can all stay, they're not particularly performance critical code or executed a lot of times in a regular program.

        jub0bs

        I think they can all stay [...]

        Even the one in the `Encode` method? That method tends to be invoked on the server side, to modify/tidy a query string. See net/http/httputil.ReverseProxy, for instance.

        Sean Liao

        Sorry, I meant so the others, and we merge this CL.

        Gerrit-Comment-Date: Wed, 15 Oct 2025 19:58:15 +0000
        unsatisfied_requirement
        open
        diffy

        t hepudds (Gerrit)

        unread,
        4:56 PM (7 hours ago) 4:56 PM
        to jub0bs, Gerrit Bot, goph...@pubsubhelper.golang.org, Jes Cok, Damien Neil, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Damien Neil and Russ Cox

        t hepudds added 1 comment

        File src/net/url/url_test.go
        Line 1094, Patchset 2 (Latest):}
        t hepudds . unresolved

        For showing the benefit of pre-sizing, probably at least one of these should have a larger element count -- say maybe at least 10.

        I think the current results in the CL commit message are probably not showing the benefit of pre-sizing, or at least, in a quick test, I seem to be able to match those allocation count results without any pre-sizing by instead just manually inlining slices.Sorted:

        ```diff
        diff --git a/src/net/url/url.go b/src/net/url/url.go
        index a697548801..a343af3b33 100644
        --- a/src/net/url/url.go
        +++ b/src/net/url/url.go
        @@ -1048,7 +1048,9 @@ func (v Values) Encode() string {

        return ""
        }
        var buf strings.Builder
        - for _, k := range slices.Sorted(maps.Keys(v)) {
        +       keys := slices.Collect(maps.Keys(v))                                                                                

        + slices.Sort(keys)
        + for _, k := range keys {
        vs := v[k]
        keyEscaped := QueryEscape(k)
        for _, v := range vs {
        ```

        That said, just a quick test, so maybe I made a mistake, though I think it would be good to have at least one larger input in the benchmark.

        Separately, I suspect there's some room for improvement on the compiler side for how it is treating the original code. I'll likely file a separate issue after I double-check a bit more.

        Gerrit-CC: t hepudds <thepud...@gmail.com>
        Gerrit-Attention: Russ Cox <r...@golang.org>
        Gerrit-Attention: Damien Neil <dn...@google.com>
        Gerrit-Comment-Date: Wed, 15 Oct 2025 20:56:11 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        unsatisfied_requirement
        open
        diffy

        Damien Neil (Gerrit)

        unread,
        7:54 PM (4 hours ago) 7:54 PM
        to jub0bs, Gerrit Bot, goph...@pubsubhelper.golang.org, t hepudds, Jes Cok, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Russ Cox

        Damien Neil voted and added 1 comment

        Votes added by Damien Neil

        Code-Review+2

        1 comment

        Patchset-level comments
        File-level comment, Patchset 2 (Latest):
        Damien Neil . resolved

        LGTM as-is, will wait until tomorrow before submitting in case you want to add a test case with more keys.

        Open in Gerrit

        Related details

        Attention is currently required from:
        • Russ Cox
        Submit Requirements:
        • requirement satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ia0d7579f90434f0546d93b680ab18b47a1ffbdac
        Gerrit-Change-Number: 711280
        Gerrit-PatchSet: 2
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Damien Neil <dn...@google.com>
        Gerrit-Reviewer: Russ Cox <r...@golang.org>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Jes Cok <xigua...@gmail.com>
        Gerrit-CC: Sean Liao <se...@liao.dev>
        Gerrit-CC: jub0bs <jub0bsin...@gmail.com>
        Gerrit-CC: t hepudds <thepud...@gmail.com>
        Gerrit-Attention: Russ Cox <r...@golang.org>
        Gerrit-Comment-Date: Wed, 15 Oct 2025 23:54:16 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: Yes
        satisfied_requirement
        unsatisfied_requirement
        open
        diffy
        Reply all
        Reply to author
        Forward
        0 new messages