Skip to content

Commit

Permalink
Drop the dist HashMap in routing, replacing it with a Vec.
Browse files Browse the repository at this point in the history
Now that we have unique, dense, 32-bit identifiers for all the
nodes in our network graph, we can store the per-node information
when routing in a simple `Vec` rather than a `HashMap`. This avoids
the overhead of hashing and table scanning entirely, for a nice
"simple" optimization win.
  • Loading branch information
TheBlueMatt committed Jul 10, 2024
1 parent c34980c commit 43d250d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 43 deletions.
16 changes: 12 additions & 4 deletions lightning/src/routing/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,14 @@ impl<'a> DirectedChannelInfo<'a> {
/// Refers to the `node_id` receiving the payment from the previous hop.
#[inline]
pub fn target(&self) -> &'a NodeId { if self.from_node_one { &self.channel.node_two } else { &self.channel.node_one } }

/// Returns the source node's counter
#[inline]
pub(super) fn source_counter(&self) -> u32 { if self.from_node_one { self.channel.node_one_counter } else { self.channel.node_two_counter } }

/// Returns the target node's counter
#[inline]
pub(super) fn target_counter(&self) -> u32 { if self.from_node_one { self.channel.node_two_counter } else { self.channel.node_one_counter } }
}

impl<'a> fmt::Debug for DirectedChannelInfo<'a> {
Expand Down Expand Up @@ -1854,21 +1862,21 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
(&mut channel_info.node_one_counter, node_id_a),
(&mut channel_info.node_two_counter, node_id_b)
];
for (node_counter, current_node_id) in node_counter_id.iter_mut() {
for (chan_info_node_counter, current_node_id) in node_counter_id.iter_mut() {
match nodes.entry(current_node_id.clone()) {
IndexedMapEntry::Occupied(node_entry) => {
let node = node_entry.into_mut();
node.channels.push(short_channel_id);
**node_counter = node.node_counter;
**chan_info_node_counter = node.node_counter;
},
IndexedMapEntry::Vacant(node_entry) => {
let mut removed_node_counters = self.removed_node_counters.lock().unwrap();
**node_counter = removed_node_counters.pop()
**chan_info_node_counter = removed_node_counters.pop()
.unwrap_or(self.next_node_counter.fetch_add(1, Ordering::Relaxed) as u32);
node_entry.insert(NodeInfo {
channels: vec!(short_channel_id),
announcement_info: None,
node_counter: **node_counter,
node_counter: **chan_info_node_counter,
});
}
};
Expand Down
Loading

0 comments on commit 43d250d

Please sign in to comment.