JuliaArrays / JuliaArrays/FixedSizeArrays.jl
Remove indirection by defaulting to `MemoryView`, not `Memory`
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 99
- Forks
- 8
- PR merge metrics
- No merged PRs in 30d
Description
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
Memorystruct (mutable struct, so has its own address) - The memory struct contains a pointer to the buffer referred to by the
Memorystruct. Load the value from here
- Load the address of the
- MemoryRef
- Load the value directly from the pointer in
MemoryRefwithout indirection
- Load the value directly from the pointer in
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the FixedSizeArray(::Array), FixedSizeArray(::Memory), and related constructors, then inspect how they store Memory versus MemoryView. Use the Julia REPL and the @code_native examples from the issue to compare the generated access paths. Done means the relevant constructors use MemoryView internally without breaking existing behavior or tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- performance
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100