Skip to content

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

Open
@pickfire

Description

@pickfire

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

Metadata

Metadata

Assignees

Labels

A-collectionsArea: `std::collections`A-iteratorsArea: IteratorsC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchE-help-wantedCall for participation: Help is requested to fix this issue.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.I-heavyIssue: Problems and improvements with respect to binary size of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions