diff --git a/Cargo.lock b/Cargo.lock index 698afbec45f..aa9219d57ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2959,7 +2959,7 @@ dependencies = [ [[package]] name = "libp2p-relay" -version = "0.21.0" +version = "0.21.1" dependencies = [ "asynchronous-codec", "bytes", @@ -3185,7 +3185,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 0518f70128d..842a08e7ecc 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.1", 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 84b0cf2622e..ed996dc6fdd 100644 --- a/misc/peer-store/src/memory_store.rs +++ b/misc/peer-store/src/memory_store.rs @@ -95,7 +95,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/CHANGELOG.md b/protocols/gossipsub/CHANGELOG.md index a72eef56fe0..0bfc18b5876 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) + - Fix `unsubscribe_backoff` expecting number of seconds instead of `Duration` See [PR 6124](https://github.com/libp2p/rust-libp2p/pull/6124) diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 9d7d6356211..4c2b89bed31 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/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/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(()) => {