Skip to content

net: make SplitHostPort alloc-free in more cases#78398

Closed
jub0bs wants to merge 1 commit intogolang:masterfrom
jub0bs:optim-net-splithostport
Closed

net: make SplitHostPort alloc-free in more cases#78398
jub0bs wants to merge 1 commit intogolang:masterfrom
jub0bs:optim-net-splithostport

Conversation

@jub0bs
Copy link
Copy Markdown
Contributor

@jub0bs jub0bs commented Mar 27, 2026

Because SplitHostPort is not inlineable, its error result cannot benefit
from mid-stack inlining and must be moved to the heap. However, callers
of SplitHostPort that only use its error result in nil checks are pretty
typical, and their performance needlessly suffers from the resulting
allocation.

Drawing inspiration from CL 734440, this CL refactors SplitHostPort to
an inlineable wrapper around a function that returns, not an error, but
the concrete type of SplitHostPort's error result. Consequently, that
error result can stay on the stack in cases where callers only use it in
nil checks, and SplitHostPort is now allocation-free for such callers.

To clarify: the goal of this CL is to make SplitHostPort
allocation-free, not simply in non-error cases, but also in error cases
in which the error result is only used in nil checks.

@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: ea71385) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/760021.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@jub0bs jub0bs force-pushed the optim-net-splithostport branch from ea71385 to aca3ddc Compare March 27, 2026 09:47
@gopherbot
Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/760021.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from jub0bs:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/760021.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: aca3ddc) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/760021.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Damien Neil:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/760021.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from jub0bs:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/760021.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from Damien Neil:

Patch Set 2:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/760021.
After addressing review feedback, remember to publish your drafts!

Because SplitHostPort is not inlineable, its error result cannot benefit
from mid-stack inlining and must be moved to the heap. However, callers
of SplitHostPort that only use its error result in nil checks are pretty
typical, and their performance needlessly suffers from the resulting
allocation.

Drawing inspiration from CL 734440, this CL refactors SplitHostPort to
an inlineable wrapper around a function that returns, not an error, but
the concrete type of SplitHostPort's error result. Consequently, that
error result can stay on the stack in cases where callers only use it in
nil checks, and SplitHostPort is now allocation-free for such callers.

To clarify: the goal of this CL is to make SplitHostPort
allocation-free, not simply in non-error cases, but also in error cases
in which the error result is only used in nil checks.
@jub0bs jub0bs force-pushed the optim-net-splithostport branch from aca3ddc to ca9b9c9 Compare March 29, 2026 08:00
@gopherbot
Copy link
Copy Markdown
Contributor

This PR (HEAD: ca9b9c9) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/760021.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Copy Markdown
Contributor

Message from jub0bs:

Patch Set 2:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/760021.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Copy Markdown
Contributor

Message from jub0bs:

Patch Set 3:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/760021.
After addressing review feedback, remember to publish your drafts!

@jub0bs jub0bs changed the title net: save an alloc for typical SplitHostPort calls net: make SplitHostPort alloc-free in more cases Mar 29, 2026
@jub0bs jub0bs closed this Mar 29, 2026
@gopherbot
Copy link
Copy Markdown
Contributor

Message from jub0bs:

Patch Set 3:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/760021.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants