]> Cypherpunks repositories - gostls13.git/commitdiff
net/netip: optimize AddrPort.String for IPv6 addresses
authorTobias Klauser <tklauser@distanz.ch>
Tue, 14 Nov 2023 21:39:13 +0000 (22:39 +0100)
committerGopher Robot <gobot@golang.org>
Fri, 17 Nov 2023 16:45:56 +0000 (16:45 +0000)
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 <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
src/net/netip/netip.go
src/net/netip/netip_test.go

index 46f466c076b3414d86a875bc9fbefb6daa9857e7..a6bd4dedec132b38e1d0dd50f5b27844cbd4b86c 100644 (file)
@@ -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.
index 36e57ce17147faad141f0ef28a495c68a05d649a..a748ac34f13ccb9ce338425a99af0c0c43404bc3 100644 (file)
@@ -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"},