Skip to content

Reduce stack footprint by using core::mem::transmute_copy #160

@maxdexh

Description

@maxdexh

The stack footprint of several functions can be improved by using core::mem::transmute_copy on rust versions starting at 1.83 (using e.g. the rustversion crate).

For example, const_transmute can be changed to

pub const unsafe fn const_transmute<A, B>(a: A) -> B {
    core::mem::transmute_copy(&core::mem::ManuallyDrop::new(a))
}

With this, assume_init can also be changed to just defer to const_transmute (which is a valid way of performing MaybeUninit::assume_init)

impl<T, N: ArrayLength> GenericArray<T, N> {
    pub const unsafe fn assume_init(array: GenericArray<MaybeUninit<T>, N>) -> Self {
        const_transmute(array)
    }
}

This is more stack-friendly than even the current implementation of IntrusiveArrayBuilder::array_assume_init, though that one can also be improved further by using transmute_copy(&array) directly, since GenericArray<MaybeUninit<T>, N> is known to not have any drop glue.

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