Hey guys! I have an implementation doubt, may be y...
# multiplatform
g
Hey guys! I have an implementation doubt, may be you can help me: I have a shared single common class that internally holds a
MutableMap
, so: For JVM: I use the
ConcurrentHashMap
class For Web: I use just a
mutableMapOf
But for Native, I have to
freeze()
the singleton to avoid the
IncorrectDereferenceException
. So, if the whole graph is frozen, I don’t know how to have a
MutableMap
inside it. Any tips?
a
But I don't think it will be thread safe (like ConcurrentHashMap), not sure
You can also use something like AtomicReference<Map>, then you will be able to update and iterate concurrently.
k
If you can wait for 1.3.70, there’s a new stately thing. Thread-isolated mutability! Way faster than the original stately collections. https://github.com/touchlab/Stately/blob/kpg/iso_state/stately-iso-collections/src/commonMain/kotlin/co/touchlab/stately/collections/IsoMutableMap.kt
Current stately collections are thread safe, but are implemented with AtomicReference, so they’re not great performance-wise
g
Thanks! I’ll take at look at that library. For now, I’ve drafted this class, and seems to work well (except for entries, keys and values collections), but I’m not using it
I’ll definitely give a try on
frozenHashMap
💪
@kpgalligan is it possible you lack of a
js
target for that platform? It’s failing to resolve for that target :(
a
Yeah, no JS for
stately-collections
k
There was a reason but I don’t recall offhand
g
Browsers are single threaded maybe? The thing is that without it I can’t use this library, at least not without a simple dependency on commonMain 😞
k
Had nothing to do with single threaded. I haven’t looked at it much in recent times because it’s always been on the list of things that was going to get a refactor.
Published a quick release with js. Version number 0.9.6.1. Haven’t actually tried it in JS, but the artifact should be there soon-ish