Performance benefits of writing getters for struct fields

One reason many packages use getter functions is to make interfaces a bit more explicit.
For example the fields of structs are often considered to be internals that could be renamed, or depend on the specific instance of the subtype, while one can instead export a getter function which simply accesses a field.
For example something like

struct ArrayWrap <: AbstractArray
    parent:: AbstractVector{Float64}
end
Base.parent(A::ArrayWrap) = A.parent

struct ArrayWrap2 <: AbstractArray
    data:: AbstractVector{Float64}
end

Base.parent(A::ArrayWrap2) = A.data

function doSomething(vec::AbstracArray)
   par = parent(vec)
   ...
end

is quite commonly used, and does not require every wrapper of an array to give the parent array the same name, they just need to export the appropriate parent function.

But this has nothing to do with performance as correctly said in the post above. Both versions will be compiled to the same

3 Likes