8000 Any interest in switching from net.IP to netip.Addr to encode IP addresses in Flows? · Issue #35 · ti-mo/conntrack · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Any interest in switching from net.IP to netip.Addr to encode IP addresses in Flows? #35
Closed
@antoninbas

Description

@antoninbas

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:

conntrack/tuple.go

Lines 117 to 128 in 7b6dc48

switch ipTupleType(ad.Type()) {
case ctaIPv4Src:
ipt.SourceAddress = net.IPv4(b[0], b[1], b[2], b[3])
case ctaIPv6Src:
ipt.SourceAddress = net.IP(b)
case ctaIPv4Dst:
ipt.DestinationAddress = net.IPv4(b[0], b[1], b[2], b[3])
case ctaIPv6Dst:
ipt.DestinationAddress = net.IP(b)
default:
return fmt.Errorf("child type %d: %w", ad.Type(), errUnknownAttribute)
}

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0