<@U0RM4EPC7> do you think that persistent data str...
# arrow-contributors
a
@simon.vergauwen do you think that persistent data structures synergize really well with lenses / optics?
im' just reading the docs and it occurred to me
s
Optics are meant to be used with immutable data so it's a perfect fit. However it's solves a different problem than the typeclass hierarchy.
a
it solves the problem of mutating deeply nested immutable structures, no?
like
assoc-in
in clojure
s
Not only that, it also solves the problem that those operations aren't reusable or composable.
Not sure if that's similar in Clojure or not tho.
a
does this also solve thread safety?
like atoms/refs in clojure?
s
We have
Ref
in Arrow Fx 🙂
a
what does it do?
s
It’s a functional atomic ref (or a higher kind version of AtomicReference). It’s built on top of effects so we can take into account exceptions and async jumps and we can guarantee safety across that.
a
how about performance?
s
It’s uses a similar principle as typeclasses. All Optics are interfaces so you can delegate to whatever preforms best but in general we can derive all this boilerplate at compile time. I.e. data classes just use
copy
to update immutable fields and
copy
gets generated by the compiler.
Sealed classes use
when
which is equivalent to
TABLE_SWITCH
so super fast
Complexer structures with Optics like
Fold
or
Traversal
can be derived from
Foldable
and
Traverse
so they always should be the highest performing.
The idea is also that you use code gen with Optics so you get composable (re-useable) and boilerplate free code to work with immutable data.
A very nice example when working with a complex structure like Json here. https://github.com/47deg/helios/blob/master/helios-sample/src/main/kotlin/helios/sample/sample.kt
I need to work on docs there 2…. but the code for Optics is vey small (less than 200 loc) so definitely interesting to check out if you’re curios about the internals. I hand wrote everything there to produce an artefact with small bytecode 🙂
how about performance?
Or did you mean something else?
a
yep I understand how the code is generated / can be overridden
I was asking about the atomic ref
i'm interested in the performance characteristics of ref vs actor or simple thread confinement
s
Oh, it just wraps
AtomicReference
. And for MPP we’ll wrap kotlinx.atomicfu which is also a simply a typealiased
AtomicReference
for Java.
i’m interested in the performance characteristics of ref vs actor or simple thread confinement
Oh, that’s more related to architecture than it’s related to individual building blocks of Arrow. In Arrow Fx you’ll typically find all low level building blocks but in the context of
F
.
Ref
is
AtomicReference
,
Promise
has many use-cases but a good example is that you can use it as a latch to do synchronisation and
Semaphore
. So it’s all low level building blocks concurrency people are typically familiar with.
Wish I could comment more on your actual question but I am not knowledge enough on those subjects.
Feel free to share any lecture you find 🙂
a
i'll write some prototypes after my vacation
i've tried Kotlin's
actor
but its performance is abysmal
i need shared mutable state
but the state needs to remain consistent
s
Ye, I imagine. I have an
actor
implementation on top of
IO
and concurrency tools in Arrow
a
that's why i need the persistent data structures
so I don't have to confine reads only writes
but with atomic-fu I might not even need to confine writes
s
Then my solution might work for you, although it’s rather an example as an actual fast impl.
It’s a combination of
Queue
,
Ref
and
Promise
. Where you basically have a
Queue
of
Promise
and you can
take
in the actor to act on a message and you can
put
in the
Queue
to send a message.
a
i see
i figured something similar
that writes are wrapped in an
Eval
and put in a queue
and a single threaded coroutine executes them in sequence
i don't even need a queue/eval for this since this functionality is provided by coroutines out of the box
s
I based it on this. https://github.com/vlovgr/actors-cats-effect-fs2 The 20min live coding session is very enlightening on how to build advance concurrency stuff with Arrow Fx (Cats-effects).
a
thanks I'll take a look
👍
did you have a chance to review the changes to my PR?
s
I am reviewing it now 🙂
a
👍