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

Better codegen for FromIterator Box<[T]> #75636

Open
pickfire opened this issue Aug 17, 2020 · 3 comments
Open

Better codegen for FromIterator Box<[T]> #75636

pickfire opened this issue Aug 17, 2020 · 3 comments
Assignees
Labels
A-collections Area: `std::collection` A-iterators Area: Iterators E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. I-heavy Issue: Problems and improvements with respect to binary size of generated code. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@pickfire
Copy link
Contributor

pickfire commented Aug 17, 2020

Box<[T]> requires 2x assembly (96 -> 185) compared to Vec<T>. I think this could be improved.

pub fn t<'a>(iter: &'a [&str]) -> Vec<&'a str> {
    iter.iter().cloned().collect()
}
pub fn t<'a>(iter: &'a [&str]) -> Box<[&'a str]> {
    iter.iter().cloned().collect()
}

Looking at the generated code, looks like some extra unnecessary checks were added.

hmm, some panic handling for "Tried to shrink to a larger capacity"
(which should be statically unreachable)
Is that even possible?
no, but I guess it isn't seeing that len<=capacity
@cuviper

https://godbolt.org/z/c9ffTT

However, this does not affect the following.

pub fn t<'a>() -> Box<[&'a str]> {
    ["a", "b"].iter().cloned().collect()
}

I will be taking a look at this in the meantime while looking at join for Iterator. I wish an assert could solve this.

CC @lzutao @cuviper

@leonardo-m
Copy link

I opened multiple threads on related topics, like:
https://internals.rust-lang.org/t/more-efficient-boxed-slice-creation/12271

@LeSeulArtichaut LeSeulArtichaut added I-heavy Issue: Problems and improvements with respect to binary size of generated code. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 18, 2020
@cuviper
Copy link
Member

cuviper commented Aug 18, 2020

#75677 removes the panic, "Tried to shrink to a larger capacity". However, I think this issue might be further improved if the compiler could also see that it will never need a realloc, if we specialize TrustedLen iterators to reserve the exact capacity in the first place.

bors added a commit to rust-lang-ci/rust that referenced this issue Aug 19, 2020
Don't panic in Vec::shrink_to_fit

We can help the compiler see that `Vec::shrink_to_fit` will never reach the panic case in `RawVec::shrink_to_fit`, just by guarding the call only for cases where the capacity is strictly greater. A capacity less than the length is only possible through an unsafe call to `set_len`, which would break the `Vec` invariants, so `shrink_to_fit` can just ignore that.

This removes the panicking code from the examples in both rust-lang#71861 and rust-lang#75636.
@yaahc yaahc added E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. labels Jan 26, 2022
@the8472 the8472 added A-collections Area: `std::collection` A-iterators Area: Iterators labels Feb 8, 2022
@GoldsteinE
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: `std::collection` A-iterators Area: Iterators E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. I-heavy Issue: Problems and improvements with respect to binary size of generated code. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants