Closed
Description
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
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
Category: An issue proposing an enhancement or a PR with one.Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.Issue: Problems and improvements with respect to performance of generated code.Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.