From: Tobias Klauser Date: Wed, 24 Jan 2024 21:14:49 +0000 (+0100) Subject: net/netip: optimize Addr.MarshalText allocation for 4in6 addresses X-Git-Tag: go1.23rc1~1388 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=700edbf73e16e2c55d59a917233662bad88623bc;p=gostls13.git 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 Auto-Submit: Tobias Klauser Reviewed-by: Damien Neil Reviewed-by: Cherry Mui --- diff --git a/src/net/netip/netip.go b/src/net/netip/netip.go index 156f01e11e..e566a4861f 100644 --- a/src/net/netip/netip.go +++ b/src/net/netip/netip.go @@ -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. diff --git a/src/net/netip/netip_test.go b/src/net/netip/netip_test.go index a748ac34f1..d80582139e 100644 --- a/src/net/netip/netip_test.go +++ b/src/net/netip/netip_test.go @@ -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() + } + }) } }