-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-collectionsArea: `std::collections`Area: `std::collections`C-bugCategory: This is a bug.Category: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
I tried this code:
#[repr(align(32))]
#[derive(Copy, Clone)]
pub struct Data {
u64s: [u64; 4],
options: Option<(u64, u8, Option<u8>)>
}
pub fn len_minus_one(mut vec: Vec<Data>, value: &mut Data) {
*value = vec[vec.len() - 1];
vec.pop();
}
pub fn pop(mut vec: Vec<Data>, value: &mut Data) {
*value = vec.pop().unwrap();
}
I expected to see this happen: The two functions generate similar assembly.
Instead, this happened: Vec::pop()
uses far less efficient copying.
Meta
rustc --version --verbose
:
<version>
binary: rustc
commit-hash: 88f19c6dab716c6281af7602e30f413e809c5974
commit-date: 2021-05-03
host: x86_64-pc-windows-msvc
release: 1.52.0
LLVM version: 12.0.0
It also happens on Nightly, although the generated assembly is much better.
Edit: It appears to be related to an Option
holding the value, because the same behaviour is observed when using vec.get(vec.len() - 1).copied().unwrap()
but not *vec.get(vec.len() - 1).unwrap()
.
Playground Link
leonardo-mSoveu
Metadata
Metadata
Assignees
Labels
A-collectionsArea: `std::collections`Area: `std::collections`C-bugCategory: This is a bug.Category: This is a bug.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.