Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit reordering LeafNode fields #70675

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/liballoc/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub const MIN_LEN: usize = B - 1;
pub const CAPACITY: usize = 2 * B - 1;

/// The underlying representation of leaf nodes.
// The layout is fixed so that we have tighter control over field ordering.
// We're okay with wasting some memory in trade-off for better cache locality.
#[repr(C)]
struct LeafNode<K, V> {
/// We use `*const` as opposed to `*mut` so as to be covariant in `K` and `V`.
Expand All @@ -56,16 +58,19 @@ struct LeafNode<K, V> {
/// This is only guaranteed to be initialized when `parent` is non-null.
parent_idx: MaybeUninit<u16>,

/// The arrays storing the values of the node. Only the first `len` elements of each
/// array are initialized and valid.
vals: [MaybeUninit<V>; CAPACITY],

/// The number of keys and values this node stores.
///
/// This next to `parent_idx` to encourage the compiler to join `len` and
/// `parent_idx` into the same 32-bit word, reducing space overhead.
// This is right before the keys because we frequently access both.
len: u16,

/// The arrays storing the actual data of the node. Only the first `len` elements of each
/// array are initialized and valid.
// This is intentionally last so as to both be a) close to `len` and b)
// close to the edges in the internal node case.
keys: [MaybeUninit<K>; CAPACITY],
vals: [MaybeUninit<V>; CAPACITY],
}

impl<K, V> LeafNode<K, V> {
Expand Down