Can anyone tell me, what is the value of using <ht...
# kotlin-native
s
Can anyone tell me, what is the value of using https://github.com/Kotlin/kotlinx.atomicfu over just using atomic references? It seems like it provides an API like an atomic reference, but a lot of work has gone into this library, and I just don't understand the value it provides. I know I'm missing something, I just can't figure out what.
g
I see obvious differences: that atomicfu is compile-only library. And Atomic* from K/N is Kotlin Native only implementation But it would be interesting to hear about future of atomicfu vs
konan.worker.Atomic*
Maybe @elizarov could provide some information about this.
e
For K/N-only projects you should use K/N atomic primitives.
AtomicFu
was designed to support cross-platform atomics. It the current incarnation its native version (to be released soon) is single-threaded only (a la JS), but will be reimplemented in multi-threaded fashion over K/N atomic primitives to support cross-platform multi-threaded code.
In the future (very long future, after K/N release)
AtomicFu
might got folded into the common standard library and compiler, providing cross-platform atomics out-of-the box. There are many many language design problems standing between now and that future.
👍 1
s
Why is atomic fu altering bytecode then? If I have a jvm-only kotlin project, is there any advantage to using atomicfu?
e
For JVM only project AtomicFu is both convenient (you write idiomatic Kotlin code) and offers you more performance (the resulting bytecode is efficient).
It offers you a convince of
AtomicInteger
-like API (but with a Kotlin twist of inline functions) with a performance of
AtomicFieldUpdater
(which is very cumbersome to use manually). It also transparently supports Java 9 varhandles (which are even more performant).
s
I've never used
AtomicFieldUpdater
. I'll read into it. I'm assuming that the bytecode transformation is efficient and far less impactful on compile times than Java Annotation Processors? Has the compile time impact been measured? I'm curious.
e
It is quite fast, but I did not measure it.