Skip to content

Commit 74c8a67

Browse files
committed
Swap IndexedMap implementation for a HashMap+B-Tree
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 implementation of our `IndexedMap` with a `HashMap` to store the elements itself and a `BTreeSet` to store the keys set in sorted order for iteration. As of this commit on the same hardware as the above few commits, the benchmark results are: ``` test routing::router::benches::generate_mpp_routes_with_probabilistic_scorer ... bench: 109,544,993 ns/iter (+/- 27,553,574) test routing::router::benches::generate_mpp_routes_with_zero_penalty_scorer ... bench: 81,164,590 ns/iter (+/- 55,422,930) test routing::router::benches::generate_routes_with_probabilistic_scorer ... bench: 34,726,569 ns/iter (+/- 9,646,345) test routing::router::benches::generate_routes_with_zero_penalty_scorer ... bench: 22,772,355 ns/iter (+/- 9,574,418) ```
1 parent 6bdc24c commit 74c8a67

File tree

1 file changed

+67
-22
lines changed

1 file changed

+67
-22
lines changed

lightning/src/util/indexed_map.rs

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! This module has a map which can be iterated in a deterministic order. See the [`IndexedMap`].
22
3-
use alloc::collections::{BTreeMap, btree_map};
3+
use crate::prelude::{HashMap, hash_map};
4+
use alloc::collections::{BTreeSet, btree_set};
5+
use core::hash::Hash;
46
use core::cmp::Ord;
57
use core::ops::RangeBounds;
68

@@ -18,15 +20,18 @@ use core::ops::RangeBounds;
1820
/// actually backed by a `HashMap`, with some additional tracking to ensure we can iterate over
1921
/// keys in the order defined by [`Ord`].
2022
#[derive(Clone, PartialEq, Eq)]
21-
pub struct IndexedMap<K: Ord, V> {
22-
map: BTreeMap<K, V>,
23+
pub struct IndexedMap<K: Hash + Ord, V> {
24+
map: HashMap<K, V>,
25+
// TODO: Explore swapping this for a sorted vec (that is only sorted on first range() call)
26+
keys: BTreeSet<K>,
2327
}
2428

25-
impl<K: Ord, V> IndexedMap<K, V> {
29+
impl<K: Clone + Hash + Ord, V> IndexedMap<K, V> {
2630
/// Constructs a new, empty map
2731
pub fn new() -> Self {
2832
Self {
29-
map: BTreeMap::new(),
33+
map: HashMap::new(),
34+
keys: BTreeSet::new(),
3035
}
3136
}
3237

@@ -49,26 +54,37 @@ impl<K: Ord, V> IndexedMap<K, V> {
4954

5055
/// Removes the element with the given `key`, returning it, if one exists.
5156
pub fn remove(&mut self, key: &K) -> Option<V> {
52-
self.map.remove(key)
57+
let ret = self.map.remove(key);
58+
if let Some(_) = ret {
59+
assert!(self.keys.remove(key), "map and keys must be consistent");
60+
}
61+
ret
5362
}
5463

5564
/// Inserts the given `key`/`value` pair into the map, returning the element that was
5665
/// previously stored at the given `key`, if one exists.
5766
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
58-
self.map.insert(key, value)
67+
let ret = self.map.insert(key.clone(), value);
68+
if ret.is_none() {
69+
assert!(self.keys.insert(key), "map and keys must be consistent");
70+
}
71+
ret
5972
}
6073

6174
/// Returns an [`Entry`] for the given `key` in the map, allowing access to the value.
6275
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
63-
match self.map.entry(key) {
64-
btree_map::Entry::Vacant(entry) => {
76+
match self.map.entry(key.clone()) {
77+
hash_map::Entry::Vacant(entry) => {
6578
Entry::Vacant(VacantEntry {
66-
underlying_entry: entry
79+
underlying_entry: entry,
80+
key,
81+
keys: &mut self.keys,
6782
})
6883
},
69-
btree_map::Entry::Occupied(entry) => {
84+
hash_map::Entry::Occupied(entry) => {
7085
Entry::Occupied(OccupiedEntry {
71-
underlying_entry: entry
86+
underlying_entry: entry,
87+
keys: &mut self.keys,
7288
})
7389
}
7490
}
@@ -91,8 +107,11 @@ impl<K: Ord, V> IndexedMap<K, V> {
91107
}
92108

93109
/// 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)
110+
pub fn range<R: RangeBounds<K>>(&self, range: R) -> Range<K, V> {
111+
Range {
112+
inner_range: self.keys.range(range),
113+
map: &self.map,
114+
}
96115
}
97116

98117
/// Returns the number of `key`/`value` pairs in the map
@@ -106,36 +125,62 @@ impl<K: Ord, V> IndexedMap<K, V> {
106125
}
107126
}
108127

128+
/// An iterator over a range of values in an [`IndexedMap`]
129+
pub struct Range<'a, K: Hash + Ord, V> {
130+
inner_range: btree_set::Range<'a, K>,
131+
map: &'a HashMap<K, V>,
132+
}
133+
impl<'a, K: Hash + Ord, V: 'a> Iterator for Range<'a, K, V> {
134+
type Item = (&'a K, &'a V);
135+
fn next(&mut self) -> Option<(&'a K, &'a V)> {
136+
self.inner_range.next().map(|k| {
137+
(k, self.map.get(k).expect("map and keys must be consistent"))
138+
})
139+
}
140+
}
141+
109142
/// 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>,
143+
pub struct VacantEntry<'a, K: Hash + Ord, V> {
144+
#[cfg(feature = "hashbrown")]
145+
underlying_entry: hash_map::VacantEntry<'a, K, V, hash_map::DefaultHashBuilder>,
146+
#[cfg(not(feature = "hashbrown"))]
147+
underlying_entry: hash_map::VacantEntry<'a, K, V>,
148+
key: K,
149+
keys: &'a mut BTreeSet<K>,
112150
}
113151

114152
/// 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>,
153+
pub struct OccupiedEntry<'a, K: Hash + Ord, V> {
154+
#[cfg(feature = "hashbrown")]
155+
underlying_entry: hash_map::OccupiedEntry<'a, K, V, hash_map::DefaultHashBuilder>,
156+
#[cfg(not(feature = "hashbrown"))]
157+
underlying_entry: hash_map::OccupiedEntry<'a, K, V>,
158+
keys: &'a mut BTreeSet<K>,
117159
}
118160

119161
/// A mutable reference to a position in the map. This can be used to reference, add, or update the
120162
/// value at a fixed key.
121-
pub enum Entry<'a, K: Ord, V> {
163+
pub enum Entry<'a, K: Hash + Ord, V> {
122164
/// A mutable reference to a position within the map where there is no value.
123165
Vacant(VacantEntry<'a, K, V>),
124166
/// A mutable reference to a position within the map where there is currently a value.
125167
Occupied(OccupiedEntry<'a, K, V>),
126168
}
127169

128-
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
170+
impl<'a, K: Hash + Ord, V> VacantEntry<'a, K, V> {
129171
/// Insert a value into the position described by this entry.
130172
pub fn insert(self, value: V) -> &'a mut V {
173+
assert!(self.keys.insert(self.key), "map and keys must be consistent");
131174
self.underlying_entry.insert(value)
132175
}
133176
}
134177

135-
impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
178+
impl<'a, K: Hash + Ord, V> OccupiedEntry<'a, K, V> {
136179
/// Remove the value at the position described by this entry.
137180
pub fn remove_entry(self) -> (K, V) {
138-
self.underlying_entry.remove_entry()
181+
let res = self.underlying_entry.remove_entry();
182+
assert!(self.keys.remove(&res.0), "map and keys must be consistent");
183+
res
139184
}
140185

141186
/// Get a reference to the value at the position described by this entry.

0 commit comments

Comments
 (0)