netlink, nltest: add ReceiveIter to stream responses#258
Merged
nickgarlis merged 1 commit intomdlayher:mainfrom Mar 22, 2026
Merged
netlink, nltest: add ReceiveIter to stream responses#258nickgarlis merged 1 commit intomdlayher:mainfrom
nickgarlis merged 1 commit intomdlayher:mainfrom
Conversation
nickgarlis
added a commit
to nickgarlis/nftables
that referenced
this pull request
Feb 25, 2026
Currently, listing many rules or set elements generates a lot of intermediate slices, which increases memory usage unnecessarily. This change unmarshals elements during iteration, avoiding these intermediate allocations. Benchmarks show improved performance, particularly when reading rules. Depends on: mdlayher/netlink#258
nickgarlis
added a commit
to nickgarlis/nftables
that referenced
this pull request
Feb 25, 2026
Currently, listing many rules or set elements generates a lot of intermediate slices, which increases memory usage unnecessarily. This change unmarshals elements during iteration, avoiding these intermediate allocations. Benchmarks show improved performance, particularly when reading rules. Depends on: mdlayher/netlink#258
Owner
|
Hi @nickgarlis, thanks for the contributions. I've added you as a collaborator since you're actively making improvements to the ecosystem. Thank you! |
Collaborator
Author
Hi @mdlayher, thanks for the invite! I appreciate the trust. I’ll do my best to be helpful and keep things in good shape. |
aojea
reviewed
Feb 27, 2026
aojea
reviewed
Feb 27, 2026
Add ReceiveIter() method that returns an iter.Seq2[Message, error] for iterating over netlink messages without collecting them into a slice. Refactor lockedReceive() to use the new lockedReceiveIter() iterator internally to eliminate code duplication. Reduces memory usage for large responses, particularly when using ReceiveIter. If iteration is stopped early on multi-part responses, the remaining buffer is drained to keep the socket in a consistent state.
6adc9ca to
9c6cd65
Compare
Collaborator
Author
|
I benchmarked this change using BenchmarkNftablesDump with the current nftables implementation (using Receive), and observed the following results: I then updated the consumer to use ReceiveIter as shown in this PR google/nftables#357, and got the following results: |
nickgarlis
added a commit
to nickgarlis/nftables
that referenced
this pull request
Mar 19, 2026
Currently, listing many rules or set elements generates a lot of intermediate slices, which increases memory usage unnecessarily. This change unmarshals elements during iteration, avoiding these intermediate allocations. Benchmarks show improved performance, particularly when reading rules. Depends on: mdlayher/netlink#258
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add ReceiveIter() method that returns an iter.Seq2[Message, error] for iterating over netlink messages without collecting them into a slice. Refactor lockedReceive() to use the new lockedReceiveIter() iterator internally to eliminate code duplication.
I've tested this with google/nftables and got a notable 33% memory pressure reduction on large responses.
This is in an exploratory phase but is looking promising so far. I think that in order to evaluate this approach, the should be addressed:
cc @aojea