net/netip: optimize Addr.String for 4in6 addresses

name                      old time/op    new time/op    delta
AddrString/v4-8             20.0ns ± 0%    19.7ns ± 1%   -1.42%  (p=0.000 n=9+10)
AddrString/v6-8             58.9ns ± 6%    57.1ns ± 0%   -3.08%  (p=0.000 n=10+9)
AddrString/v6_ellipsis-8    59.9ns ± 1%    59.5ns ± 0%   -0.53%  (p=0.027 n=8+10)
AddrString/v6_v4-8          33.7ns ± 1%    24.3ns ± 0%  -27.82%  (p=0.000 n=9+10)
AddrString/v6_zone-8        61.4ns ± 1%    61.6ns ± 0%     ~     (p=0.190 n=9+9)

name                      old alloc/op   new alloc/op   delta
AddrString/v4-8              16.0B ± 0%     16.0B ± 0%     ~     (all equal)
AddrString/v6-8              48.0B ± 0%     48.0B ± 0%     ~     (all equal)
AddrString/v6_ellipsis-8     24.0B ± 0%     24.0B ± 0%     ~     (all equal)
AddrString/v6_v4-8           24.0B ± 0%     24.0B ± 0%     ~     (all equal)
AddrString/v6_zone-8         24.0B ± 0%     24.0B ± 0%     ~     (all equal)

name                      old allocs/op  new allocs/op  delta
AddrString/v4-8               1.00 ± 0%      1.00 ± 0%     ~     (all equal)
AddrString/v6-8               1.00 ± 0%      1.00 ± 0%     ~     (all equal)
AddrString/v6_ellipsis-8      1.00 ± 0%      1.00 ± 0%     ~     (all equal)
AddrString/v6_v4-8            1.00 ± 0%      1.00 ± 0%     ~     (all equal)
AddrString/v6_zone-8          1.00 ± 0%      1.00 ± 0%     ~     (all equal)

Change-Id: Ie611ee8629a2ff457dcff83b08d0c94c93af3182
Reviewed-on: https://go-review.googlesource.com/c/go/+/557777
Reviewed-by: Damien Neil <dneil@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: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Tobias Klauser 2024-01-24 22:15:15 +01:00 committed by Gopher Robot
parent 700edbf73e
commit 4f4d6508b9
2 changed files with 9 additions and 6 deletions

View File

@ -756,11 +756,7 @@ func (ip Addr) String() string {
return ip.string4()
default:
if ip.Is4In6() {
if z := ip.Zone(); z != "" {
return "::ffff:" + ip.Unmap().string4() + "%" + z
} else {
return "::ffff:" + ip.Unmap().string4()
}
return ip.string4In6()
}
return ip.string6()
}
@ -847,6 +843,13 @@ func (ip Addr) appendTo4(ret []byte) []byte {
return ret
}
func (ip Addr) string4In6() string {
const max = len("::ffff:255.255.255.255%enp5s0")
ret := make([]byte, 0, max)
ret = ip.appendTo4In6(ret)
return string(ret)
}
func (ip Addr) appendTo4In6(ret []byte) []byte {
ret = append(ret, "::ffff:"...)
ret = ip.Unmap().appendTo4(ret)

View File

@ -1691,7 +1691,7 @@ func BenchmarkStdParseIP(b *testing.B) {
}
}
func BenchmarkIPString(b *testing.B) {
func BenchmarkAddrString(b *testing.B) {
for _, test := range parseBenchInputs {
ip := MustParseAddr(test.ip)
b.Run(test.name, func(b *testing.B) {