Question: which is faster, creating new coroutines or reusing old coroutines
Hi!
I want to build a system with coroutines, but performance is my concern. My question is how to design the system: should I create a fixed number of coroutines at the beginning and keep reusing them, or can I simplify my design: create a coroutines, once it is done destroy it and create another one. I would prefer the second solution unless it is slower.
should I create a fixed number of coroutines at the beginning and keep reusing them, or can I simplify my design: create a coroutines, once it is done destroy it and create another one
In most scenarios, there is not very much performance difference between these two choices. Strictly speaking, the former would be slightly faster than the latter because the former is more cache-friendly and some memory allocation and release operations are reduced. We can use tcmalloc to mitigate such differences.
But if the memory itself is the bottleneck of your system, then the former one is probably the better choice.
.
Thanks for the answer. One more question. is it possible to create a system with e.g. one main co and several slave co, such as, when aco_yield is called, the task doesn't switch to the main co, but to the next co in the ring
Currently, I have this
slave1 -> main co -> slave2 -> main co -> slave3 -> ... slave8 ->main co -> slave1
But what I want is
slave1 -> slave2 -> slave3 -> slave4 -> ... -> slave8 -> main co -> slave1
Is the second possible?
slave1 -> main co -> slave2 -> main co -> slave3 -> ... slave8 ->main co -> slave1
Only this usage is currently supported. This is what we called asymmetric coroutine. The main co could be used as a scheduler.
slave1 -> slave2 -> slave3 -> slave4 -> ... -> slave8 -> main co -> slave1
This is what we called symmetric coroutine. And is not currently supported by libaco.