Skip to content

Commit 83b5352

Browse files
1911860538Jorropo
1911860538
authored andcommitted
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: 4c02e4c GitHub-Pull-Request: #73422 Reviewed-on: https://go-review.googlesource.com/c/go/+/666356 Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 8a85a2e commit 83b5352

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/net/http/transport.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,11 +2931,17 @@ func (pc *persistConn) closeLocked(err error) {
29312931
pc.mutateHeaderFunc = nil
29322932
}
29332933

2934-
var portMap = map[string]string{
2935-
"http": "80",
2936-
"https": "443",
2937-
"socks5": "1080",
2938-
"socks5h": "1080",
2934+
func schemePort(scheme string) string {
2935+
switch scheme {
2936+
case "http":
2937+
return "80"
2938+
case "https":
2939+
return "443"
2940+
case "socks5", "socks5h":
2941+
return "1080"
2942+
default:
2943+
return ""
2944+
}
29392945
}
29402946

29412947
func idnaASCIIFromURL(url *url.URL) string {
@@ -2950,7 +2956,7 @@ func idnaASCIIFromURL(url *url.URL) string {
29502956
func canonicalAddr(url *url.URL) string {
29512957
port := url.Port()
29522958
if port == "" {
2953-
port = portMap[url.Scheme]
2959+
port = schemePort(url.Scheme)
29542960
}
29552961
return net.JoinHostPort(idnaASCIIFromURL(url), port)
29562962
}

0 commit comments

Comments
 (0)