https://kotlinlang.org logo
Title
s

Stefan Oltmann

06/08/2022, 2:57 PM
I got an
@SharedImmutable
private val backgroundDispatcher: CoroutineDispatcher = Dispatchers.Default
and it looks like that every Job started by
backgroundDispatcher.launch()
runs on the same NSThread. I use
1.6.1-native-mt
. The same code on JVM runs every job on another thread. Do I have to do something extra to get this parallel?
1
m

mkrussel

06/08/2022, 3:03 PM
on the
mt
branch
Dispatchers.Default
is single threaded on native, it's just a different thread than the main thread.
:thank-you: 1
s

Stefan Oltmann

06/08/2022, 3:04 PM
Oh wow, that's shocking. What can I do to have multiple threads?
I was thinking
mt
stands for *m*ulti*t*hreading 🙈
m

Mitchell Syer

06/08/2022, 3:07 PM
Use regular coroutines
1.6.1
and use the new memory model https://github.com/JetBrains/kotlin/blob/master/kotlin-native/NEW_MM.md
s

Stefan Oltmann

06/08/2022, 3:09 PM
So no chance to solve this with the old model? I fear that the new model might not be stable enough. 🤔
only for evaluation purposes
... I want to ship my app in a few weeks and right now I try to tune performance.
x

xiaobailong24

06/08/2022, 5:51 PM
s

Stefan Oltmann

06/09/2022, 6:49 AM
Yes, I use the old MM already with Dispatchers.Default and I have a lot of freeze(), ensureNeverFrozen() and so on of course. I just wasn't aware that Dispatchters.Default is single-threaded, but the more I think of it the better I understand... maybe that's a default so most frameworks just work that don't care about "capture" a value. But I was thinking there must be a Multi-threaded Dispatcher I could use if I know what I do and only pass frozen state around. But looks like there is none.
x

xiaobailong24

06/09/2022, 10:17 AM
Maybe CourotineWorker is what you want?
s

Stefan Oltmann

06/09/2022, 10:21 AM
Could be. Looks like it. I will try that. Thank you. 🙂
@xiaobailong24 Almost works, except for Ktor.
x

xiaobailong24

06/13/2022, 3:33 PM
@Stefan Oltmann Yes. U can see the below mentioned issues: https://github.com/Autodesk/coroutineworker/issues/83 https://github.com/Autodesk/coroutineworker/issues/72
:thank-you: 1
s

Stefan Oltmann

06/13/2022, 8:03 PM
Ok, thank you. So it's not just me, KTor really can't be used with CoroutineWorker. I can also confirm that for sure. But CoroutineWorker helps me anyway, because it seems to work nicely together with SQLDelight and so I can use it together with
native-mt
to load some work like metadata extraction and thumbnail creation to multiple NSThreads. I tried the normal variant (without experimental support), but that one does not work at all. So I'm now in the situation where I can't multithread anything that is related to REST API communication (since KTor doesn't work), but I can speed up some of the other things. I guess for my first release this will have to be good enough and as soon as the new memory model becomes production ready I can solve the rest of my performance issues. Again, thank you for your help.
🎉 1