kpgalligan
11/24/2019, 5:19 PMErik Christensen
11/24/2019, 6:30 PMConcurrentHashMap
. I use it to share hashes between threads that are built up lazily. It's possible there's a better/more efficient approach with K/N, but the Stately shared hash map seemed easy/familiar enough to use for the time being.basher
11/24/2019, 10:01 PMkpgalligan
11/24/2019, 10:07 PMbasher
11/24/2019, 10:29 PMkpgalligan
11/24/2019, 10:29 PMErik Christensen
11/24/2019, 10:31 PMkpgalligan
11/24/2019, 10:31 PMval imap = IsoMap<String, SomeData>(createState { mutableMapOf<String, SomeData>() })
val duration = measureTime {
repeat(100_000) {
imap.put("ttt $it", SomeData("jjj $it"))
}
}
println("Coroutine duation ${duration} count ${imap.size()}")
imap.clear()
val coroutineBatchduration = measureTime {
repeat(100) {outerLoop ->
val dataMap = mutableMapOf<String, SomeData>()
repeat(1000){
val index = (outerLoop * 1000) + it
dataMap.put("ttt $index", SomeData("jjj $index"))
}
imap.putMap(dataMap)
}
}
println("Coroutine batch duation ${coroutineBatchduration} count ${imap.size()}")
imap.clear()
val m = mutableMapOf<String, SomeData>()
val mutableDuration = measureTime {
repeat(100_000) {
m.put("ttt $it", SomeData("jjj $it"))
}
}
println("Mutable duation ${mutableDuration} count ${m.size}")
val fm = frozenHashMap<String, SomeData>()
val frozenDuration = measureTime {
repeat(100_000) {
fm.put("ttt $it", SomeData("jjj $it"))
}
}
println("Frozen duation ${frozenDuration} count ${fm.size}")
Coroutine duation 18.4s count 100000
Coroutine batch duation 634ms count 100000
Mutable duation 451ms count 100000
Frozen duation 30.5s count 100000
open class IsolateState<T : Any>(private val stateId: Int) {
internal suspend fun <R> access(block: (T) -> R): R = withContext(stateDispatcher) {
block(stateMap.get(stateId) as T)
}
}
access
callsclass IsoMap<K,V>(stateId: Int) : IsolateState<MutableMap<K,V>>(stateId){
suspend fun clear() = withContext(stateDispatcher) {
access { it.clear() }
}
suspend fun size() = withContext(stateDispatcher) {
access { it.size }
}
suspend fun get(mapKey: K): V? = withContext(stateDispatcher) {
access { it.get(mapKey) }
}
suspend fun put(mapKey: K, data: V) = withContext(stateDispatcher) {
access { it.put(mapKey, data) }
}
suspend fun putMap(map: Map<K, V>) = withContext(stateDispatcher) {
access { it.putAll(map) }
}
}
basher
11/25/2019, 12:23 AMkpgalligan
11/25/2019, 12:44 AMErik Christensen
11/25/2019, 2:29 AMkpgalligan
11/25/2019, 4:18 AMErik Christensen
11/25/2019, 4:20 AMErik Christensen
11/25/2019, 4:26 AMQinghua
11/25/2019, 5:23 AMPlaying with using multithreaded coroutines to implement shared collections by keeping the “collection” in a single thread, and storing/pulling frozen data. Performs significantly better than Stately collections.It seems that using a Worker can achieve that read/write mutable shared collection in a single thread. Why it needs
coroutine
here?kpgalligan
11/25/2019, 6:25 AMQinghua
11/25/2019, 8:19 AM