Skip to content

Commit

Permalink
keep peers in HashSet instead of SmallVec
Browse files Browse the repository at this point in the history
This prevents us from adding duplicates into the list of peer addresses we have.
  • Loading branch information
b-zee committed Oct 23, 2023
1 parent 525a3ab commit 98355b6
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ where
/// reachable addresses, if any.
connected: HashMap<PeerId, SmallVec<[Connection; 2]>>,
/// Externally managed addresses via `add_address` and `remove_address`.
addresses: HashMap<PeerId, SmallVec<[Multiaddr; 6]>>,
addresses: HashMap<PeerId, HashSet<Multiaddr>>,
/// Requests that have not yet been sent and are waiting for a connection
/// to be established.
pending_outbound_requests: HashMap<PeerId, SmallVec<[RequestProtocol<TCodec>; 10]>>,
Expand Down Expand Up @@ -441,15 +441,7 @@ where
/// Returns true if the address was added, false otherwise (i.e. if the
/// address is already in the list).
pub fn add_address(&mut self, peer: &PeerId, address: Multiaddr) -> bool {
let addrs = self.addresses.entry(*peer).or_default();

// Add only if address is not already in the list.
if addrs.iter().all(|a| *a != address) {
addrs.push(address);
true
} else {
false
}
self.addresses.entry(*peer).or_default().insert(address)
}

/// Removes an address of a peer previously added via `add_address`.
Expand Down

0 comments on commit 98355b6

Please sign in to comment.