Atomic{T} types boxing and performance


I was looking at the source for the Atomic operations and it looks like the Atomic{T} type is a mutable struct, which means that a Vector{Atomic{T}} is an array of references to the integers.

When using atomic operations with OpenMP and GCC atomic instructions, there is no need to have this layer of indirection. In this case there is no difference between an “array of ints” and an “array of ints on which I will use atomic instructions.”

Could we have a similar behavior in Julia? What would prevent this from being implemented?


I have plan to change it though we can’t have an atomic field that’s inlined, rather atomic operations on normal fields.


I have been working on a package, UnsafeAtomics.jl implementing atomic operations on Array{T <: AtomicTypes} which removes the layer of indirection @jpfairbanks mentions. I have got most of the atomic operations working on Arrays at this point (except FloatingTypes add, sub, min and max) and tests from Base passing for these operations.

I’ve been trying to get atomics to work on Int and Float types and not just Arrays but I’ve been getting BoundsErrors on passing the value of pointer_from_objref to the llvmcall. What could be the reason for this?

Is there a possibility for this functionality to be integrated into Base? If so, what changes should I make?



I don’t think that’s the correct approach. What I think what we need is an atomic operation that can operate on Refs, which will also need to properly handle the GC write barrier for pointer refs. We also need a field ref typep so that it can be operated on fields too. Then we need the necessary optimizations in typeinf so that the operation is allocation-free. is impossible.