简体   繁体   中英

How many coroutines is too many?

I need to speed up a search over some collection with millions of elements. Search predicate needs to be passed as argument.

I have been wondering wether the simplest solution(at least for now) wouldn't be just using coroutines for the task.

The question I am facing right now is how many coroutines can I actually create at once. :D As a side note there might be more than one such search running concurrently.

Can I make millions of coroutines(one for every item) for every such search? Should I decide on some workload per coroutine(for example 1000 items per coroutine)? Should I also decide on some cap for coroutines amount?

I have rough understanding of coroutines and how they actually work, however, I have no idea what are the performance limitations of this feature.

Thanks!

The memory weight of a coroutine scales with the depth of the call trace from the coroutine builder block to the suspension point. Each suspend fun call adds another Continuation object to a linked list and this is retained while the coroutine is suspended. A rough figure for one Continuation instance is 100 bytes.

So, if you have a call trace depth of, say, 5, that amounts to 500 bytes per item. A million items is 500 MB.

However, unless your search code involves blocking operations that would leave a thread idle, you aren't gaining anything from coroutines. Your task looks more like an instance of data paralellism and you can solve it very efficiently using the java.util.stream API (as noted by user marstran in the comment).

According the kotlin coroutine starter guide , the example launches 100K coroutines. I believe what you intend to do is exactly what kotlin coroutine is designed for.

If you will not do many modifications over your collection then just store it in a HashMap, else store it in a TreeMap. Then just search items there. I believe the search methods implemented there are optimized enough to handle a million items in a blink. I would not use coroutines in this case.

Documentation (for Kotlin):

HashMap: https://developer.android.com/reference/kotlin/java/util/HashMap

TreeMap: https://developer.android.com/reference/kotlin/java/util/TreeMap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM