Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

287
Views
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!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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).

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!