From 1cecbb38f477a94938a27936d717f2e6f048623e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 14 Nov 2023 22:39:41 +0100 Subject: [PATCH] net/netip: optimize AddrPort.String for IPv4 addresses MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Use Addr.appendTo4 like in Addr.String. name old time/op new time/op delta AddrPortString/v4-8 47.5ns ± 0% 27.7ns ± 1% -41.81% (p=0.000 n=8+10) AddrPortString/v6-8 95.2ns ± 0% 94.9ns ± 1% -0.35% (p=0.034 n=10+10) AddrPortString/v6_ellipsis-8 96.8ns ± 0% 96.6ns ± 0% -0.15% (p=0.008 n=9+9) AddrPortString/v6_v4-8 70.9ns ± 0% 70.9ns ± 0% ~ (p=0.425 n=10+10) AddrPortString/v6_zone-8 97.0ns ± 0% 97.0ns ± 0% ~ (p=0.838 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 96.0B ± 0% 96.0B ± 0% ~ (all equal) AddrPortString/v6_ellipsis-8 56.0B ± 0% 56.0B ± 0% ~ (all equal) AddrPortString/v6_v4-8 56.0B ± 0% 56.0B ± 0% ~ (all equal) AddrPortString/v6_zone-8 56.0B ± 0% 56.0B ± 0% ~ (all equal) name old allocs/op new allocs/op delta AddrPortString/v4-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) AddrPortString/v6-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) AddrPortString/v6_ellipsis-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) AddrPortString/v6_v4-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) AddrPortString/v6_zone-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) Change-Id: I3d3fcee807ca33d1e8d6dafb03ab844ea0c76bea Reviewed-on: https://go-review.googlesource.com/c/go/+/542396 TryBot-Result: Gopher Robot Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI Auto-Submit: Tobias Klauser Reviewed-by: Damien Neil Run-TryBot: Tobias Klauser --- src/net/netip/netip.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/net/netip/netip.go b/src/net/netip/netip.go index a6bd4dedec..1d20a4aa7f 100644 --- a/src/net/netip/netip.go +++ b/src/net/netip/netip.go @@ -1118,12 +1118,10 @@ func (p AddrPort) String() string { case z0: return "invalid AddrPort" case z4: - a := p.ip.As4() - buf := make([]byte, 0, 21) - for i := range a { - buf = strconv.AppendUint(buf, uint64(a[i]), 10) - buf = append(buf, "...:"[i]) - } + const max = len("255.255.255.255:65535") + buf := make([]byte, 0, max) + buf = p.ip.appendTo4(buf) + buf = append(buf, ':') buf = strconv.AppendUint(buf, uint64(p.port), 10) return string(buf) default: -- 2.50.0