Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any interest in switching from net.IP to netip.Addr to encode IP addresses in Flows? #35

Closed
antoninbas opened this issue Sep 28, 2023 · 2 comments · Fixed by #36
Closed

Comments

@antoninbas
Copy link
Contributor

antoninbas commented Sep 28, 2023

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.

@ti-mo
Copy link
Owner

ti-mo commented Oct 4, 2023

@antoninbas I don't have much time to work on this library anymore and don't use it in any production code, but I'd be happy to take patches to switch to netip. It would be a breaking change, but one with a well-documented way forward, and something that needs to happen at some point regardless.

Happy someone is still using this! 🙂

antoninbas added a commit to antoninbas/conntrack that referenced this issue Oct 10, 2023
Using the net/netip package instead of the net package can help reduce
the memory footprint of the library and help reduce the number of
heap allocations.

This is a breaking change for consumers for the libray as exported types
are updated to use fields of type netip.Addr instead of net.IP.

Fixes ti-mo#35

Signed-off-by: Antonin Bas <abas@vmware.com>
@antoninbas
Copy link
Contributor Author

@ti-mo I have opened a PR for this proposal: #36

antoninbas added a commit to antoninbas/conntrack that referenced this issue Oct 12, 2023
Using the net/netip package instead of the net package can help reduce
the memory footprint of the library and help reduce the number of
heap allocations.

This is a breaking change for consumers for the libray as exported types
are updated to use fields of type netip.Addr instead of net.IP.

We also remove the depedency on go-cmp and use assert.Equal instead in
tests.

Fixes ti-mo#35

Signed-off-by: Antonin Bas <abas@vmware.com>
ti-mo pushed a commit to antoninbas/conntrack that referenced this issue Oct 16, 2023
Using the net/netip package instead of the net package can help reduce
the memory footprint of the library and help reduce the number of
heap allocations.

This is a breaking change for consumers of the library as exported types
are updated to use fields of type netip.Addr instead of net.IP.

We also remove the depedency on go-cmp and use assert.Equal instead in
tests.

Fixes ti-mo#35

Signed-off-by: Antonin Bas <abas@vmware.com>
@ti-mo ti-mo closed this as completed in #36 Oct 16, 2023
ti-mo pushed a commit that referenced this issue Oct 16, 2023
Using the net/netip package instead of the net package can help reduce
the memory footprint of the library and help reduce the number of
heap allocations.

This is a breaking change for consumers of the library as exported types
are updated to use fields of type netip.Addr instead of net.IP.

We also remove the depedency on go-cmp and use assert.Equal instead in
tests.

Fixes #35

Signed-off-by: Antonin Bas <abas@vmware.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants