Skip to content

Better memory layout by moving len before xs #254

Closed
@JakkuSakura

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

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