Skip to content

Commit 6e18dd4

Browse files
authored
fix: reduce allocations in or_insert and get_or_insert (#6136)
## Description This PR tries to reduce allocations in a few places by replacing (get)_or_insert with (get)_or_insert_with
1 parent 62f0e65 commit 6e18dd4

File tree

11 files changed

+70
-47
lines changed

11 files changed

+70
-47
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ libp2p-ping = { version = "0.47.0", path = "protocols/ping" }
9797
libp2p-plaintext = { version = "0.43.0", path = "transports/plaintext" }
9898
libp2p-pnet = { version = "0.26.0", path = "transports/pnet" }
9999
libp2p-quic = { version = "0.13.0", path = "transports/quic" }
100-
libp2p-relay = { version = "0.21.0", path = "protocols/relay" }
100+
libp2p-relay = { version = "0.21.1", path = "protocols/relay" }
101101
libp2p-rendezvous = { version = "0.17.0", path = "protocols/rendezvous" }
102102
libp2p-request-response = { version = "0.29.0", path = "protocols/request-response" }
103103
libp2p-server = { version = "0.12.7", path = "misc/server" }
@@ -109,7 +109,7 @@ libp2p-tcp = { version = "0.44.0", path = "transports/tcp" }
109109
libp2p-tls = { version = "0.6.2", path = "transports/tls" }
110110
libp2p-uds = { version = "0.43.0", path = "transports/uds" }
111111
libp2p-upnp = { version = "0.5.1", path = "protocols/upnp" }
112-
libp2p-webrtc = { version = "0.9.0-alpha.1", path = "transports/webrtc" }
112+
libp2p-webrtc = { version = "0.9.0-alpha.2", path = "transports/webrtc" }
113113
libp2p-webrtc-utils = { version = "0.4.0", path = "misc/webrtc-utils" }
114114
libp2p-webrtc-websys = { version = "0.4.0", path = "transports/webrtc-websys" }
115115
libp2p-websocket = { version = "0.45.2", path = "transports/websocket" }

misc/peer-store/src/memory_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<T> MemoryStore<T> {
9595
let record = self
9696
.records
9797
.entry(*peer)
98-
.or_insert(PeerRecord::new(self.config.record_capacity));
98+
.or_insert_with(|| PeerRecord::new(self.config.record_capacity));
9999
let is_new = record.add_address(address, is_permanent);
100100
if is_new {
101101
self.push_event_and_wake(Event::PeerAddressAdded {

protocols/gossipsub/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- Remove `Rpc` from the public API.
66
See [PR 6091](https://github.com/libp2p/rust-libp2p/pull/6091)
77

8+
- reduce allocations by replacing `or_insert` with `or_insert_with`
9+
See [PR 6136](https://github.com/libp2p/rust-libp2p/pull/6136)
10+
811
- Fix `unsubscribe_backoff` expecting number of seconds instead of `Duration`
912
See [PR 6124](https://github.com/libp2p/rust-libp2p/pull/6124)
1013

protocols/gossipsub/src/behaviour.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,14 +3125,17 @@ where
31253125
// The protocol negotiation occurs once a message is sent/received. Once this happens we
31263126
// update the type of peer that this is in order to determine which kind of routing should
31273127
// occur.
3128-
let connected_peer = self.connected_peers.entry(peer_id).or_insert(PeerDetails {
3129-
kind: PeerKind::Floodsub,
3130-
connections: vec![],
3131-
outbound: false,
3132-
sender: Sender::new(self.config.connection_handler_queue_len()),
3133-
topics: Default::default(),
3134-
dont_send: LinkedHashMap::new(),
3135-
});
3128+
let connected_peer = self
3129+
.connected_peers
3130+
.entry(peer_id)
3131+
.or_insert_with(|| PeerDetails {
3132+
kind: PeerKind::Floodsub,
3133+
connections: vec![],
3134+
outbound: false,
3135+
sender: Sender::new(self.config.connection_handler_queue_len()),
3136+
topics: Default::default(),
3137+
dont_send: LinkedHashMap::new(),
3138+
});
31363139
// Add the new connection
31373140
connected_peer.connections.push(connection_id);
31383141

@@ -3150,16 +3153,19 @@ where
31503153
_: Endpoint,
31513154
_: PortUse,
31523155
) -> Result<THandler<Self>, ConnectionDenied> {
3153-
let connected_peer = self.connected_peers.entry(peer_id).or_insert(PeerDetails {
3154-
kind: PeerKind::Floodsub,
3155-
connections: vec![],
3156-
// Diverging from the go implementation we only want to consider a peer as outbound peer
3157-
// if its first connection is outbound.
3158-
outbound: !self.px_peers.contains(&peer_id),
3159-
sender: Sender::new(self.config.connection_handler_queue_len()),
3160-
topics: Default::default(),
3161-
dont_send: LinkedHashMap::new(),
3162-
});
3156+
let connected_peer = self
3157+
.connected_peers
3158+
.entry(peer_id)
3159+
.or_insert_with(|| PeerDetails {
3160+
kind: PeerKind::Floodsub,
3161+
connections: vec![],
3162+
// Diverging from the go implementation we only want to consider a peer as outbound
3163+
// peer if its first connection is outbound.
3164+
outbound: !self.px_peers.contains(&peer_id),
3165+
sender: Sender::new(self.config.connection_handler_queue_len()),
3166+
topics: Default::default(),
3167+
dont_send: LinkedHashMap::new(),
3168+
});
31633169
// Add the new connection
31643170
connected_peer.connections.push(connection_id);
31653171

protocols/relay/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.21.1
2+
3+
- reduce allocations by replacing `get_or_insert` with `get_or_insert_with`
4+
See [PR 6136](https://github.com/libp2p/rust-libp2p/pull/6136)
5+
16
## 0.21.0
27

38
<!-- Update to libp2p-swarm v0.47.0 -->

protocols/relay/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-relay"
33
edition.workspace = true
44
rust-version = { workspace = true }
55
description = "Communications relaying for libp2p"
6-
version = "0.21.0"
6+
version = "0.21.1"
77
authors = ["Parity Technologies <[email protected]>", "Max Inden <[email protected]>"]
88
license = "MIT"
99
repository = "https://github.com/libp2p/rust-libp2p"

protocols/relay/src/priv_client/transport.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,12 @@ fn parse_relayed_multiaddr(addr: Multiaddr) -> Result<RelayedMultiaddr, Transpor
297297
if before_circuit {
298298
relayed_multiaddr
299299
.relay_addr
300-
.get_or_insert(Multiaddr::empty())
300+
.get_or_insert_with(Multiaddr::empty)
301301
.push(p);
302302
} else {
303303
relayed_multiaddr
304304
.dst_addr
305-
.get_or_insert(Multiaddr::empty())
305+
.get_or_insert_with(Multiaddr::empty)
306306
.push(p);
307307
}
308308
}

transports/webrtc/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.9.0-alpha.2
2+
3+
- reduce allocations by replacing `get_or_insert` with `get_or_insert_with`
4+
See [PR 6136](https://github.com/libp2p/rust-libp2p/pull/6136)
5+
16
## 0.9.0-alpha.1
27

38
- Bump `webrtc` dependency to `0.12.0`.

transports/webrtc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libp2p-webrtc"
3-
version = "0.9.0-alpha.1"
3+
version = "0.9.0-alpha.2"
44
authors = ["Parity Technologies <[email protected]>"]
55
description = "WebRTC transport for libp2p"
66
repository = "https://github.com/libp2p/rust-libp2p"

0 commit comments

Comments
 (0)