Why does Coroutine.MoveNext take so much time?

Hi, I’m writing some procedural terrain generation code, so far I’ve split my world into chunks and spread the generation of chunks across frames by using coroutines, specifically 5 chunks for each frame. When I looked at the profiler I noticed something strange.

From the profiler, we can see that the chunk generation job takes 0.157 ms to complete, and there are 5 of them. However, the whole Coroutine.MoveNext takes a staggering 7.17 ms! That’s way more than the time 5 jobs take combined. From the timeline it’s clearly visible that there are huge gaps between each job iteration. Why is that the case? And how can I optimise it more?

Thanks in advance for any ideas and suggestions!

Post your code! :wink:

Frankly, most times coroutines are overused and lead to even more code managing them than if you were just starting your jobs in Update and checking if they are done in LateUpdate. That’s really all you need, coroutines just overcomplicate these things.

And you run the risk of running too many coroutines without realizing it.

Hi, thanks for replying! I enabled the deep profiling option in the profiler and now it shows everything in the timeline!

So the gaps in the previous screenshot were just those actions that were hidden when deep profiling was not enabled.