]> Cypherpunks repositories - gostls13.git/commitdiff
net/netip: optimize Addr.MarshalText allocation for 4in6 addresses
authorTobias Klauser <tklauser@distanz.ch>
Wed, 24 Jan 2024 21:14:49 +0000 (22:14 +0100)
committerGopher Robot <gobot@golang.org>
Wed, 24 Jan 2024 22:27:15 +0000 (22:27 +0000)
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>
src/net/netip/netip.go
src/net/netip/netip_test.go

index 156f01e11e3a0691f2495e2749ff01b8edfb9688..e566a4861f2830d82a9b67fdd5a56c5e52b805c1 100644 (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.
index a748ac34f13ccb9ce338425a99af0c0c43404bc3..d80582139ee81f41fea7fb9c165d337aace60694 100644 (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()
+                       }
+               })
        }
 }