Skip to content

Commit 6bdc24c

Browse files
committed
Add a new IndexedMap type and use it in network graph storage
Our network graph has to be iterable in a deterministic order and with the ability to iterate over a specific range. Thus, historically, we've used a `BTreeMap` to do the iteration. This is fine, except our map needs to also provide high performance lookups in order to make route-finding fast. Sadly, `BTreeMap`s are quite slow due to the branching penalty. Here we replace the `BTreeMap`s in the scorer with a dummy wrapper. In the next commit the internals thereof will be replaced with a `HashMap`-based implementation.
1 parent 6991a14 commit 6bdc24c

File tree

4 files changed

+190
-32
lines changed

4 files changed

+190
-32
lines changed

lightning/src/routing/gossip.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ use crate::util::logger::{Logger, Level};
3232
use crate::util::events::{MessageSendEvent, MessageSendEventsProvider};
3333
use crate::util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
3434
use crate::util::string::PrintableString;
35+
use crate::util::indexed_map::{IndexedMap, Entry as IndexedMapEntry};
3536

3637
use crate::io;
3738
use crate::io_extras::{copy, sink};
3839
use crate::prelude::*;
39-
use alloc::collections::{BTreeMap, btree_map::Entry as BtreeEntry};
4040
use core::{cmp, fmt};
4141
use crate::sync::{RwLock, RwLockReadGuard};
4242
#[cfg(feature = "std")]
@@ -133,8 +133,8 @@ pub struct NetworkGraph<L: Deref> where L::Target: Logger {
133133
genesis_hash: BlockHash,
134134
logger: L,
135135
// Lock order: channels -> nodes
136-
channels: RwLock<BTreeMap<u64, ChannelInfo>>,
137-
nodes: RwLock<BTreeMap<NodeId, NodeInfo>>,
136+
channels: RwLock<IndexedMap<u64, ChannelInfo>>,
137+
nodes: RwLock<IndexedMap<NodeId, NodeInfo>>,
138138
// Lock order: removed_channels -> removed_nodes
139139
//
140140
// NOTE: In the following `removed_*` maps, we use seconds since UNIX epoch to track time instead
@@ -158,8 +158,8 @@ pub struct NetworkGraph<L: Deref> where L::Target: Logger {
158158

159159
/// A read-only view of [`NetworkGraph`].
160160
pub struct ReadOnlyNetworkGraph<'a> {
161-
channels: RwLockReadGuard<'a, BTreeMap<u64, ChannelInfo>>,
162-
nodes: RwLockReadGuard<'a, BTreeMap<NodeId, NodeInfo>>,
161+
channels: RwLockReadGuard<'a, IndexedMap<u64, ChannelInfo>>,
162+
nodes: RwLockReadGuard<'a, IndexedMap<NodeId, NodeInfo>>,
163163
}
164164

165165
/// Update to the [`NetworkGraph`] based on payment failure information conveyed via the Onion
@@ -1130,13 +1130,13 @@ impl<L: Deref> Writeable for NetworkGraph<L> where L::Target: Logger {
11301130
self.genesis_hash.write(writer)?;
11311131
let channels = self.channels.read().unwrap();
11321132
(channels.len() as u64).write(writer)?;
1133-
for (ref chan_id, ref chan_info) in channels.iter() {
1133+
for (ref chan_id, ref chan_info) in channels.unordered_iter() {
11341134
(*chan_id).write(writer)?;
11351135
chan_info.write(writer)?;
11361136
}
11371137
let nodes = self.nodes.read().unwrap();
11381138
(nodes.len() as u64).write(writer)?;
1139-
for (ref node_id, ref node_info) in nodes.iter() {
1139+
for (ref node_id, ref node_info) in nodes.unordered_iter() {
11401140
node_id.write(writer)?;
11411141
node_info.write(writer)?;
11421142
}
@@ -1155,14 +1155,14 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
11551155

11561156
let genesis_hash: BlockHash = Readable::read(reader)?;
11571157
let channels_count: u64 = Readable::read(reader)?;
1158-
let mut channels: BTreeMap<u64, ChannelInfo> = BTreeMap::new();
1158+
let mut channels = IndexedMap::new();
11591159
for _ in 0..channels_count {
11601160
let chan_id: u64 = Readable::read(reader)?;
11611161
let chan_info = Readable::read(reader)?;
11621162
channels.insert(chan_id, chan_info);
11631163
}
11641164
let nodes_count: u64 = Readable::read(reader)?;
1165-
let mut nodes: BTreeMap<NodeId, NodeInfo> = BTreeMap::new();
1165+
let mut nodes = IndexedMap::new();
11661166
for _ in 0..nodes_count {
11671167
let node_id = Readable::read(reader)?;
11681168
let node_info = Readable::read(reader)?;
@@ -1190,11 +1190,11 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
11901190
impl<L: Deref> fmt::Display for NetworkGraph<L> where L::Target: Logger {
11911191
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
11921192
writeln!(f, "Network map\n[Channels]")?;
1193-
for (key, val) in self.channels.read().unwrap().iter() {
1193+
for (key, val) in self.channels.read().unwrap().unordered_iter() {
11941194
writeln!(f, " {}: {}", key, val)?;
11951195
}
11961196
writeln!(f, "[Nodes]")?;
1197-
for (&node_id, val) in self.nodes.read().unwrap().iter() {
1197+
for (&node_id, val) in self.nodes.read().unwrap().unordered_iter() {
11981198
writeln!(f, " {}: {}", log_bytes!(node_id.as_slice()), val)?;
11991199
}
12001200
Ok(())
@@ -1217,8 +1217,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
12171217
secp_ctx: Secp256k1::verification_only(),
12181218
genesis_hash,
12191219
logger,
1220-
channels: RwLock::new(BTreeMap::new()),
1221-
nodes: RwLock::new(BTreeMap::new()),
1220+
channels: RwLock::new(IndexedMap::new()),
1221+
nodes: RwLock::new(IndexedMap::new()),
12221222
last_rapid_gossip_sync_timestamp: Mutex::new(None),
12231223
removed_channels: Mutex::new(HashMap::new()),
12241224
removed_nodes: Mutex::new(HashMap::new()),
@@ -1251,7 +1251,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
12511251
/// purposes.
12521252
#[cfg(test)]
12531253
pub fn clear_nodes_announcement_info(&self) {
1254-
for node in self.nodes.write().unwrap().iter_mut() {
1254+
for node in self.nodes.write().unwrap().unordered_iter_mut() {
12551255
node.1.announcement_info = None;
12561256
}
12571257
}
@@ -1381,7 +1381,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
13811381
let node_id_b = channel_info.node_two.clone();
13821382

13831383
match channels.entry(short_channel_id) {
1384-
BtreeEntry::Occupied(mut entry) => {
1384+
IndexedMapEntry::Occupied(mut entry) => {
13851385
//TODO: because asking the blockchain if short_channel_id is valid is only optional
13861386
//in the blockchain API, we need to handle it smartly here, though it's unclear
13871387
//exactly how...
@@ -1400,17 +1400,17 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14001400
return Err(LightningError{err: "Already have knowledge of channel".to_owned(), action: ErrorAction::IgnoreDuplicateGossip});
14011401
}
14021402
},
1403-
BtreeEntry::Vacant(entry) => {
1403+
IndexedMapEntry::Vacant(entry) => {
14041404
entry.insert(channel_info);
14051405
}
14061406
};
14071407

14081408
for current_node_id in [node_id_a, node_id_b].iter() {
14091409
match nodes.entry(current_node_id.clone()) {
1410-
BtreeEntry::Occupied(node_entry) => {
1410+
IndexedMapEntry::Occupied(node_entry) => {
14111411
node_entry.into_mut().channels.push(short_channel_id);
14121412
},
1413-
BtreeEntry::Vacant(node_entry) => {
1413+
IndexedMapEntry::Vacant(node_entry) => {
14141414
node_entry.insert(NodeInfo {
14151415
channels: vec!(short_channel_id),
14161416
announcement_info: None,
@@ -1584,7 +1584,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
15841584
for scid in node.channels.iter() {
15851585
if let Some(chan_info) = channels.remove(scid) {
15861586
let other_node_id = if node_id == chan_info.node_one { chan_info.node_two } else { chan_info.node_one };
1587-
if let BtreeEntry::Occupied(mut other_node_entry) = nodes.entry(other_node_id) {
1587+
if let IndexedMapEntry::Occupied(mut other_node_entry) = nodes.entry(other_node_id) {
15881588
other_node_entry.get_mut().channels.retain(|chan_id| {
15891589
*scid != *chan_id
15901590
});
@@ -1643,7 +1643,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16431643
// Sadly BTreeMap::retain was only stabilized in 1.53 so we can't switch to it for some
16441644
// time.
16451645
let mut scids_to_remove = Vec::new();
1646-
for (scid, info) in channels.iter_mut() {
1646+
for (scid, info) in channels.unordered_iter_mut() {
16471647
if info.one_to_two.is_some() && info.one_to_two.as_ref().unwrap().last_update < min_time_unix {
16481648
info.one_to_two = None;
16491649
}
@@ -1812,10 +1812,10 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
18121812
Ok(())
18131813
}
18141814

1815-
fn remove_channel_in_nodes(nodes: &mut BTreeMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
1815+
fn remove_channel_in_nodes(nodes: &mut IndexedMap<NodeId, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
18161816
macro_rules! remove_from_node {
18171817
($node_id: expr) => {
1818-
if let BtreeEntry::Occupied(mut entry) = nodes.entry($node_id) {
1818+
if let IndexedMapEntry::Occupied(mut entry) = nodes.entry($node_id) {
18191819
entry.get_mut().channels.retain(|chan_id| {
18201820
short_channel_id != *chan_id
18211821
});
@@ -1836,8 +1836,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
18361836
impl ReadOnlyNetworkGraph<'_> {
18371837
/// Returns all known valid channels' short ids along with announced channel info.
18381838
///
1839-
/// (C-not exported) because we have no mapping for `BTreeMap`s
1840-
pub fn channels(&self) -> &BTreeMap<u64, ChannelInfo> {
1839+
/// (C-not exported) because we don't want to return lifetime'd references
1840+
pub fn channels(&self) -> &IndexedMap<u64, ChannelInfo> {
18411841
&*self.channels
18421842
}
18431843

@@ -1854,8 +1854,8 @@ impl ReadOnlyNetworkGraph<'_> {
18541854

18551855
/// Returns all known nodes' public keys along with announced node info.
18561856
///
1857-
/// (C-not exported) because we have no mapping for `BTreeMap`s
1858-
pub fn nodes(&self) -> &BTreeMap<NodeId, NodeInfo> {
1857+
/// (C-not exported) because we don't want to return lifetime'd references
1858+
pub fn nodes(&self) -> &IndexedMap<NodeId, NodeInfo> {
18591859
&*self.nodes
18601860
}
18611861

lightning/src/routing/router.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5507,9 +5507,9 @@ mod tests {
55075507
'load_endpoints: for _ in 0..10 {
55085508
loop {
55095509
seed = seed.overflowing_mul(0xdeadbeef).0;
5510-
let src = &PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
5510+
let src = &PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
55115511
seed = seed.overflowing_mul(0xdeadbeef).0;
5512-
let dst = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
5512+
let dst = PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
55135513
let payment_params = PaymentParameters::from_node_id(dst);
55145514
let amt = seed as u64 % 200_000_000;
55155515
let params = ProbabilisticScoringParameters::default();
@@ -5545,9 +5545,9 @@ mod tests {
55455545
'load_endpoints: for _ in 0..10 {
55465546
loop {
55475547
seed = seed.overflowing_mul(0xdeadbeef).0;
5548-
let src = &PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
5548+
let src = &PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
55495549
seed = seed.overflowing_mul(0xdeadbeef).0;
5550-
let dst = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
5550+
let dst = PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
55515551
let payment_params = PaymentParameters::from_node_id(dst).with_features(channelmanager::provided_invoice_features(&config));
55525552
let amt = seed as u64 % 200_000_000;
55535553
let params = ProbabilisticScoringParameters::default();
@@ -5745,9 +5745,9 @@ mod benches {
57455745
'load_endpoints: for _ in 0..150 {
57465746
loop {
57475747
seed *= 0xdeadbeef;
5748-
let src = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
5748+
let src = PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
57495749
seed *= 0xdeadbeef;
5750-
let dst = PublicKey::from_slice(nodes.keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
5750+
let dst = PublicKey::from_slice(nodes.unordered_keys().skip(seed % nodes.len()).next().unwrap().as_slice()).unwrap();
57515751
let params = PaymentParameters::from_node_id(dst).with_features(features.clone());
57525752
let first_hop = first_hop(src);
57535753
let amt = seed as u64 % 1_000_000;

lightning/src/util/indexed_map.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
//! This module has a map which can be iterated in a deterministic order. See the [`IndexedMap`].
2+
3+
use alloc::collections::{BTreeMap, btree_map};
4+
use core::cmp::Ord;
5+
use core::ops::RangeBounds;
6+
7+
/// A map which can be iterated in a deterministic order.
8+
///
9+
/// This would traditionally be accomplished by simply using a `BTreeMap`, however a B-Trees
10+
/// generally have very slow lookups. Because we use a nodes+channels map while finding routes
11+
/// across the network graph, our network graph backing map must be as performant as possible.
12+
/// However, because peers expect to sync the network graph from us (and we need to support that
13+
/// without holding a lock on the graph for the duration of the sync or dumping the entire graph
14+
/// into our outbound message queue), we need an iterable map with a consistent iteration order we
15+
/// can jump to a starting point on.
16+
///
17+
/// Thus, we have a custom data structure here - its API mimics that of Rust's `BTreeMap`, but is
18+
/// actually backed by a `HashMap`, with some additional tracking to ensure we can iterate over
19+
/// keys in the order defined by [`Ord`].
20+
#[derive(Clone, PartialEq, Eq)]
21+
pub struct IndexedMap<K: Ord, V> {
22+
map: BTreeMap<K, V>,
23+
}
24+
25+
impl<K: Ord, V> IndexedMap<K, V> {
26+
/// Constructs a new, empty map
27+
pub fn new() -> Self {
28+
Self {
29+
map: BTreeMap::new(),
30+
}
31+
}
32+
33+
#[inline(always)]
34+
/// Fetches the element with the given `key`, if one exists.
35+
pub fn get(&self, key: &K) -> Option<&V> {
36+
self.map.get(key)
37+
}
38+
39+
/// Fetches a mutable reference to the element with the given `key`, if one exists.
40+
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
41+
self.map.get_mut(key)
42+
}
43+
44+
#[inline]
45+
/// Returns true if an element with the given `key` exists in the map.
46+
pub fn contains_key(&self, key: &K) -> bool {
47+
self.map.contains_key(key)
48+
}
49+
50+
/// Removes the element with the given `key`, returning it, if one exists.
51+
pub fn remove(&mut self, key: &K) -> Option<V> {
52+
self.map.remove(key)
53+
}
54+
55+
/// Inserts the given `key`/`value` pair into the map, returning the element that was
56+
/// previously stored at the given `key`, if one exists.
57+
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
58+
self.map.insert(key, value)
59+
}
60+
61+
/// Returns an [`Entry`] for the given `key` in the map, allowing access to the value.
62+
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
63+
match self.map.entry(key) {
64+
btree_map::Entry::Vacant(entry) => {
65+
Entry::Vacant(VacantEntry {
66+
underlying_entry: entry
67+
})
68+
},
69+
btree_map::Entry::Occupied(entry) => {
70+
Entry::Occupied(OccupiedEntry {
71+
underlying_entry: entry
72+
})
73+
}
74+
}
75+
}
76+
77+
/// Returns an iterator which iterates over the keys in the map, in a random order.
78+
pub fn unordered_keys(&self) -> impl Iterator<Item = &K> {
79+
self.map.keys()
80+
}
81+
82+
/// Returns an iterator which iterates over the `key`/`value` pairs in a random order.
83+
pub fn unordered_iter(&self) -> impl Iterator<Item = (&K, &V)> {
84+
self.map.iter()
85+
}
86+
87+
/// Returns an iterator which iterates over the `key`s and mutable references to `value`s in a
88+
/// random order.
89+
pub fn unordered_iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
90+
self.map.iter_mut()
91+
}
92+
93+
/// Returns an iterator which iterates over the `key`/`value` pairs in a given range.
94+
pub fn range<R: RangeBounds<K>>(&self, range: R) -> btree_map::Range<K, V> {
95+
self.map.range(range)
96+
}
97+
98+
/// Returns the number of `key`/`value` pairs in the map
99+
pub fn len(&self) -> usize {
100+
self.map.len()
101+
}
102+
103+
/// Returns true if there are no elements in the map
104+
pub fn is_empty(&self) -> bool {
105+
self.map.is_empty()
106+
}
107+
}
108+
109+
/// An [`Entry`] for a key which currently has no value
110+
pub struct VacantEntry<'a, K: Ord, V> {
111+
underlying_entry: btree_map::VacantEntry<'a, K, V>,
112+
}
113+
114+
/// An [`Entry`] for an existing key-value pair
115+
pub struct OccupiedEntry<'a, K: Ord, V> {
116+
underlying_entry: btree_map::OccupiedEntry<'a, K, V>,
117+
}
118+
119+
/// A mutable reference to a position in the map. This can be used to reference, add, or update the
120+
/// value at a fixed key.
121+
pub enum Entry<'a, K: Ord, V> {
122+
/// A mutable reference to a position within the map where there is no value.
123+
Vacant(VacantEntry<'a, K, V>),
124+
/// A mutable reference to a position within the map where there is currently a value.
125+
Occupied(OccupiedEntry<'a, K, V>),
126+
}
127+
128+
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
129+
/// Insert a value into the position described by this entry.
130+
pub fn insert(self, value: V) -> &'a mut V {
131+
self.underlying_entry.insert(value)
132+
}
133+
}
134+
135+
impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
136+
/// Remove the value at the position described by this entry.
137+
pub fn remove_entry(self) -> (K, V) {
138+
self.underlying_entry.remove_entry()
139+
}
140+
141+
/// Get a reference to the value at the position described by this entry.
142+
pub fn get(&self) -> &V {
143+
self.underlying_entry.get()
144+
}
145+
146+
/// Get a mutable reference to the value at the position described by this entry.
147+
pub fn get_mut(&mut self) -> &mut V {
148+
self.underlying_entry.get_mut()
149+
}
150+
151+
/// Consume this entry, returning a mutable reference to the value at the position described by
152+
/// this entry.
153+
pub fn into_mut(self) -> &'a mut V {
154+
self.underlying_entry.into_mut()
155+
}
156+
}

lightning/src/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ pub(crate) mod transaction_utils;
4040
pub(crate) mod scid_utils;
4141
pub(crate) mod time;
4242

43+
pub mod indexed_map;
44+
4345
/// Logging macro utilities.
4446
#[macro_use]
4547
pub(crate) mod macro_logger;

0 commit comments

Comments
 (0)