net/http: replace map lookup with switch for scheme port

Improve scheme port lookup by replacing map with switch, reducing overhead and improving performance.

Change-Id: I45c790da15e237d5f32c50d342b3713b98fd2ffa
GitHub-Last-Rev: 4c02e4cabf181b365fbf2b722e3051625a289527
GitHub-Pull-Request: golang/go#73422
Reviewed-on: https://go-review.googlesource.com/c/go/+/666356
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
1911860538 2025-04-17 14:51:18 +00:00 committed by Jorropo
parent 8a85a2e70a
commit 83b53527fa

View File

@ -2931,11 +2931,17 @@ func (pc *persistConn) closeLocked(err error) {
pc.mutateHeaderFunc = nil
}
var portMap = map[string]string{
"http": "80",
"https": "443",
"socks5": "1080",
"socks5h": "1080",
func schemePort(scheme string) string {
switch scheme {
case "http":
return "80"
case "https":
return "443"
case "socks5", "socks5h":
return "1080"
default:
return ""
}
}
func idnaASCIIFromURL(url *url.URL) string {
@ -2950,7 +2956,7 @@ func idnaASCIIFromURL(url *url.URL) string {
func canonicalAddr(url *url.URL) string {
port := url.Port()
if port == "" {
port = portMap[url.Scheme]
port = schemePort(url.Scheme)
}
return net.JoinHostPort(idnaASCIIFromURL(url), port)
}