net/netip: optimize Addr.MarshalText allocation for 4in6 addresses

name                           old time/op    new time/op    delta
AddrMarshalText/v4-8             18.5ns ± 1%    18.8ns ± 1%   +1.40%  (p=0.000 n=9+10)
AddrMarshalText/v6-8             58.0ns ± 0%    57.5ns ± 0%   -0.93%  (p=0.000 n=9+10)
AddrMarshalText/v6_ellipsis-8    61.4ns ± 0%    60.4ns ± 0%   -1.65%  (p=0.000 n=10+9)
AddrMarshalText/v6_v4-8          25.7ns ± 0%    26.2ns ± 1%   +1.86%  (p=0.000 n=10+10)
AddrMarshalText/v6_zone-8        61.8ns ± 0%    60.8ns ± 0%   -1.63%  (p=0.000 n=10+9)

name                           old alloc/op   new alloc/op   delta
AddrMarshalText/v4-8              16.0B ± 0%     16.0B ± 0%     ~     (all equal)
AddrMarshalText/v6-8              48.0B ± 0%     48.0B ± 0%     ~     (all equal)
AddrMarshalText/v6_ellipsis-8     48.0B ± 0%     48.0B ± 0%     ~     (all equal)
AddrMarshalText/v6_v4-8           48.0B ± 0%     32.0B ± 0%  -33.33%  (p=0.000 n=10+10)
AddrMarshalText/v6_zone-8         48.0B ± 0%     48.0B ± 0%     ~     (all equal)

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

Change-Id: Ib298a0fb9ec6b55e37d0559dad387242dc82aab9
Reviewed-on: https://go-review.googlesource.com/c/go/+/557776
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>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Tobias Klauser 2024-01-24 22:14:49 +01:00 committed by Gopher Robot
parent b495ddbe13
commit 700edbf73e
2 changed files with 13 additions and 8 deletions

View File

@ -949,14 +949,15 @@ func (ip Addr) MarshalText() ([]byte, error) {
b := make([]byte, 0, max)
return ip.appendTo4(b), nil
default:
max := len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0")
b := make([]byte, 0, max)
if ip.Is4In6() {
max := len("::ffff:255.255.255.255%enp5s0")
b := make([]byte, 0, max)
return ip.appendTo4In6(b), nil
}
max := len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0")
b := make([]byte, 0, max)
return ip.appendTo6(b), nil
}
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.

View File

@ -1715,11 +1715,15 @@ func BenchmarkIPStringExpanded(b *testing.B) {
}
}
func BenchmarkIPMarshalText(b *testing.B) {
b.ReportAllocs()
ip := MustParseAddr("66.55.44.33")
for i := 0; i < b.N; i++ {
sinkBytes, _ = ip.MarshalText()
func BenchmarkAddrMarshalText(b *testing.B) {
for _, test := range parseBenchInputs {
ip := MustParseAddr(test.ip)
b.Run(test.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
sinkBytes, _ = ip.MarshalText()
}
})
}
}