Atomic Operations are too expensive.

performance
multithreading

#1

Are atomic operations supposed to be this costly?
Is memory and allocations required for simple read/write operations?

julia> a = Atomic{Int64}(0)
Atomic{Int64}(0)

julia> @btime a[]
  100.546 ns (1 allocation: 16 bytes)
0

julia> @btime a[] = 100
  101.535 ns (1 allocation: 16 bytes)
100

julia> a = 0
0

julia> typeof(a)
Int64


julia> @btime a
  2.335 ns (0 allocations: 0 bytes)
0

julia> @btime a = 100
  0.025 ns (0 allocations: 0 bytes)
100

The memory useage when looping on a single array with 1000 integers sometimes goes upto
2 GB since an operation (not variable) requires memory.

Is it possible to be an error with the benchmarking tools?
Are any improvements expected in the near future?


#2

No. They are not supposed to be this costly. And they are indeed cheaper and non-allocating on my 0.6.3, but really slow on my 0.7.0-alpha.136. So I’d guess this is yet another transient performance bug on 0.7

edit: Issue is opened https://github.com/JuliaLang/julia/issues/27694.


#3

Yes they are supposed to be more expensive but no they are not supposed to allocate and no you are not benchmarking correctly since a is a non const global.


#4

And see the issue linked above for the correct way to use the macro.