Skip to content

Commit

Permalink
fix(swarm): eliminating protocol cloning when nothing is happening
Browse files Browse the repository at this point in the history
Code keeps the API while eliminating repetitive protocol cloning when protocols did not change, If protocol changes occur, only then the protocols are cloned to a reused buffer from which they are borrowed for iteration. Following are benchmark results:

|behaviour count|iterations|protocols|timings|change*|
|-|-|-|-|-|
|1|1000|10|27.798 µs 28.134 µs 28.493 µs|-15.771% -14.523% -13.269%|
|1|1000|100|55.171 µs 55.578 µs 56.009 µs|-51.831% -50.162% -48.437%|
|1|1000|1000|289.24 µs 290.99 µs 293.00 µs|-61.748% -60.895% -60.054%|
|5|1000|2|34.000 µs 34.216 µs 34.457 µs|-18.538% -16.231% -14.011%|
|5|1000|20|70.962 µs 71.428 µs 72.005 µs|-40.501% -38.944% -37.309%|
|5|1000|200|426.17 µs 433.27 µs 442.60 µs|-44.824% -42.663% -40.262%|
|10|1000|1|42.993 µs 44.382 µs 45.655 µs|-18.839% -16.292% -13.584%|
|10|1000|10|94.022 µs 96.787 µs 99.321 µs|-25.469% -23.572% -21.562%|
|10|1000|100|543.13 µs 554.91 µs 569.06 µs|-43.781% -42.189% -40.568%|
|20|500|1|63.150 µs 64.846 µs 66.860 µs|-9.5693% -6.1722% -2.6400%|
|20|500|10|212.21 µs 217.48 µs 222.64 µs|-16.525% -14.234% -11.925%|
|20|500|100|1.6651 ms 1.7083 ms 1.7490 ms|-27.704% -25.683% -23.618%|

change*: 3da7d91 is the baseline

Pull-Request: #5026.
  • Loading branch information
jakubDoka committed Jul 5, 2024
1 parent 693d7b7 commit 02fc027
Show file tree
Hide file tree
Showing 9 changed files with 707 additions and 91 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ libp2p-rendezvous = { version = "0.14.1", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.26.4", path = "protocols/request-response" }
libp2p-server = { version = "0.12.7", path = "misc/server" }
libp2p-stream = { version = "0.1.0-alpha.1", path = "protocols/stream" }
libp2p-swarm = { version = "0.45.0", path = "swarm" }
libp2p-swarm = { version = "0.45.1", path = "swarm" }
libp2p-swarm-derive = { version = "=0.34.2", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" }
libp2p-tcp = { version = "0.42.0", path = "transports/tcp" }
Expand Down
7 changes: 7 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@

## 0.45.1

- Optimize internal connection `fn poll`. New implementation now scales much better with number of listen protocols active.
No changes to public API introduced.
See [PR 5026](https://github.com/libp2p/rust-libp2p/pull/5026)

## 0.45.0

- Add peer_id to `FromSwarm::ListenFailure`.
Expand Down
7 changes: 6 additions & 1 deletion swarm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-swarm"
edition = "2021"
rust-version = { workspace = true }
description = "The libp2p swarm"
version = "0.45.0"
version = "0.45.1"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down Expand Up @@ -52,6 +52,7 @@ libp2p-swarm-derive = { path = "../swarm-derive" } # Using `pat
libp2p-swarm-test = { path = "../swarm-test" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing.
libp2p-yamux = { path = "../muxers/yamux" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing.
quickcheck = { workspace = true }
criterion = { version = "0.5", features = ["async_tokio"] }
void = "1"
once_cell = "1.19.0"
trybuild = "1.0.95"
Expand All @@ -69,5 +70,9 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]
rustc-args = ["--cfg", "docsrs"]

[[bench]]
name = "connection_handler"
harness = false

[lints]
workspace = true
Loading

0 comments on commit 02fc027

Please sign in to comment.