Skip to content

Asserts the maximum value that can be returned from Vec::len#131193

Merged
bors merged 2 commits intorust-lang:masterfrom
EFanZh:asserts-vec-len
Dec 22, 2024
Merged

Asserts the maximum value that can be returned from Vec::len#131193
bors merged 2 commits intorust-lang:masterfrom
EFanZh:asserts-vec-len

Conversation

@EFanZh
Copy link
Contributor

@EFanZh EFanZh commented Oct 3, 2024

Currently, casting Vec<i32> to Vec<u32> takes O(1) time:

// See <https://godbolt.org/z/hxq3hnYKG> for assembly output.
pub fn cast(vec: Vec<i32>) -> Vec<u32> {
    vec.into_iter().map(|e| e as _).collect()
}

But the generated assembly is not the same as the identity function, which prevents us from casting Vec<Vec<i32>> to Vec<Vec<u32>> within O(1) time:

// See <https://godbolt.org/z/7n48bxd9f> for assembly output.
pub fn cast(vec: Vec<Vec<i32>>) -> Vec<Vec<u32>> {
    vec.into_iter()
        .map(|e| e.into_iter().map(|e| e as _).collect())
        .collect()
}

This change tries to fix the problem. You can see the comparison here: https://godbolt.org/z/jdManrKvx.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.