Skip to content

Commit

Permalink
net: optimize WriteToUDPAddrPort
Browse files Browse the repository at this point in the history
See the recent change optimizing WriteMsgUDPAddrPort
for an explanation of why this change includes copy/paste/modify
instead of implementing WriteToUDP using WriteToUDPAddrPort.

name                          old time/op    new time/op    delta
WriteToReadFromUDPAddrPort-8    5.02µs ± 3%    4.71µs ± 2%   -6.31%  (p=0.000 n=15+14)

name                          old alloc/op   new alloc/op   delta
WriteToReadFromUDPAddrPort-8     68.0B ± 0%      4.0B ± 0%  -94.12%  (p=0.000 n=15+15)

name                          old allocs/op  new allocs/op  delta
WriteToReadFromUDPAddrPort-8      3.00 ± 0%      1.00 ± 0%  -66.67%  (p=0.000 n=15+15)

Change-Id: I301715e774de07eb6ccb4e329ccf2e554609abc1
Reviewed-on: https://go-review.googlesource.com/c/go/+/360599
Trust: Josh Bleecher Snyder <[email protected]>
Run-TryBot: Josh Bleecher Snyder <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
josharian committed Nov 2, 2021
1 parent 0b37bd9 commit 433ba58
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/net/udpsock.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,14 @@ func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {

// WriteToUDPAddrPort acts like WriteTo but takes a netip.AddrPort.
func (c *UDPConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error) {
// TODO(bradfitz): make this efficient, making the internal net package
// type throughout be netip.Addr and only converting to the net.IP slice
// version at the edge. But for now (2021-10-20), this is a wrapper around
// the old way.
return c.WriteToUDP(b, UDPAddrFromAddrPort(addr))
if !c.ok() {
return 0, syscall.EINVAL
}
n, err := c.writeToAddrPort(b, addr)
if err != nil {
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addrPortUDPAddr{addr}, Err: err}
}
return n, err
}

// WriteTo implements the PacketConn WriteTo method.
Expand Down
4 changes: 4 additions & 0 deletions src/net/udpsock_plan9.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (c *UDPConn) writeTo(b []byte, addr *UDPAddr) (int, error) {
return len(b), nil
}

func (c *UDPConn) writeToAddrPort(b []byte, addr netip.AddrPort) (int, error) {
return c.writeTo(b, UDPAddrFromAddrPort(addr)) // TODO: optimize instead of allocating
}

func (c *UDPConn) writeMsg(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
return 0, 0, syscall.EPLAN9
}
Expand Down
26 changes: 26 additions & 0 deletions src/net/udpsock_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,32 @@ func (c *UDPConn) writeTo(b []byte, addr *UDPAddr) (int, error) {
}
}

func (c *UDPConn) writeToAddrPort(b []byte, addr netip.AddrPort) (int, error) {
if c.fd.isConnected {
return 0, ErrWriteToConnected
}
if !addr.IsValid() {
return 0, errMissingAddress
}

switch c.fd.family {
case syscall.AF_INET:
sa, err := addrPortToSockaddrInet4(addr)
if err != nil {
return 0, err
}
return c.fd.writeToInet4(b, sa)
case syscall.AF_INET6:
sa, err := addrPortToSockaddrInet6(addr)
if err != nil {
return 0, err
}
return c.fd.writeToInet6(b, sa)
default:
return 0, &AddrError{Err: "invalid address family", Addr: addr.Addr().String()}
}
}

func (c *UDPConn) writeMsg(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
if c.fd.isConnected && addr != nil {
return 0, 0, ErrWriteToConnected
Expand Down

0 comments on commit 433ba58

Please sign in to comment.