Skip to content

memcpy-style copies of small fixed length arrays become memcpy, unless done via a loop with ops::Index #92993

Open
@saethlin

Description

@saethlin

godbolt demo

I have a [u8; 4] and I want to copy an unknown number of bytes from it into a &mut [u8]. Based on benchmarking, on x86_64 at least it is much slower to call memcpy than it is to do a byte-by-byte copy. The fastest implementation of this pattern in Rust is this:

if len > 4 {
    core::hint::unreachable_unchecked();
}
for i in 0..len {
    *dst.get_unchecked_mut(i) = src[i];
}

That just doesn't seem right to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-arrayArea: `[T; N]`A-codegenArea: Code generationC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions