Skip to content

Commit 43d250d

Browse files
committed
Drop the dist HashMap in routing, replacing it with a Vec.
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.
1 parent c34980c commit 43d250d

File tree

3 files changed

+145
-43
lines changed

3 files changed

+145
-43
lines changed

lightning/src/routing/gossip.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,14 @@ impl<'a> DirectedChannelInfo<'a> {
11021102
/// Refers to the `node_id` receiving the payment from the previous hop.
11031103
#[inline]
11041104
pub fn target(&self) -> &'a NodeId { if self.from_node_one { &self.channel.node_two } else { &self.channel.node_one } }
1105+
1106+
/// Returns the source node's counter
1107+
#[inline]
1108+
pub(super) fn source_counter(&self) -> u32 { if self.from_node_one { self.channel.node_one_counter } else { self.channel.node_two_counter } }
1109+
1110+
/// Returns the target node's counter
1111+
#[inline]
1112+
pub(super) fn target_counter(&self) -> u32 { if self.from_node_one { self.channel.node_two_counter } else { self.channel.node_one_counter } }
11051113
}
11061114

11071115
impl<'a> fmt::Debug for DirectedChannelInfo<'a> {
@@ -1854,21 +1862,21 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
18541862
(&mut channel_info.node_one_counter, node_id_a),
18551863
(&mut channel_info.node_two_counter, node_id_b)
18561864
];
1857-
for (node_counter, current_node_id) in node_counter_id.iter_mut() {
1865+
for (chan_info_node_counter, current_node_id) in node_counter_id.iter_mut() {
18581866
match nodes.entry(current_node_id.clone()) {
18591867
IndexedMapEntry::Occupied(node_entry) => {
18601868
let node = node_entry.into_mut();
18611869
node.channels.push(short_channel_id);
1862-
**node_counter = node.node_counter;
1870+
**chan_info_node_counter = node.node_counter;
18631871
},
18641872
IndexedMapEntry::Vacant(node_entry) => {
18651873
let mut removed_node_counters = self.removed_node_counters.lock().unwrap();
1866-
**node_counter = removed_node_counters.pop()
1874+
**chan_info_node_counter = removed_node_counters.pop()
18671875
.unwrap_or(self.next_node_counter.fetch_add(1, Ordering::Relaxed) as u32);
18681876
node_entry.insert(NodeInfo {
18691877
channels: vec!(short_channel_id),
18701878
announcement_info: None,
1871-
node_counter: **node_counter,
1879+
node_counter: **chan_info_node_counter,
18721880
});
18731881
}
18741882
};

0 commit comments

Comments
 (0)