Skip to content

Tracking Issue for slice_to_boxed #82725

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

Closed
4 tasks
calebsander opened this issue Mar 3, 2021 · 2 comments
Closed
4 tasks

Tracking Issue for slice_to_boxed #82725

calebsander opened this issue Mar 3, 2021 · 2 comments
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@calebsander
Copy link
Contributor

calebsander commented Mar 3, 2021

Feature gate: #![feature(slice_to_boxed)]

This is a tracking issue for copying/cloning slices into boxed slices.

The current recommended approach for turning a &[T] where T: Clone into a Box<[T]> seems to be to use .to_vec().into_boxed_slice(). This misses some opportunities for optimization, since the Vec is allocated with a known length. A Godbolt comparison shows that Vec::into_boxed_slice() emits unnecessary calls to realloc() and free() and uses some extra registers for local variables: https://godbolt.org/z/eq7Pc7.

There is currently an impl<T: Copy> From<&[T]> for Box<[T]>, but not for T: Clone like there is for Vec<T>.

We propose adding a function on slices to clone/copy them into boxed slices and relaxing the From<&[T]> bound to T: Clone for Box<[T]>. Both of these should be specialized to a memcpy() for the common case of T: Copy. This impl bound change would be insta-stable, so it should be delayed until we are ready to stabilize the rest of this feature.

Public API

impl<T: Clone> [T] {
  pub fn to_boxed_slice(&self) -> Box<Self>;
  pub fn to_boxed_slice_in<A: Allocator>(&self, alloc: A) -> Box<Self, A>;
}

// Bounds relaxed from T: Copy
impl<T: Clone> From<&[T]> for Box<[T]> { /* ... */ }

// Bounds relaxed from T: Copy
impl<T: Clone> From<Cow<'_, [T]>> for Box<[T]> { /* ... */ }

Steps / History

Unresolved Questions

  • None yet.
@calebsander calebsander added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Mar 3, 2021
@leonardo-m
Copy link

See also #80752

@yaahc
Copy link
Member

yaahc commented Jun 1, 2022

It looks like the PR for this tracking issue never made it into master since it ran into some perf issues, so for now we're going to close this tracking issue. Anyone interested in reviving this proposal should file a PR, work through the issues, and then file a tracking issue once those have been resolved.

@yaahc yaahc closed this as completed Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants