Skip to content

Missed optimization with reserve and extend_from_slice #123944

Open
@SUPERCILEX

Description

@SUPERCILEX

I tried this code:

pub fn zzz(v: &mut Vec<u8>, s: &[u8]) {
    v.reserve(s.len());
    v.extend_from_slice(s);
}

I expected to see this happen: only one do_reserve_and_handle call.

Instead, this happened: extend_from_slice stills checks the capacity and calls reserve. It seems like LLVM assumes aren't able to bubble up out of multiple functions. I was able to get the optimization to work in #119465 but it required putting assumes in every function along the reserve chain.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler 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