Skip to content

Commit 231a5e7

Browse files
committed
Pre-calculate heap element scores (retaining RouteGraphNode size)
`RouteGraphNode` currently recalculates scores in its `Ord` implementation, wasting time while sorting the main Dijkstra's heap. Further, some time ago, when implementing the `htlc_maximum_msat` amount reduction while walking the graph, we added `PathBuildingHop::was_processed`, looking up the source node in `dist` each time we pop'ed an element off of the binary heap. As a result, we now have a reference to our `PathBuildingHop` when processing a best-node's channels, leading to several fields in `RouteGraphNode` being entirely redundant. Here we drop those fields, but add a pre-calculated score field, as well as force a suboptimal `RouteGraphNode` layout, retaining its existing 64 byte size. Without the suboptimal layout, performance is very mixed, but with it performance is mostly improved, by around 10% in most tests.
1 parent aa4785c commit 231a5e7

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

lightning/src/routing/router.rs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -967,33 +967,24 @@ impl_writeable_tlv_based!(RouteHintHop, {
967967
});
968968

969969
#[derive(Eq, PartialEq)]
970+
#[repr(C)] // Force our field ordering to keep the size 64 bytes
970971
struct RouteGraphNode {
971972
node_id: NodeId,
972-
lowest_fee_to_node: u64,
973-
total_cltv_delta: u32,
973+
score: u64,
974974
// The maximum value a yet-to-be-constructed payment path might flow through this node.
975975
// This value is upper-bounded by us by:
976976
// - how much is needed for a path being constructed
977977
// - how much value can channels following this node (up to the destination) can contribute,
978978
// considering their capacity and fees
979979
value_contribution_msat: u64,
980-
/// The effective htlc_minimum_msat at this hop. If a later hop on the path had a higher HTLC
981-
/// minimum, we use it, plus the fees required at each earlier hop to meet it.
982-
path_htlc_minimum_msat: u64,
983-
/// All penalties incurred from this hop on the way to the destination, as calculated using
984-
/// channel scoring.
985-
path_penalty_msat: u64,
980+
total_cltv_delta: u32,
986981
/// The number of hops walked up to this node.
987982
path_length_to_node: u8,
988983
}
989984

990985
impl cmp::Ord for RouteGraphNode {
991986
fn cmp(&self, other: &RouteGraphNode) -> cmp::Ordering {
992-
let other_score = cmp::max(other.lowest_fee_to_node, other.path_htlc_minimum_msat)
993-
.saturating_add(other.path_penalty_msat);
994-
let self_score = cmp::max(self.lowest_fee_to_node, self.path_htlc_minimum_msat)
995-
.saturating_add(self.path_penalty_msat);
996-
other_score.cmp(&self_score).then_with(|| other.node_id.cmp(&self.node_id))
987+
other.score.cmp(&self.score).then_with(|| other.node_id.cmp(&self.node_id))
997988
}
998989
}
999990

@@ -1003,6 +994,16 @@ impl cmp::PartialOrd for RouteGraphNode {
1003994
}
1004995
}
1005996

997+
// While RouteGraphNode can be laid out as 56 bytes, performance appears to be improved
998+
// substantially when it is laid out at exactly 64 bytes.
999+
//
1000+
// Thus, we use `#[repr(C)]` on the struct to force a suboptimal layout and check that it stays 64
1001+
// bytes here.
1002+
#[cfg(any(ldk_bench, not(any(test, fuzzing))))]
1003+
const _GRAPH_NODE_SMALL: usize = 64 - core::mem::size_of::<RouteGraphNode>();
1004+
#[cfg(any(ldk_bench, not(any(test, fuzzing))))]
1005+
const _GRAPH_NODE_FIXED_SIZE: usize = core::mem::size_of::<RouteGraphNode>() - 64;
1006+
10061007
/// A wrapper around the various hop representations.
10071008
///
10081009
/// Can be used to examine the properties of a hop,
@@ -2119,15 +2120,6 @@ where L::Target: Logger {
21192120
score_params);
21202121
let path_penalty_msat = $next_hops_path_penalty_msat
21212122
.saturating_add(channel_penalty_msat);
2122-
let new_graph_node = RouteGraphNode {
2123-
node_id: src_node_id,
2124-
lowest_fee_to_node: total_fee_msat,
2125-
total_cltv_delta: hop_total_cltv_delta,
2126-
value_contribution_msat,
2127-
path_htlc_minimum_msat,
2128-
path_penalty_msat,
2129-
path_length_to_node,
2130-
};
21312123

21322124
// Update the way of reaching $candidate.source()
21332125
// with the given short_channel_id (from $candidate.target()),
@@ -2152,6 +2144,13 @@ where L::Target: Logger {
21522144
.saturating_add(path_penalty_msat);
21532145

21542146
if !old_entry.was_processed && new_cost < old_cost {
2147+
let new_graph_node = RouteGraphNode {
2148+
node_id: src_node_id,
2149+
score: cmp::max(total_fee_msat, path_htlc_minimum_msat).saturating_add(path_penalty_msat),
2150+
total_cltv_delta: hop_total_cltv_delta,
2151+
value_contribution_msat,
2152+
path_length_to_node,
2153+
};
21552154
targets.push(new_graph_node);
21562155
old_entry.next_hops_fee_msat = $next_hops_fee_msat;
21572156
old_entry.hop_use_fee_msat = hop_use_fee_msat;
@@ -2225,18 +2224,26 @@ where L::Target: Logger {
22252224
// meaning how much will be paid in fees after this node (to the best of our knowledge).
22262225
// This data can later be helpful to optimize routing (pay lower fees).
22272226
macro_rules! add_entries_to_cheapest_to_target_node {
2228-
( $node: expr, $node_id: expr, $fee_to_target_msat: expr, $next_hops_value_contribution: expr,
2229-
$next_hops_path_htlc_minimum_msat: expr, $next_hops_path_penalty_msat: expr,
2227+
( $node: expr, $node_id: expr, $next_hops_value_contribution: expr,
22302228
$next_hops_cltv_delta: expr, $next_hops_path_length: expr ) => {
2229+
let fee_to_target_msat;
2230+
let next_hops_path_htlc_minimum_msat;
2231+
let next_hops_path_penalty_msat;
22312232
let skip_node = if let Some(elem) = dist.get_mut(&$node_id) {
22322233
let was_processed = elem.was_processed;
22332234
elem.was_processed = true;
2235+
fee_to_target_msat = elem.total_fee_msat;
2236+
next_hops_path_htlc_minimum_msat = elem.path_htlc_minimum_msat;
2237+
next_hops_path_penalty_msat = elem.path_penalty_msat;
22342238
was_processed
22352239
} else {
22362240
// Entries are added to dist in add_entry!() when there is a channel from a node.
22372241
// Because there are no channels from payee, it will not have a dist entry at this point.
22382242
// If we're processing any other node, it is always be the result of a channel from it.
22392243
debug_assert_eq!($node_id, maybe_dummy_payee_node_id);
2244+
fee_to_target_msat = 0;
2245+
next_hops_path_htlc_minimum_msat = 0;
2246+
next_hops_path_penalty_msat = 0;
22402247
false
22412248
};
22422249

@@ -2246,9 +2253,9 @@ where L::Target: Logger {
22462253
let candidate = CandidateRouteHop::FirstHop {
22472254
details, payer_node_id: &our_node_id,
22482255
};
2249-
add_entry!(&candidate, $fee_to_target_msat,
2256+
add_entry!(&candidate, fee_to_target_msat,
22502257
$next_hops_value_contribution,
2251-
$next_hops_path_htlc_minimum_msat, $next_hops_path_penalty_msat,
2258+
next_hops_path_htlc_minimum_msat, next_hops_path_penalty_msat,
22522259
$next_hops_cltv_delta, $next_hops_path_length);
22532260
}
22542261
}
@@ -2271,10 +2278,10 @@ where L::Target: Logger {
22712278
short_channel_id: *chan_id,
22722279
};
22732280
add_entry!(&candidate,
2274-
$fee_to_target_msat,
2281+
fee_to_target_msat,
22752282
$next_hops_value_contribution,
2276-
$next_hops_path_htlc_minimum_msat,
2277-
$next_hops_path_penalty_msat,
2283+
next_hops_path_htlc_minimum_msat,
2284+
next_hops_path_penalty_msat,
22782285
$next_hops_cltv_delta, $next_hops_path_length);
22792286
}
22802287
}
@@ -2319,7 +2326,7 @@ where L::Target: Logger {
23192326
// If not, targets.pop() will not even let us enter the loop in step 2.
23202327
None => {},
23212328
Some(node) => {
2322-
add_entries_to_cheapest_to_target_node!(node, payee, 0, path_value_msat, 0, 0u64, 0, 0);
2329+
add_entries_to_cheapest_to_target_node!(node, payee, path_value_msat, 0, 0);
23232330
},
23242331
});
23252332

@@ -2526,7 +2533,7 @@ where L::Target: Logger {
25262533
// Both these cases (and other cases except reaching recommended_value_msat) mean that
25272534
// paths_collection will be stopped because found_new_path==false.
25282535
// This is not necessarily a routing failure.
2529-
'path_construction: while let Some(RouteGraphNode { node_id, lowest_fee_to_node, total_cltv_delta, mut value_contribution_msat, path_htlc_minimum_msat, path_penalty_msat, path_length_to_node, .. }) = targets.pop() {
2536+
'path_construction: while let Some(RouteGraphNode { node_id, total_cltv_delta, mut value_contribution_msat, path_length_to_node, .. }) = targets.pop() {
25302537

25312538
// Since we're going payee-to-payer, hitting our node as a target means we should stop
25322539
// traversing the graph and arrange the path out of what we found.
@@ -2661,8 +2668,8 @@ where L::Target: Logger {
26612668
match network_nodes.get(&node_id) {
26622669
None => {},
26632670
Some(node) => {
2664-
add_entries_to_cheapest_to_target_node!(node, node_id, lowest_fee_to_node,
2665-
value_contribution_msat, path_htlc_minimum_msat, path_penalty_msat,
2671+
add_entries_to_cheapest_to_target_node!(node, node_id,
2672+
value_contribution_msat,
26662673
total_cltv_delta, path_length_to_node);
26672674
},
26682675
}

0 commit comments

Comments
 (0)