notice: I mean that I want to use Kotlin coroutine...
# coroutines
n
notice: I mean that I want to use Kotlin coroutines to launch many interpreters simultaneously, that is not about Lua coroutines
g
Why do you exactly need coroutines to launch multiple interpreters? Coroutines are really helpful if you write asyncronous code, but if you just want to run multiple parallel tasks you can use coroutines but it's not something significantly better than launch a few threads,
n
In my case I need to do much work asynchronously. A new instance of a Lua interpreter should start up on each HTTP request from a user. The requests are coming from the web frontend via Ktor. Given that the execution of Lua script would involve doing many HTTP requests itself, which is mostly waiting, and that there could be many users making the requests simultaneously, I think that Kotlin coroutines would shine
as for threads, well, you know their disadvantages of using them in such a scenario 🙂
l
@noncom Does your Lua code can call your Kotlin code back when it has run successfully, without having to block the thread it has been started on?
n
Yes, it should be able to do so. Currently I have no problems because everything is blocking. But I need to support more users and that's why calling Lua must not block the threads
g
What is your bottleneck? CPU or RAM (also ram consumed by threads)
n
CPU.. it is generally adviced not to have more threads than 2x number of your cores. So, assuming that I'd like to run 1000 interpreters in parallel, that is a harsh limit
g
But if your problem is CPU not sure how non-blocking approach can help you. Do you consume all CPU cores? If so, it means that you have enough parallelism in your app and further parallelization probably will not help or even make things worse. Sometimes high CPU usage may be caused by blocking or locks, when too many threads just spinning and wait for unlock, but it's not always the case, you can analyze it by checking CPU stats
Sure, running 1000 threads simultaneously is bad idea, but such big amount of threads is mostly memory problem and general performance because of context switch. But you always can use thread pool with limited amount of threads, it's good option for CPU bounded tasks.
Also, I don't think that
not to have more threads than 2x number of your cores.
is universal advise, it's highly depends on what your threads do and many other factors m It's good advice If your thread running CPU bounded task in this case increasing amount of threads will do things worse
I don't want to say that use non-blocking approach and coroutines is bad, it's of course allows you to write scalable apps. And such things as http or other io should be non-blocking if it's possible. I just wanna say that it's not a dogma and highly depends on your use case and app
n
@gildor I see... that's interesting. I think that there are empty spots in my understanding of the current sutuation. Really, I must make more experiments and measurements and first understand what everything is up to. You've given some nice advices at what to look at! Thank you, I'll try out these proposals to see what it'll come up to.
👍 2
g
I would recommend to watch these videos about performance analysis, benchmarking and optimisations. It’s about JVM so can be directly used for Kotlin apps same as for Java:

https://www.youtube.com/watch?v=Zw_z7pjis7k

https://www.youtube.com/watch?v=eDTTxYCGsKc

On first video, starting from 58-59 minute there is a slide about analysis of CPU utilization
n
@gildor thank you 🙂