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

Optimize NetworkPacket struct size #36

Merged

Conversation

one-d-wide
Copy link
Contributor

Hi.

I identified a performance bottleneck in NetworkPacket being 9376 bytes long! This amount was mainly contributed by etherparse::NetHeaders enum including etherparse::Ipv{4,6}Extensions, that are 1032 and 9236 bytes long respectively, while not being used anywhere in this project.

Since NetworkPacket is quiet often passed around in memory and it's so heavy that it caused memmove to kick in ref. This resulted in a significant amount of time being spent in memmove even for smaller payloads.

So excluding the ip extensions' data from the NetworkPacket resulted in 6000% performance gain for decoding small packets (64 bytes) and 200% gain for decoding large packets (65k bytes). The gain is decreasing because currently all the payload stored in NetworkPacket is still being copied to a Vec.

Flamegraph for `decode_mtu_64` benchmark without the optimization

decode_mtu_64

Flamegraph for `decode_mtu_64` benchmark with the optimization

decode_mtu_64_optim

Also the way I wrapped criterion to allow it to benchmark private fields isn't really conventional, so feel free to discard associated commit if that doesn't fit this project.

@SajjadPourali SajjadPourali self-assigned this Apr 6, 2024
@SajjadPourali SajjadPourali merged commit 4fcc81f into narrowlink:main Apr 6, 2024
3 checks passed
@SajjadPourali
Copy link
Collaborator

Thanks!

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 this pull request may close these issues.

2 participants