A Discourse post by foobar_v2 mentioned an important fact I've never considered: Memory access has more indirections when accessed through a Memory as opposed to a MemoryRef:
- Memory
- Load the address of the
Memory struct (mutable struct, so has its own address)
- The memory struct contains a pointer to the buffer referred to by the
Memory struct. Load the value from here
- MemoryRef
- Load the value directly from the pointer in
MemoryRef without indirection
Therefore, a FixedSizeArray wrapping a MemoryView is more efficient than one wrapping a Memory, as it elides one pointer chase:
julia> using FixedSizeArrays, MemoryViews;
julia> v = [1,2,3];
julia> A = FixedSizeArray(v);
julia> B = FixedSizeArrays.new_fixed_size_array(MemoryView(v), (3,));
julia> f(x) = @inbounds x[3];
julia> @code_native debuginfo=:none dump_module=false f(A)
.section __TEXT,__text,regular,pure_instructions
ldr x8, [x1]
ldr x9, [x20, #16]
ldr x9, [x9, #16]
ldr xzr, [x9]
ldr x8, [x8, #8]
ldr x0, [x8, #16]
ret
julia> @code_native debuginfo=:none dump_module=false f(B)
.section __TEXT,__text,regular,pure_instructions
ldr x8, [x20, #16]
ldr x8, [x8, #16]
ldr xzr, [x8]
ldr x8, [x0]
ldr x0, [x8, #16]
ret
The tradeoff is that it's 32 bytes in size, as opposed to 16. The reason is that the MemoryView inlines into the FixedSizeArray, which is also the reason we safe a pointer load.
Eliding one pointer chase at the cost of 2 integers of memory is almost certainly a performance benefit.
I suggest that certain constructors like FixedSizeArray(::Array) and FixedSizeArray(::Memory) uses MemoryViews internally.
A Discourse post by foobar_v2 mentioned an important fact I've never considered: Memory access has more indirections when accessed through a
Memoryas opposed to aMemoryRef:Memorystruct (mutable struct, so has its own address)Memorystruct. Load the value from hereMemoryRefwithout indirectionTherefore, a
FixedSizeArraywrapping aMemoryViewis more efficient than one wrapping aMemory, as it elides one pointer chase:The tradeoff is that it's 32 bytes in size, as opposed to 16. The reason is that the MemoryView inlines into the FixedSizeArray, which is also the reason we safe a pointer load.
Eliding one pointer chase at the cost of 2 integers of memory is almost certainly a performance benefit.
I suggest that certain constructors like
FixedSizeArray(::Array)andFixedSizeArray(::Memory)uses MemoryViews internally.