Skip to content

Vec::pop() sometimes returns value inefficiently. #85365

Open
@human-0

Description

@human-0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`C-bugCategory: This is a bug.I-slowIssue: 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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions