From: Tobias Klauser Date: Wed, 24 Jan 2024 21:15:15 +0000 (+0100) Subject: net/netip: optimize Addr.String for 4in6 addresses X-Git-Tag: go1.23rc1~1387 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4f4d6508b9dd704c527b54063faee6d809e647cf;p=gostls13.git 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 LUCI-TryBot-Result: Go LUCI Auto-Submit: Tobias Klauser Reviewed-by: Cherry Mui --- diff --git a/src/net/netip/netip.go b/src/net/netip/netip.go index e566a4861f..5c0c7afd97 100644 --- a/src/net/netip/netip.go +++ b/src/net/netip/netip.go @@ -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) diff --git a/src/net/netip/netip_test.go b/src/net/netip/netip_test.go index d80582139e..c914c5f256 100644 --- a/src/net/netip/netip_test.go +++ b/src/net/netip/netip_test.go @@ -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) {