]> Cypherpunks repositories - gostls13.git/commitdiff
net/netip: optimize As4 and As16
authorJosh Bleecher Snyder <josharian@gmail.com>
Fri, 5 Nov 2021 17:07:11 +0000 (10:07 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Fri, 5 Nov 2021 17:23:06 +0000 (17:23 +0000)
name    old time/op  new time/op  delta
As16-8  2.88ns ± 3%  2.16ns ± 3%  -25.19%  (p=0.000 n=15+15)

Fixes #49379
Updates #20859

Change-Id: If4cf58d19ed0e2ac0f179da5c132ed37061e4cb7
Reviewed-on: https://go-review.googlesource.com/c/go/+/361674
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/netip/netip.go
src/net/netip/netip_test.go

index b0c13b81fc4881105e5efdfc51b8abfc56b631d3..8cde6ef3d20e85c2b318ced05c997b58c3969dfa 100644 (file)
@@ -698,21 +698,19 @@ const (
 // IPv6 addresses with zones are returned without their zone (use the
 // Zone method to get it).
 // The ip zero value returns all zeroes.
-func (ip Addr) As16() [16]byte {
-       var ret [16]byte
-       bePutUint64(ret[:8], ip.addr.hi)
-       bePutUint64(ret[8:], ip.addr.lo)
-       return ret
+func (ip Addr) As16() (a16 [16]byte) {
+       bePutUint64(a16[:8], ip.addr.hi)
+       bePutUint64(a16[8:], ip.addr.lo)
+       return a16
 }
 
 // As4 returns an IPv4 or IPv4-in-IPv6 address in its 4-byte representation.
 // If ip is the zero Addr or an IPv6 address, As4 panics.
 // Note that 0.0.0.0 is not the zero Addr.
-func (ip Addr) As4() [4]byte {
+func (ip Addr) As4() (a4 [4]byte) {
        if ip.z == z4 || ip.Is4In6() {
-               var ret [4]byte
-               bePutUint32(ret[:], uint32(ip.addr.lo))
-               return ret
+               bePutUint32(a4[:], uint32(ip.addr.lo))
+               return a4
        }
        if ip.z == z0 {
                panic("As4 called on IP zero value")
index 241a71bb83c190ba6a00259907018d2864239f19..c39b1ec201efeaa9752b3c546b1d28a031093944 100644 (file)
@@ -1806,3 +1806,12 @@ func TestInvalidAddrPortString(t *testing.T) {
                }
        }
 }
+
+var sink16 [16]byte
+
+func BenchmarkAs16(b *testing.B) {
+       addr := MustParseAddr("1::10")
+       for i := 0; i < b.N; i++ {
+               sink16 = addr.As16()
+       }
+}