From 209d3372095632c3e00011681180dcfa3f748acd Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 13 Aug 2025 10:46:21 +0800 Subject: [PATCH 1/5] fix: reduce allocations in `or_insert` and `get_or_insert` --- Cargo.lock | 4 +- Cargo.toml | 4 +- misc/peer-store/src/memory_store.rs | 2 +- protocols/gossipsub/src/behaviour.rs | 42 +++++++++++-------- protocols/relay/Cargo.toml | 2 +- protocols/relay/src/priv_client/transport.rs | 4 +- transports/webrtc/Cargo.toml | 2 +- transports/webrtc/src/tokio/connection.rs | 44 +++++++++++--------- 8 files changed, 57 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25b477e92b5..335c35a036c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2988,7 +2988,7 @@ dependencies = [ [[package]] name = "libp2p-relay" -version = "0.21.0" +version = "0.21.1" dependencies = [ "asynchronous-codec", "bytes", @@ -3214,7 +3214,7 @@ dependencies = [ [[package]] name = "libp2p-webrtc" -version = "0.9.0-alpha.1" +version = "0.9.0-alpha.2" dependencies = [ "async-trait", "futures", diff --git a/Cargo.toml b/Cargo.toml index feaf87d306d..cc6bbd1174a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,7 +97,7 @@ libp2p-ping = { version = "0.47.0", path = "protocols/ping" } libp2p-plaintext = { version = "0.43.0", path = "transports/plaintext" } libp2p-pnet = { version = "0.26.0", path = "transports/pnet" } libp2p-quic = { version = "0.13.0", path = "transports/quic" } -libp2p-relay = { version = "0.21.0", path = "protocols/relay" } +libp2p-relay = { version = "0.21.1", path = "protocols/relay" } libp2p-rendezvous = { version = "0.17.0", path = "protocols/rendezvous" } libp2p-request-response = { version = "0.29.0", path = "protocols/request-response" } libp2p-server = { version = "0.12.7", path = "misc/server" } @@ -109,7 +109,7 @@ libp2p-tcp = { version = "0.44.0", path = "transports/tcp" } libp2p-tls = { version = "0.6.2", path = "transports/tls" } libp2p-uds = { version = "0.43.0", path = "transports/uds" } libp2p-upnp = { version = "0.5.0", path = "protocols/upnp" } -libp2p-webrtc = { version = "0.9.0-alpha.1", path = "transports/webrtc" } +libp2p-webrtc = { version = "0.9.0-alpha.2", path = "transports/webrtc" } libp2p-webrtc-utils = { version = "0.4.0", path = "misc/webrtc-utils" } libp2p-webrtc-websys = { version = "0.4.0", path = "transports/webrtc-websys" } libp2p-websocket = { version = "0.45.2", path = "transports/websocket" } diff --git a/misc/peer-store/src/memory_store.rs b/misc/peer-store/src/memory_store.rs index 8fbe31c6292..b774d6cb42e 100644 --- a/misc/peer-store/src/memory_store.rs +++ b/misc/peer-store/src/memory_store.rs @@ -96,7 +96,7 @@ impl MemoryStore { let record = self .records .entry(*peer) - .or_insert(PeerRecord::new(self.config.record_capacity)); + .or_insert_with(|| PeerRecord::new(self.config.record_capacity)); let is_new = record.add_address(address, is_permanent); if is_new { self.push_event_and_wake(Event::PeerAddressAdded { diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 9d7d6356211..c260f5b56d4 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -3125,14 +3125,17 @@ where // The protocol negotiation occurs once a message is sent/received. Once this happens we // update the type of peer that this is in order to determine which kind of routing should // occur. - let connected_peer = self.connected_peers.entry(peer_id).or_insert(PeerDetails { - kind: PeerKind::Floodsub, - connections: vec![], - outbound: false, - sender: Sender::new(self.config.connection_handler_queue_len()), - topics: Default::default(), - dont_send: LinkedHashMap::new(), - }); + let connected_peer = self + .connected_peers + .entry(peer_id) + .or_insert_with(|| PeerDetails { + kind: PeerKind::Floodsub, + connections: vec![], + outbound: false, + sender: Sender::new(self.config.connection_handler_queue_len()), + topics: Default::default(), + dont_send: LinkedHashMap::new(), + }); // Add the new connection connected_peer.connections.push(connection_id); @@ -3150,16 +3153,19 @@ where _: Endpoint, _: PortUse, ) -> Result, ConnectionDenied> { - let connected_peer = self.connected_peers.entry(peer_id).or_insert(PeerDetails { - kind: PeerKind::Floodsub, - connections: vec![], - // Diverging from the go implementation we only want to consider a peer as outbound peer - // if its first connection is outbound. - outbound: !self.px_peers.contains(&peer_id), - sender: Sender::new(self.config.connection_handler_queue_len()), - topics: Default::default(), - dont_send: LinkedHashMap::new(), - }); + let connected_peer = self + .connected_peers + .entry(peer_id) + .or_insert_with(|| PeerDetails { + kind: PeerKind::Floodsub, + connections: vec![], + // Diverging from the go implementation we only want to consider a peer as outbound peer + // if its first connection is outbound. + outbound: !self.px_peers.contains(&peer_id), + sender: Sender::new(self.config.connection_handler_queue_len()), + topics: Default::default(), + dont_send: LinkedHashMap::new(), + }); // Add the new connection connected_peer.connections.push(connection_id); diff --git a/protocols/relay/Cargo.toml b/protocols/relay/Cargo.toml index 6124744cb0d..3871abbcf8a 100644 --- a/protocols/relay/Cargo.toml +++ b/protocols/relay/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-relay" edition.workspace = true rust-version = { workspace = true } description = "Communications relaying for libp2p" -version = "0.21.0" +version = "0.21.1" authors = ["Parity Technologies ", "Max Inden "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/protocols/relay/src/priv_client/transport.rs b/protocols/relay/src/priv_client/transport.rs index ed9faa946db..c5c17fd5137 100644 --- a/protocols/relay/src/priv_client/transport.rs +++ b/protocols/relay/src/priv_client/transport.rs @@ -297,12 +297,12 @@ fn parse_relayed_multiaddr(addr: Multiaddr) -> Result"] description = "WebRTC transport for libp2p" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/transports/webrtc/src/tokio/connection.rs b/transports/webrtc/src/tokio/connection.rs index 19232707e7f..ee1275ee347 100644 --- a/transports/webrtc/src/tokio/connection.rs +++ b/transports/webrtc/src/tokio/connection.rs @@ -203,27 +203,29 @@ impl StreamMuxer for Connection { cx: &mut Context<'_>, ) -> Poll> { let peer_conn = self.peer_conn.clone(); - let fut = self.outbound_fut.get_or_insert(Box::pin(async move { - let peer_conn = peer_conn.lock().await; + let fut = self.outbound_fut.get_or_insert_with(|| { + Box::pin(async move { + let peer_conn = peer_conn.lock().await; - let data_channel = peer_conn.create_data_channel("", None).await?; + let data_channel = peer_conn.create_data_channel("", None).await?; - // No need to hold the lock during the DTLS handshake. - drop(peer_conn); + // No need to hold the lock during the DTLS handshake. + drop(peer_conn); - tracing::trace!(channel=%data_channel.id(), "Opening data channel"); + tracing::trace!(channel=%data_channel.id(), "Opening data channel"); - let (tx, rx) = oneshot::channel::>(); + let (tx, rx) = oneshot::channel::>(); - // Wait until the data channel is opened and detach it. - register_data_channel_open_handler(data_channel, tx).await; + // Wait until the data channel is opened and detach it. + register_data_channel_open_handler(data_channel, tx).await; - // Wait until data channel is opened and ready to use - match rx.await { - Ok(detached) => Ok(detached), - Err(e) => Err(Error::Internal(e.to_string())), - } - })); + // Wait until data channel is opened and ready to use + match rx.await { + Ok(detached) => Ok(detached), + Err(e) => Err(Error::Internal(e.to_string())), + } + }) + }); match ready!(fut.as_mut().poll(cx)) { Ok(detached) => { @@ -250,12 +252,14 @@ impl StreamMuxer for Connection { tracing::debug!("Closing connection"); let peer_conn = self.peer_conn.clone(); - let fut = self.close_fut.get_or_insert(Box::pin(async move { - let peer_conn = peer_conn.lock().await; - peer_conn.close().await?; + let fut = self.close_fut.get_or_insert_with(|| { + Box::pin(async move { + let peer_conn = peer_conn.lock().await; + peer_conn.close().await?; - Ok(()) - })); + Ok(()) + }) + }); match ready!(fut.as_mut().poll(cx)) { Ok(()) => { From 9bc44da6ffe9d90c9de4500de9b62a8874a54611 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 13 Aug 2025 10:51:25 +0800 Subject: [PATCH 2/5] changelog --- protocols/gossipsub/CHANGELOG.md | 3 +++ protocols/relay/CHANGELOG.md | 5 +++++ transports/webrtc/CHANGELOG.md | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/protocols/gossipsub/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index f83c554d333..d5306393d79 100644 --- a/protocols/gossipsub/CHANGELOG.md +++ b/protocols/gossipsub/CHANGELOG.md @@ -5,6 +5,9 @@ - Remove `Rpc` from the public API. See [PR 6091](https://github.com/libp2p/rust-libp2p/pull/6091) +- reduce allocations by replacing `or_insert` with `or_insert_with` + See [PR 6136](https://github.com/libp2p/rust-libp2p/pull/6136) + ## 0.49.2 - Relax `Behaviour::with_metrics` requirements, do not require DataTransform and TopicSubscriptionFilter to also impl Default diff --git a/protocols/relay/CHANGELOG.md b/protocols/relay/CHANGELOG.md index 0f17112a76e..fde8a2a6807 100644 --- a/protocols/relay/CHANGELOG.md +++ b/protocols/relay/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.21.1 + +- reduce allocations by replacing `get_or_insert` with `get_or_insert_with` + See [PR 6136](https://github.com/libp2p/rust-libp2p/pull/6136) + ## 0.21.0 diff --git a/transports/webrtc/CHANGELOG.md b/transports/webrtc/CHANGELOG.md index c38179c5b14..d4b7ab41536 100644 --- a/transports/webrtc/CHANGELOG.md +++ b/transports/webrtc/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.9.0-alpha.2 + +- reduce allocations by replacing `get_or_insert` with `get_or_insert_with` + See [PR 6136](https://github.com/libp2p/rust-libp2p/pull/6136) + ## 0.9.0-alpha.1 - Bump `webrtc` dependency to `0.12.0`. From c8bd4a737e4bea3219c42236dbe6880ab4932a90 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 13 Aug 2025 10:53:22 +0800 Subject: [PATCH 3/5] fix fmt --- protocols/gossipsub/src/behaviour.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index c260f5b56d4..4c2b89bed31 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -3159,8 +3159,8 @@ where .or_insert_with(|| PeerDetails { kind: PeerKind::Floodsub, connections: vec![], - // Diverging from the go implementation we only want to consider a peer as outbound peer - // if its first connection is outbound. + // Diverging from the go implementation we only want to consider a peer as outbound + // peer if its first connection is outbound. outbound: !self.px_peers.contains(&peer_id), sender: Sender::new(self.config.connection_handler_queue_len()), topics: Default::default(), From 3b593d38a6b277a0d6858ae2e51b079539f418e9 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 13 Aug 2025 15:06:01 +0800 Subject: [PATCH 4/5] fix test --- Cargo.lock | 2 +- Cargo.toml | 2 +- transports/webrtc/CHANGELOG.md | 2 +- transports/webrtc/Cargo.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ceb1345e373..3b77cc3825b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3193,7 +3193,7 @@ dependencies = [ [[package]] name = "libp2p-webrtc" -version = "0.9.0-alpha.2" +version = "0.9.1-alpha" dependencies = [ "async-trait", "futures", diff --git a/Cargo.toml b/Cargo.toml index 2035a5f16b5..6ea50c4c6d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,7 +109,7 @@ libp2p-tcp = { version = "0.44.0", path = "transports/tcp" } libp2p-tls = { version = "0.6.2", path = "transports/tls" } libp2p-uds = { version = "0.43.0", path = "transports/uds" } libp2p-upnp = { version = "0.5.0", path = "protocols/upnp" } -libp2p-webrtc = { version = "0.9.0-alpha.2", path = "transports/webrtc" } +libp2p-webrtc = { version = "0.9.1-alpha", path = "transports/webrtc" } libp2p-webrtc-utils = { version = "0.4.0", path = "misc/webrtc-utils" } libp2p-webrtc-websys = { version = "0.4.0", path = "transports/webrtc-websys" } libp2p-websocket = { version = "0.45.2", path = "transports/websocket" } diff --git a/transports/webrtc/CHANGELOG.md b/transports/webrtc/CHANGELOG.md index d4b7ab41536..13eedd5f0fd 100644 --- a/transports/webrtc/CHANGELOG.md +++ b/transports/webrtc/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.9.0-alpha.2 +## 0.9.1-alpha - reduce allocations by replacing `get_or_insert` with `get_or_insert_with` See [PR 6136](https://github.com/libp2p/rust-libp2p/pull/6136) diff --git a/transports/webrtc/Cargo.toml b/transports/webrtc/Cargo.toml index 0eb9d948e5d..08243cba727 100644 --- a/transports/webrtc/Cargo.toml +++ b/transports/webrtc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-webrtc" -version = "0.9.0-alpha.2" +version = "0.9.1-alpha" authors = ["Parity Technologies "] description = "WebRTC transport for libp2p" repository = "https://github.com/libp2p/rust-libp2p" From 58294c8bf017bf3cb35997c774fc6b555f293ded Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Wed, 13 Aug 2025 16:14:01 +0800 Subject: [PATCH 5/5] revert version to 0.9.0-alpha.2 --- Cargo.lock | 2 +- Cargo.toml | 2 +- transports/webrtc/CHANGELOG.md | 2 +- transports/webrtc/Cargo.toml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b77cc3825b..ceb1345e373 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3193,7 +3193,7 @@ dependencies = [ [[package]] name = "libp2p-webrtc" -version = "0.9.1-alpha" +version = "0.9.0-alpha.2" dependencies = [ "async-trait", "futures", diff --git a/Cargo.toml b/Cargo.toml index 6ea50c4c6d5..2035a5f16b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,7 +109,7 @@ libp2p-tcp = { version = "0.44.0", path = "transports/tcp" } libp2p-tls = { version = "0.6.2", path = "transports/tls" } libp2p-uds = { version = "0.43.0", path = "transports/uds" } libp2p-upnp = { version = "0.5.0", path = "protocols/upnp" } -libp2p-webrtc = { version = "0.9.1-alpha", path = "transports/webrtc" } +libp2p-webrtc = { version = "0.9.0-alpha.2", path = "transports/webrtc" } libp2p-webrtc-utils = { version = "0.4.0", path = "misc/webrtc-utils" } libp2p-webrtc-websys = { version = "0.4.0", path = "transports/webrtc-websys" } libp2p-websocket = { version = "0.45.2", path = "transports/websocket" } diff --git a/transports/webrtc/CHANGELOG.md b/transports/webrtc/CHANGELOG.md index 13eedd5f0fd..d4b7ab41536 100644 --- a/transports/webrtc/CHANGELOG.md +++ b/transports/webrtc/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.9.1-alpha +## 0.9.0-alpha.2 - reduce allocations by replacing `get_or_insert` with `get_or_insert_with` See [PR 6136](https://github.com/libp2p/rust-libp2p/pull/6136) diff --git a/transports/webrtc/Cargo.toml b/transports/webrtc/Cargo.toml index 08243cba727..0eb9d948e5d 100644 --- a/transports/webrtc/Cargo.toml +++ b/transports/webrtc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-webrtc" -version = "0.9.1-alpha" +version = "0.9.0-alpha.2" authors = ["Parity Technologies "] description = "WebRTC transport for libp2p" repository = "https://github.com/libp2p/rust-libp2p"