Skip to content

Commit 3654954

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 c8653de commit 3654954

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
@@ -1164,12 +1164,7 @@ impl cmp::PartialOrd for RouteGraphNode {
11641164

11651165
// While RouteGraphNode can be laid out with fewer bytes, performance appears to be improved
11661166
// substantially when it is laid out at exactly 64 bytes.
1167-
//
1168-
// Thus, we use `#[repr(C)]` on the struct to force a suboptimal layout and check that it stays 64
1169-
// bytes here.
1170-
#[cfg(any(ldk_bench, not(any(test, fuzzing))))]
11711167
const _GRAPH_NODE_SMALL: usize = 64 - core::mem::size_of::<RouteGraphNode>();
1172-
#[cfg(any(ldk_bench, not(any(test, fuzzing))))]
11731168
const _GRAPH_NODE_FIXED_SIZE: usize = core::mem::size_of::<RouteGraphNode>() - 64;
11741169

11751170
/// A [`CandidateRouteHop::FirstHop`] entry.
@@ -1673,7 +1668,7 @@ fn iter_equal<I1: Iterator, I2: Iterator>(mut iter_a: I1, mut iter_b: I2)
16731668
/// Fee values should be updated only in the context of the whole path, see update_value_and_recompute_fees.
16741669
/// These fee values are useful to choose hops as we traverse the graph "payee-to-payer".
16751670
#[derive(Clone)]
1676-
#[repr(C)] // Force fields to appear in the order we define them.
1671+
#[repr(align(128))]
16771672
struct PathBuildingHop<'a> {
16781673
candidate: CandidateRouteHop<'a>,
16791674
/// If we've already processed a node as the best node, we shouldn't process it again. Normally
@@ -1694,11 +1689,6 @@ struct PathBuildingHop<'a> {
16941689
/// channel scoring.
16951690
path_penalty_msat: u64,
16961691

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

17041694
/// All the fees paid *after* this channel on the way to the destination
@@ -1715,17 +1705,8 @@ struct PathBuildingHop<'a> {
17151705
value_contribution_msat: u64,
17161706
}
17171707

1718-
// Checks that the entries in the `find_route` `dist` map fit in (exactly) two standard x86-64
1719-
// cache lines. Sadly, they're not guaranteed to actually lie on a cache line (and in fact,
1720-
// generally won't, because at least glibc's malloc will align to a nice, big, round
1721-
// boundary...plus 16), but at least it will reduce the amount of data we'll need to load.
1722-
//
1723-
// Note that these assertions only pass on somewhat recent rustc, and thus are gated on the
1724-
// ldk_bench flag.
1725-
#[cfg(ldk_bench)]
1726-
const _NODE_MAP_SIZE_TWO_CACHE_LINES: usize = 128 - core::mem::size_of::<(NodeId, PathBuildingHop)>();
1727-
#[cfg(ldk_bench)]
1728-
const _NODE_MAP_SIZE_EXACTLY_CACHE_LINES: usize = core::mem::size_of::<(NodeId, PathBuildingHop)>() - 128;
1708+
const _NODE_MAP_SIZE_TWO_CACHE_LINES: usize = 128 - core::mem::size_of::<Option<PathBuildingHop>>();
1709+
const _NODE_MAP_SIZE_EXACTLY_TWO_CACHE_LINES: usize = core::mem::size_of::<Option<PathBuildingHop>>() - 128;
17291710

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

0 commit comments

Comments
 (0)