I feel my suggestion with Ref was under appreciated.

const a = Ref{Int}(0)

inc_ref() = a[] += 1

julia> @code_llvm inc_ref()

define double @julia_inc_ref_50012() #0 {
top:
  %0 = load double, double* inttoptr (i64 140198715724432 to double*), 
align 16
  %1 = fadd double %0, 1.000000e+00
  store double %1, double* inttoptr (i64 140198715724432 to double*), align 
16
  ret double %1
}

type wrapper{T}
    x::T
end

const w = wrapper(0.0)

function inc_global()
    w.x += 1
end

@code_llvm inc_global()

julia> @code_llvm inc_global()

define double @julia_inc_global_50131() #0 {
top:
  %0 = load double, double* inttoptr (i64 140258000874464 to double*), 
align 32
  %1 = fadd double %0, 1.000000e+00
  store double %1, double* inttoptr (i64 140258000874464 to double*), align 
32
  ret double %1
}

So Ref generates the exact same code as the wrapper so if anything, Ref is 
the official way of doing this.

On Monday, June 13, 2016 at 11:37:21 PM UTC+2, Arch Robison wrote:
>
> I'm revising the notes for my JuliaCon workshop, and wondering if there is 
> a standard way/convention for the following hack to reduce the overhead of 
> accessing a global variable.  The hack is to replace a global variable, if 
> known to always be bound to objects of the same type, with a const variable 
> that wraps a reference to the the object.  For example, instead of writing:
> x = 0.0
>
> function inc_global()
>    global x
>    x += 1
> end
>
> we would write:
>
> type wrapper{T}
>     x::T
> end
>
> const x = wrapper(0.0)
>
> function inc_global()
>     w.x += 1
> end
>
> Is there a standard library type that serves the purpose of `wrapper`, or 
> a standard type that can be abused to do so?
>

Reply via email to