The advantage of using a length-1-vector instead of an int/float variable (if any)?

For most intents and purposes, it is equivalent to

mutable struct MyRef{T}
    contents::T
end
Base.getindex(x::MyRef) = x.contents
Base.setindex!(x::MyRef{T}, v) where T = (x.contents = convert(T, v); v)

It is a container that has exactly one element. Because there always is one element, you use some_ref[] instead of some_ref[1] for access.

One of the large advantages is that Array bookkeeping (allocation and free and resize) can never be elided or inlined by the compiler; these are all foreign function calls into a C library, and not native julia (array access is special cased in the JIT and hence fast, though).

Ref is pure julia, instead of julia wrapped around C.

2 Likes