Skip to content

Remove indirection by defaulting to MemoryView, not Memory #187

@jakobnissen

Description

@jakobnissen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions