Any advantage of Box<[[[u64; 16]; 16]; 16]> over Vec<u64>?

Well, remember you can always create such a box via a vector.

For example, you could do this:

pub fn demo() -> Box<[[[u64; 16]; 16]; 16]> {
    let v = vec![Default::default(); 16];
    v.into_boxed_slice().try_into().unwrap()
}

Which you can see on godbolt doesn't put anything on the stack, and only fails if the allocation fails. It's just __rust_alloc+memset:

example::demo:
        push    rbx
        mov     edi, 32768
        mov     esi, 8
        call    qword ptr [rip + __rust_alloc@GOTPCREL]
        test    rax, rax
        je      .LBB0_1
        mov     rbx, rax
        mov     edx, 32768
        mov     rdi, rax
        xor     esi, esi
        call    qword ptr [rip + memset@GOTPCREL]
        mov     rax, rbx
        pop     rbx
        ret
.LBB0_1:
        mov     edi, 32768
        mov     esi, 8
        call    qword ptr [rip + alloc::alloc::handle_alloc_error@GOTPCREL]
        ud2

EDIT: And here's a PR so it can be just __rust_alloc_zeroed instead of the separate memset: https://github.com/rust-lang/rust/pull/95362

4 Likes