Skip to content

Commit edf9c82

Browse files
committed
Align PathBuildingHop to 128b, now that we store them in a Vec
Now that `PathBuildingHop` is stored in a `Vec` (as `Option`s), rather than `HashMap` entries, they can grow to fill a full two cache lines without a memory access performance cost. In the next commit we'll take advantage of this somewhat, but here we update the assertions and drop the `repr(C)`, allowing rust to lay the memory out as it wishes.
1 parent 2ec5837 commit edf9c82

File tree

1 file changed

+3
-22
lines changed

1 file changed

+3
-22
lines changed

lightning/src/routing/router.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,12 +1126,7 @@ impl cmp::PartialOrd for RouteGraphNode {
11261126

11271127
// While RouteGraphNode can be laid out with fewer bytes, performance appears to be improved
11281128
// substantially when it is laid out at exactly 64 bytes.
1129-
//
1130-
// Thus, we use `#[repr(C)]` on the struct to force a suboptimal layout and check that it stays 64
1131-
// bytes here.
1132-
#[cfg(any(ldk_bench, not(any(test, fuzzing))))]
11331129
const _GRAPH_NODE_SMALL: usize = 64 - core::mem::size_of::<RouteGraphNode>();
1134-
#[cfg(any(ldk_bench, not(any(test, fuzzing))))]
11351130
const _GRAPH_NODE_FIXED_SIZE: usize = core::mem::size_of::<RouteGraphNode>() - 64;
11361131

11371132
/// A [`CandidateRouteHop::FirstHop`] entry.
@@ -1532,7 +1527,7 @@ fn iter_equal<I1: Iterator, I2: Iterator>(mut iter_a: I1, mut iter_b: I2)
15321527
/// Fee values should be updated only in the context of the whole path, see update_value_and_recompute_fees.
15331528
/// These fee values are useful to choose hops as we traverse the graph "payee-to-payer".
15341529
#[derive(Clone)]
1535-
#[repr(C)] // Force fields to appear in the order we define them.
1530+
#[repr(align(128))]
15361531
struct PathBuildingHop<'a> {
15371532
candidate: CandidateRouteHop<'a>,
15381533
target_node_counter: Option<u32>,
@@ -1562,11 +1557,6 @@ struct PathBuildingHop<'a> {
15621557
/// channel scoring.
15631558
path_penalty_msat: u64,
15641559

1565-
// The last 16 bytes are on the next cache line by default in glibc's malloc. Thus, we should
1566-
// only place fields which are not hot there. Luckily, the next three fields are only read if
1567-
// we end up on the selected path, and only in the final path layout phase, so we don't care
1568-
// too much if reading them is slow.
1569-
15701560
fee_msat: u64,
15711561

15721562
/// All the fees paid *after* this channel on the way to the destination
@@ -1583,17 +1573,8 @@ struct PathBuildingHop<'a> {
15831573
value_contribution_msat: u64,
15841574
}
15851575

1586-
// Checks that the entries in the `find_route` `dist` map fit in (exactly) two standard x86-64
1587-
// cache lines. Sadly, they're not guaranteed to actually lie on a cache line (and in fact,
1588-
// generally won't, because at least glibc's malloc will align to a nice, big, round
1589-
// boundary...plus 16), but at least it will reduce the amount of data we'll need to load.
1590-
//
1591-
// Note that these assertions only pass on somewhat recent rustc, and thus are gated on the
1592-
// ldk_bench flag.
1593-
#[cfg(ldk_bench)]
1594-
const _NODE_MAP_SIZE_TWO_CACHE_LINES: usize = 128 - core::mem::size_of::<(NodeId, PathBuildingHop)>();
1595-
#[cfg(ldk_bench)]
1596-
const _NODE_MAP_SIZE_EXACTLY_CACHE_LINES: usize = core::mem::size_of::<(NodeId, PathBuildingHop)>() - 128;
1576+
const _NODE_MAP_SIZE_TWO_CACHE_LINES: usize = 128 - core::mem::size_of::<Option<PathBuildingHop>>();
1577+
const _NODE_MAP_SIZE_EXACTLY_TWO_CACHE_LINES: usize = core::mem::size_of::<Option<PathBuildingHop>>() - 128;
15971578

15981579
impl<'a> core::fmt::Debug for PathBuildingHop<'a> {
15991580
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {

0 commit comments

Comments
 (0)