Description
The netip
package was introduced in Go 1.18. Compared to the existing net.IP
type, the netip.Addr
type takes less memory, is immutable, and is comparable so it supports == and can be used as a map key.
// Sizes: (64-bit)
// net.IP: 24 byte slice header + {4, 16} = 28 to 40 bytes
// net.IPAddr: 40 byte slice header + {4, 16} = 44 to 56 bytes + zone length
// netip.Addr: 24 bytes (zone is per-name singleton, shared across all users)
The Flow
type for this library contains 3 fields of type Tuple
, each with 2 IP addresses, that's 6 IP addresses in total. For IPv6 connections, memory usage (when considering IP addresses only, not other fields), could go down from 40*6 = 240B
to 24*6 = 144B
. And in practice, for IPv4 addresses, we would see the same benefit, due to a bug in the unmarshalling code:
Lines 117 to 128 in 7b6dc48
Calling net.IPv4(...)
creates a 16B slice (+ 24B slice header): https://cs.opensource.google/go/go/+/refs/tags/go1.21.1:src/net/ip.go;l=50-53
One thing to keep in mind is that it would be a non-backward compatible change, but:
- there is still no v1 major release for this library
- other popular low-level Go networking libraries have made this change in similar circumstances, e.g., https://github.com/mdlayher/ndp/releases/tag/v0.9.0
This is something I am happy to contribute. It will enable programs consuming this library to reduce their memory footprint, as well as start using the netip package more easily.