Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

285
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda