Skip to content

Shrink code produced by smallvec![].#386

Merged
mbrubeck merged 1 commit intoservo:v1from
nnethercote:v1-smallvec-0
Jun 6, 2025
Merged

Shrink code produced by smallvec![].#386
mbrubeck merged 1 commit intoservo:v1from
nnethercote:v1-smallvec-0

Conversation

@nnethercote
Copy link
Contributor

This is the backport of #385 from v2 to v1.

Currently `smallvec![]` expands to this:
```
{
    let count = 0usize;
    #[allow(unused_mut)]
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.capacity() {
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::vec::Vec::new())
    }
};
```
This commit adds a rule to the `smallvec!` macro for the zero-length
case so it instead expands to this:
```
::smallvec::SmallVec::new()
```
The `std::vec!` macro already has a similar special case.

This commit also improves the non-zero case.
- It removes the `#[allow(unused_mut)]`, which was only needed for the
  zero-length case.
- It changes the `*` repetitions to `+`. (Again, like `std::vec`.)
@mbrubeck mbrubeck merged commit d682405 into servo:v1 Jun 6, 2025
7 checks passed
@nnethercote nnethercote deleted the v1-smallvec-0 branch June 6, 2025 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants