Julia alignas: is there a way to specify the alignment of Julia objects in memory?

Array alignments:

#define JL_SMALL_BYTE_ALIGNMENT 16
#define JL_CACHE_BYTE_ALIGNMENT 64

Array size threshold:

// how much space we're willing to waste if an array outgrows its
// original object
#define ARRAY_INLINE_NBYTES (2048*sizeof(void*))

Array allocation. I didn’t look too closely, but calls to functions like jl_alloc_array_1d forward to _new_array_ eventually, which branches off ARRAY_INLINE_NBYTES, and calls JL_ARRAY_ALIGN to “align whole object” with either JL_SMALL_BYTE_ALIGNMENT or JL_CACHE_BYTE_ALIGNMENT.
For Float64, note that

julia> 256 * sizeof(Float64)
2048

Experimentally, the threshold seems to be at Vector{Float64}(undef, 245) and larger are always aligned to 64 bytes.

julia> any((reinterpret(UInt, pointer(Vector{Float64}(undef, 245))) % 64) ≠ zero(UInt) for i ∈ 1:1000)
false

julia> any((reinterpret(UInt, pointer(Vector{Float64}(undef, 244))) % 64) ≠ zero(UInt) for i ∈ 1:1000)
true
5 Likes