Skip to content

Better memory layout by moving len before xs #254

Description

@JakkuSakura

Hi, I noticed that len is arranged after xs

pub struct ArrayVec<T, const CAP: usize> {
    // the `len` first elements of the array are initialized
    xs: [MaybeUninit<T>; CAP],
    len: LenUint,
}

In C's world, it's a convention to move length before an array, like

#[repr(C)]
pub struct ArrayVec<T, const CAP: usize> {
    len: LenUint,
    // the `len` first elements of the array are initialized
    xs: [MaybeUninit<T>; CAP]
}

This provides a few nice features:

  • better locality. First, read the length, then read the element one by one. This should be trivial though
  • Easier to implement tail arrays if were to put on the heap
  • https://github.com/antirez/sds Simply Dynamic String uses a similar layout

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions