Skip to content

Missed optimization: repeated pointer increments don't compile to a memcpy #69187

Closed
@timvermeulen

Description

@timvermeulen
pub unsafe fn copy(slice: &[u8], dst: *mut u8) {
    let mut src = slice.as_ptr();
    let mut dst = dst;

    for _ in 0..slice.len() {
        *dst = *src;
        src = src.add(1);
        dst = dst.add(1);
    }
}

This doesn't currently compile to a memcpy (Godbolt).

I figured this was worth filing because this seems to be the main reason that a naive iter::Zip implementation isn't optimal, which is why it's implemented the way it is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.T-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