Skip to content

Suboptimal generated code for testing that a &[u8; 4] contains all zeros #71602

Closed
@dbdr

Description

@dbdr

I expected comparing a [u8; 4] to [0; 4] to generate code identical to testing a u32 is 0, and indeed it is the case when comparing values. However when dereferencing is involved the code is longer:

pub fn test_u32_ref(data: &u32) -> bool {
    *data == 0
}

pub fn test_u8array_ref(data: &[u8; 4]) -> bool {
    *data == [0; 4]
}

gives:

example::test_u32_ref:
        cmp     dword ptr [rdi], 0
        sete    al
        ret

example::test_u8array_ref:
        lea     rax, [rip + .L__unnamed_1]
        cmp     rdi, rax
        je      .LBB1_1
        cmp     dword ptr [rdi], 0
        sete    al
        ret
.LBB1_1:
        mov     al, 1
        ret

.L__unnamed_1:
        .zero   4

https://godbolt.org/z/3GaUfy

Transmuting to u32 gives the optimized code:

pub fn test_u8array_ref_transmute(data: &[u8; 4]) -> bool {
    let data = unsafe { std::mem::transmute::<[u8; 4], u32>(*data) };
    data == 0
}

This issue has been assigned to @samrat via this comment.

Metadata

Metadata

Assignees

Labels

C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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.T-libs-apiRelevant to the library API 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