rust-libp2p
rust-libp2p copied to clipboard
fix(swarm): rewrite `NetworkBehaviour` macro for more optimal code gen
Description
I have rewritten NetworkBehavior
derive macro to generate more optimal and faster to compile code when using more behaviours (5, 10, 20), I noticed performance degrades even though I benchmarked the same load. This is related to https://github.com/libp2p/rust-libp2p/pull/5026.
New macro implementation generates enums and structs for each type implementing the traits instead of type-level linked lists. In many cases, this makes resulting types more compact (we store just one enum tag, whereas composed Either
s each need to store tags to make values addressable) and makes the enum dispatch constant. This also opened the opportunity to optimize UpgradeInfoIterator
and ConnectionHandler
into a state machine (they now remember where they stopped polling/iterating and skipped exhausted subhandlers/iterators). We could optimize the NetworkBehaviour
itself too, but it would require users to put extra fields into the struct (this could be optional for BC).
Change checklist
- [x] I have performed a self-review of my code
- [x] I have made corresponding changes to the documentation (none)
- [x] I have added tests that prove my fix is effective or that my feature works (no regressions)
- [x] A changelog entry has been made in the appropriate crates
note: I misused the sync feature on github and it erased all my changes from the branch, thus pr #5303 was closed @jxs, @thomaseizinger.