From: Tobias Klauser Date: Tue, 14 Nov 2023 21:39:13 +0000 (+0100) Subject: net/netip: optimize AddrPort.String for IPv6 addresses X-Git-Tag: go1.22rc1~274 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=128d96b3ca9c44bca5ab9bb1da8133aea18f74f5;p=gostls13.git net/netip: optimize AddrPort.String for IPv6 addresses Inline the IPv6-case of joinHostPort and avoid the check for a colon in the address. The address is already known to be an IPv6 address. Also use iota.Uitoa to convert the uin16 port. name old time/op new time/op delta AddrPortString/v4-8 45.7ns ± 0% 46.6ns ± 1% +1.97% (p=0.000 n=10+10) AddrPortString/v6-8 106ns ± 3% 95ns ± 0% -10.79% (p=0.000 n=9+10) AddrPortString/v6_ellipsis-8 110ns ± 0% 96ns ± 0% -12.37% (p=0.000 n=9+10) AddrPortString/v6_v4-8 85.1ns ± 0% 70.8ns ± 0% -16.80% (p=0.000 n=9+10) AddrPortString/v6_zone-8 110ns ± 0% 97ns ± 0% -12.29% (p=0.000 n=10+10) name old alloc/op new alloc/op delta AddrPortString/v4-8 24.0B ± 0% 24.0B ± 0% ~ (all equal) AddrPortString/v6-8 101B ± 0% 96B ± 0% -4.95% (p=0.000 n=10+10) AddrPortString/v6_ellipsis-8 61.0B ± 0% 56.0B ± 0% -8.20% (p=0.000 n=10+10) AddrPortString/v6_v4-8 61.0B ± 0% 56.0B ± 0% -8.20% (p=0.000 n=10+10) AddrPortString/v6_zone-8 61.0B ± 0% 56.0B ± 0% -8.20% (p=0.000 n=10+10) name old allocs/op new allocs/op delta AddrPortString/v4-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) AddrPortString/v6-8 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.000 n=10+10) AddrPortString/v6_ellipsis-8 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.000 n=10+10) AddrPortString/v6_v4-8 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.000 n=10+10) AddrPortString/v6_zone-8 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.000 n=10+10) Change-Id: I96a7f01c4e33bfa157a98558c62e2e0a8147726d Reviewed-on: https://go-review.googlesource.com/c/go/+/542395 Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI Auto-Submit: Tobias Klauser Reviewed-by: Damien Neil --- diff --git a/src/net/netip/netip.go b/src/net/netip/netip.go index 46f466c076..a6bd4dedec 100644 --- a/src/net/netip/netip.go +++ b/src/net/netip/netip.go @@ -1128,19 +1128,10 @@ func (p AddrPort) String() string { return string(buf) default: // TODO: this could be more efficient allocation-wise: - return joinHostPort(p.ip.String(), itoa.Itoa(int(p.port))) + return "[" + p.ip.String() + "]:" + itoa.Uitoa(uint(p.port)) } } -func joinHostPort(host, port string) string { - // We assume that host is a literal IPv6 address if host has - // colons. - if bytealg.IndexByteString(host, ':') >= 0 { - return "[" + host + "]:" + port - } - return host + ":" + port -} - // AppendTo appends a text encoding of p, // as generated by MarshalText, // to b and returns the extended buffer. diff --git a/src/net/netip/netip_test.go b/src/net/netip/netip_test.go index 36e57ce171..a748ac34f1 100644 --- a/src/net/netip/netip_test.go +++ b/src/net/netip/netip_test.go @@ -390,6 +390,7 @@ func TestAddrPortMarshalTextString(t *testing.T) { want string }{ {mustIPPort("1.2.3.4:80"), "1.2.3.4:80"}, + {mustIPPort("[::]:80"), "[::]:80"}, {mustIPPort("[1::CAFE]:80"), "[1::cafe]:80"}, {mustIPPort("[1::CAFE%en0]:80"), "[1::cafe%en0]:80"}, {mustIPPort("[::FFFF:192.168.140.255]:80"), "[::ffff:192.168.140.255]:80"},