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