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
louiscad
05/16/2024, 10:43 PM
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
martypitt
05/17/2024, 6:31 AM
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
louiscad
05/17/2024, 8:32 AM
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
martypitt
05/17/2024, 11:03 AM
Our dispatchers are a bit of a mess TBH.
Is that likely to be generating the overhead we're seeing here though?
martypitt
05/17/2024, 11:04 AM
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
uli
05/17/2024, 12:54 PM
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
louiscad
05/17/2024, 2:31 PM
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.