1
0
mirror of https://github.com/golang/go synced 2024-09-28 20:14:28 -06:00

net/netip: optimize AddrPort.String for IPv4 addresses

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 <gobot@golang.org>
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>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
Tobias Klauser 2023-11-14 22:39:41 +01:00 committed by Gopher Robot
parent 128d96b3ca
commit 1cecbb38f4

View File

@ -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: