Skip to content

Reduce bloat from smallvec! in the inline case #418

Description

@bolshoytoster

Currently, smallvec!'s inline case is handled by repeatedly pushing to a SmallVec. I've measured that this produces a lot of LLVM IR + bloat in the output binary.

I've managed to reduce the size in the inline case by using From<[T, M]>:

($($x:expr),+$(,)?) => ({
    $crate::SmallVec::from([$($x,)+])
});

(Note: for that to have an effect, From<[T, M]> needs to be marked #[inline] as well)

I haven't measured how this affects the heap case, but if it's a concern, I've found that something like this reduces the output to the same size, but is a little more verbose:

($($x:expr),+$(,)?) => ({
    const COUNT: usize = 0usize $(+ $crate::smallvec!(@one $x))+;
    let vec = $crate::SmallVec::new();
    if false {
        vec
    } else if COUNT <= vec.capacity() {
        core::mem::forget(vec);
        $crate::SmallVec::from_buf([$($x,)+])
    } else {
        core::mem::forget(vec);
        $crate::SmallVec::from_vec($crate::alloc::vec![$($x,)+])
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions