Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement From<[T; M]> for SmallVec<T, N> for all M, N (v2) #338

Merged
merged 2 commits into from Feb 19, 2024

Conversation

zachs18
Copy link
Contributor

@zachs18 zachs18 commented Feb 19, 2024

(First commit removes the unnecessary T: Clone bound from From<array> and From<Vec> impls that weren't there in v1.)

Allow using SmallVec::from(array) and array.into() like you can with Vec, even when array.len() != SmallVec::inline_capacity(). If the source length is longer than the smallvec's inline capacity, it will use heap-allocation, otherwise it will use inline capacity.

const-eval limitations

Ideally, the implementation would be as below to explicitly optimize for the three different cases even in debug mode, but it doesn't work because [T; M] and [T; N] are still considered different types.

if M > N {
  Self::from_vec(Vec::from(array))
} else if M == N {
  Self::from_buf(array) // error: expected `N`, found `M`
} else {
  // same as PR
}

However, with opt-level >= 1, Rust appears to reliably compile this PR to either a basically just memcpy (for M <= N) or an alloc and a memcpy (for M > N).

Backported to v2 as #339

@zachs18
Copy link
Contributor Author

zachs18 commented Feb 19, 2024

(force-push explanation: I was going to change this to use the same unconditional with_capacity(m) -> copy_nonoverlapping(..., m) as I did in the v1 backport, but the compiler was not able to reliably optimize it as aggressively as if M > N { Self::from(Vec::from(array)) } else ... at opt-level=1, so I changed it back to that.)

@zachs18 zachs18 changed the title Implement From<[T; M]> for SmallVec<T, N> for all M, N Implement From<[T; M]> for SmallVec<T, N> for all M, N (v2) Feb 19, 2024
Copy link
Collaborator

@mbrubeck mbrubeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mbrubeck mbrubeck added this pull request to the merge queue Feb 19, 2024
Merged via the queue into servo:v2 with commit 74075e3 Feb 19, 2024
6 checks passed
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.

None yet

2 participants