@@ -964,33 +964,24 @@ impl_writeable_tlv_based!(RouteHintHop, {
964
964
} ) ;
965
965
966
966
#[ derive( Eq , PartialEq ) ]
967
+ #[ repr( C ) ] // Force our field ordering to keep the size 64 bytes
967
968
struct RouteGraphNode {
968
969
node_id : NodeId ,
969
- lowest_fee_to_node : u64 ,
970
- total_cltv_delta : u32 ,
970
+ score : u64 ,
971
971
// The maximum value a yet-to-be-constructed payment path might flow through this node.
972
972
// This value is upper-bounded by us by:
973
973
// - how much is needed for a path being constructed
974
974
// - how much value can channels following this node (up to the destination) can contribute,
975
975
// considering their capacity and fees
976
976
value_contribution_msat : u64 ,
977
- /// The effective htlc_minimum_msat at this hop. If a later hop on the path had a higher HTLC
978
- /// minimum, we use it, plus the fees required at each earlier hop to meet it.
979
- path_htlc_minimum_msat : u64 ,
980
- /// All penalties incurred from this hop on the way to the destination, as calculated using
981
- /// channel scoring.
982
- path_penalty_msat : u64 ,
977
+ total_cltv_delta : u32 ,
983
978
/// The number of hops walked up to this node.
984
979
path_length_to_node : u8 ,
985
980
}
986
981
987
982
impl cmp:: Ord for RouteGraphNode {
988
983
fn cmp ( & self , other : & RouteGraphNode ) -> cmp:: Ordering {
989
- let other_score = cmp:: max ( other. lowest_fee_to_node , other. path_htlc_minimum_msat )
990
- . saturating_add ( other. path_penalty_msat ) ;
991
- let self_score = cmp:: max ( self . lowest_fee_to_node , self . path_htlc_minimum_msat )
992
- . saturating_add ( self . path_penalty_msat ) ;
993
- other_score. cmp ( & self_score) . then_with ( || other. node_id . cmp ( & self . node_id ) )
984
+ other. score . cmp ( & self . score ) . then_with ( || other. node_id . cmp ( & self . node_id ) )
994
985
}
995
986
}
996
987
@@ -1000,6 +991,16 @@ impl cmp::PartialOrd for RouteGraphNode {
1000
991
}
1001
992
}
1002
993
994
+ // While RouteGraphNode can be laid out as 56 bytes, performance appears to be improved
995
+ // substantially when it is laid out at exactly 64 bytes.
996
+ //
997
+ // Thus, we use `#[repr(C)]` on the struct to force a suboptimal layout and check that it stays 64
998
+ // bytes here.
999
+ #[ cfg( any( ldk_bench, not( any( test, fuzzing) ) ) ) ]
1000
+ const _GRAPH_NODE_SMALL: usize = 64 - core:: mem:: size_of :: < RouteGraphNode > ( ) ;
1001
+ #[ cfg( any( ldk_bench, not( any( test, fuzzing) ) ) ) ]
1002
+ const _GRAPH_NODE_FIXED_SIZE: usize = core:: mem:: size_of :: < RouteGraphNode > ( ) - 64 ;
1003
+
1003
1004
/// A wrapper around the various hop representations.
1004
1005
///
1005
1006
/// Can be used to examine the properties of a hop,
@@ -2117,15 +2118,6 @@ where L::Target: Logger {
2117
2118
score_params) ;
2118
2119
let path_penalty_msat = $next_hops_path_penalty_msat
2119
2120
. saturating_add( channel_penalty_msat) ;
2120
- let new_graph_node = RouteGraphNode {
2121
- node_id: src_node_id,
2122
- lowest_fee_to_node: total_fee_msat,
2123
- total_cltv_delta: hop_total_cltv_delta,
2124
- value_contribution_msat,
2125
- path_htlc_minimum_msat,
2126
- path_penalty_msat,
2127
- path_length_to_node,
2128
- } ;
2129
2121
2130
2122
// Update the way of reaching $candidate.source()
2131
2123
// with the given short_channel_id (from $candidate.target()),
@@ -2150,6 +2142,13 @@ where L::Target: Logger {
2150
2142
. saturating_add( path_penalty_msat) ;
2151
2143
2152
2144
if !old_entry. was_processed && new_cost < old_cost {
2145
+ let new_graph_node = RouteGraphNode {
2146
+ node_id: src_node_id,
2147
+ score: cmp:: max( total_fee_msat, path_htlc_minimum_msat) . saturating_add( path_penalty_msat) ,
2148
+ total_cltv_delta: hop_total_cltv_delta,
2149
+ value_contribution_msat,
2150
+ path_length_to_node,
2151
+ } ;
2153
2152
targets. push( new_graph_node) ;
2154
2153
old_entry. next_hops_fee_msat = $next_hops_fee_msat;
2155
2154
old_entry. hop_use_fee_msat = hop_use_fee_msat;
@@ -2223,18 +2222,26 @@ where L::Target: Logger {
2223
2222
// meaning how much will be paid in fees after this node (to the best of our knowledge).
2224
2223
// This data can later be helpful to optimize routing (pay lower fees).
2225
2224
macro_rules! add_entries_to_cheapest_to_target_node {
2226
- ( $node: expr, $node_id: expr, $fee_to_target_msat: expr, $next_hops_value_contribution: expr,
2227
- $next_hops_path_htlc_minimum_msat: expr, $next_hops_path_penalty_msat: expr,
2225
+ ( $node: expr, $node_id: expr, $next_hops_value_contribution: expr,
2228
2226
$next_hops_cltv_delta: expr, $next_hops_path_length: expr ) => {
2227
+ let fee_to_target_msat;
2228
+ let next_hops_path_htlc_minimum_msat;
2229
+ let next_hops_path_penalty_msat;
2229
2230
let skip_node = if let Some ( elem) = dist. get_mut( & $node_id) {
2230
2231
let was_processed = elem. was_processed;
2231
2232
elem. was_processed = true ;
2233
+ fee_to_target_msat = elem. total_fee_msat;
2234
+ next_hops_path_htlc_minimum_msat = elem. path_htlc_minimum_msat;
2235
+ next_hops_path_penalty_msat = elem. path_penalty_msat;
2232
2236
was_processed
2233
2237
} else {
2234
2238
// Entries are added to dist in add_entry!() when there is a channel from a node.
2235
2239
// Because there are no channels from payee, it will not have a dist entry at this point.
2236
2240
// If we're processing any other node, it is always be the result of a channel from it.
2237
2241
debug_assert_eq!( $node_id, maybe_dummy_payee_node_id) ;
2242
+ fee_to_target_msat = 0 ;
2243
+ next_hops_path_htlc_minimum_msat = 0 ;
2244
+ next_hops_path_penalty_msat = 0 ;
2238
2245
false
2239
2246
} ;
2240
2247
@@ -2244,9 +2251,9 @@ where L::Target: Logger {
2244
2251
let candidate = CandidateRouteHop :: FirstHop {
2245
2252
details, payer_node_id: & our_node_id,
2246
2253
} ;
2247
- add_entry!( & candidate, $ fee_to_target_msat,
2254
+ add_entry!( & candidate, fee_to_target_msat,
2248
2255
$next_hops_value_contribution,
2249
- $ next_hops_path_htlc_minimum_msat, $ next_hops_path_penalty_msat,
2256
+ next_hops_path_htlc_minimum_msat, next_hops_path_penalty_msat,
2250
2257
$next_hops_cltv_delta, $next_hops_path_length) ;
2251
2258
}
2252
2259
}
@@ -2269,10 +2276,10 @@ where L::Target: Logger {
2269
2276
short_channel_id: * chan_id,
2270
2277
} ;
2271
2278
add_entry!( & candidate,
2272
- $ fee_to_target_msat,
2279
+ fee_to_target_msat,
2273
2280
$next_hops_value_contribution,
2274
- $ next_hops_path_htlc_minimum_msat,
2275
- $ next_hops_path_penalty_msat,
2281
+ next_hops_path_htlc_minimum_msat,
2282
+ next_hops_path_penalty_msat,
2276
2283
$next_hops_cltv_delta, $next_hops_path_length) ;
2277
2284
}
2278
2285
}
@@ -2317,7 +2324,7 @@ where L::Target: Logger {
2317
2324
// If not, targets.pop() will not even let us enter the loop in step 2.
2318
2325
None => { } ,
2319
2326
Some ( node) => {
2320
- add_entries_to_cheapest_to_target_node ! ( node, payee, 0 , path_value_msat, 0 , 0u64 , 0 , 0 ) ;
2327
+ add_entries_to_cheapest_to_target_node ! ( node, payee, path_value_msat, 0 , 0 ) ;
2321
2328
} ,
2322
2329
} ) ;
2323
2330
@@ -2524,7 +2531,7 @@ where L::Target: Logger {
2524
2531
// Both these cases (and other cases except reaching recommended_value_msat) mean that
2525
2532
// paths_collection will be stopped because found_new_path==false.
2526
2533
// This is not necessarily a routing failure.
2527
- ' 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 ( ) {
2534
+ ' path_construction: while let Some ( RouteGraphNode { node_id, total_cltv_delta, mut value_contribution_msat, path_length_to_node, .. } ) = targets. pop ( ) {
2528
2535
2529
2536
// Since we're going payee-to-payer, hitting our node as a target means we should stop
2530
2537
// traversing the graph and arrange the path out of what we found.
@@ -2659,8 +2666,8 @@ where L::Target: Logger {
2659
2666
match network_nodes. get ( & node_id) {
2660
2667
None => { } ,
2661
2668
Some ( node) => {
2662
- add_entries_to_cheapest_to_target_node ! ( node, node_id, lowest_fee_to_node ,
2663
- value_contribution_msat, path_htlc_minimum_msat , path_penalty_msat ,
2669
+ add_entries_to_cheapest_to_target_node ! ( node, node_id,
2670
+ value_contribution_msat,
2664
2671
total_cltv_delta, path_length_to_node) ;
2665
2672
} ,
2666
2673
}
0 commit comments