Hi - continuing my earlier conversation on perform...
# coroutines
m
Hi - continuing my earlier conversation on performance - I'm still profiling my app looking for hotspots. I have the following profiler view from IntelliJ, and I'm trying to interpret it. It appears as though Coroutines library code is consuming nearly 66% of my process time. This seems to be a very high overhead. Am I misreading this?
l
Do you have lots of concurrent flows? If so, you might want to share them using shareIn and keep (& reuse) the resulting SharedFlow reference.
🙌 1
m
Yes, we do. But unfortunately they're not shareable (if I understand correctly). We have thousands of concurrent requests coming through the platform. In the above example, this is responding to a high throughput kafka topic, where each message triggers multiple API and database calls. My understanding is that these flows aren't shareable, as they contain different request / responses.
l
Do you use Dispatchers.IO? Unless you're wrapping blocking code that's not doing CPU bound work, you should default to Dispatchers.Default. The code parts running on Dispatchers.IO should be as small as possible, and ideally, not have suspension points inside.
m
Our dispatchers are a bit of a mess TBH. Is that likely to be generating the overhead we're seeing here though?
ie., my naieve assumption of misusing dispatchers would be that processes would block / queue when they don't need to -- but what we're seeing here is a high amount of CPU consumed by framework code -- that seems different, no?
u
Well, scheduling CPU intensive code on Dispatchers.IO will congest cpu/memory/caches because it schedules more work in parallel then can be actually executed. In that case using Dispatches.Default will only schedule as many coroutines as can actually be handled by your cores in parallel, queueing up the remaining tasks.
l
The bottleneck you'd create by having too many threads trying to do CPU bound work might be visible in the coroutines machinery. If your dispatchers are a mess, I'd encourage you to look into fixing that first.