https://kotlinlang.org logo
#coroutines
Title
# coroutines
w

Waldemar Kornewald

11/29/2023, 12:26 PM
Does the coroutines lib prioritize rescheduling for certain APIs? We’re having some strange delays in seemingly all places that use Channel whereas e.g. CompletableDeferred is rescheduled very quickly after completion.
d

Dmitry Khalanskiy [JB]

11/29/2023, 12:43 PM
We try to avoid extra context switches, if that's what you mean, but the channels were recently reworked. What you see may be a regression. Did this start happening recently, or did you always notice the strange delays?
w

Waldemar Kornewald

11/29/2023, 12:50 PM
We’re currently using 1.7.3, but I think we also had this issue with the 1.6.x series. Does the combination with
Channel.receiveAsFlow()
cause any additional overhead?
d

Dmitry Khalanskiy [JB]

11/29/2023, 12:53 PM
Not especially? It does have some overhead, but in most applications, it isn't expected to be noticeable. Without any measurements, it's hard to say if what you're seeing is expected.
w

Waldemar Kornewald

11/29/2023, 12:55 PM
OK I’ll try to get back once I’ve found the time to pin this down a little bit more. We’re in the 10s of milliseconds of delays for things that should take <1ms
This was just a pattern I’ve noticed in the code and in one place we switched from CompletableDeferred to Channel because we need multi-completion without losing results
d

Dmitry Khalanskiy [JB]

11/29/2023, 1:00 PM
I would expect sending to a channel to take 10x the time it takes to resume a
CompletableDeferred
. The latter is extremely simple: it's simply scheduling a task to a dispatcher, a straightforward single-producer, single-consumer task; with channels, there's much more bookkeeping exactly because there can be many producers and many consumers.
w

Waldemar Kornewald

11/29/2023, 2:25 PM
Does SharedFlow have the same overhead as Channel + receiveAsFlow()?
d

Dmitry Khalanskiy [JB]

11/29/2023, 2:25 PM
SharedFlow is an interface, not a specific implementation.
w

Waldemar Kornewald

11/29/2023, 2:26 PM
Right, I meant MutableSharedFlow
d

Dmitry Khalanskiy [JB]

11/29/2023, 2:28 PM
My intuition is that, for the single-consumer use case, it has more overhead, but tough to say without measuring it. Its purpose is mostly to provide a specific multi-consumer behavior.
w

Waldemar Kornewald

11/29/2023, 2:30 PM
Is there any better alternative to
CompletableDeferred
which can be awaited and completed multiple times?
d

Dmitry Khalanskiy [JB]

11/29/2023, 2:31 PM
Depends on what you mean by that. Could you provide an example?
w

Waldemar Kornewald

11/29/2023, 2:32 PM
On every button press I want to send a result to a different coroutine which is awaiting for that result, but if a new result comes in the coroutine should abort processing the previous one.
Or possibly it shouldn’t abort the previous result, but rather only remember the last unprocessed result.
w

Waldemar Kornewald

11/29/2023, 2:39 PM
Yes that’s what we’re doing basically
Plus launching a coroutine and aborting it
But it might be too slow. If, for example, we use a Channel for handling the text a user types into an input field our code seems to become slower than the user can type. But maybe that’s because there’s one more indirection involved. Still, I would’ve expected the scheduling to be on the order of <1ms
d

Dmitry Khalanskiy [JB]

11/29/2023, 2:46 PM
I'm probably misunderstanding this. Are your users typing faster than 10ms per character? That's better than the world record in typing speed!
w

Waldemar Kornewald

11/29/2023, 2:53 PM
Yeah that case might be different and have even higher delays due to an indirection where two channels are involved
…and collecting a MutableStateFlow etc.
The delays seem to accumulate very quickly to unreasonable amounts
d

Dmitry Khalanskiy [JB]

11/29/2023, 2:57 PM
If you find some specific example where a seemingly simple task takes an unreasonable amount of time, please do share it. Right now, this is a bit too fuzzy to make any conclusions.
w

Waldemar Kornewald

11/29/2023, 2:57 PM
I haven’t yet debugged those in detail. I just wanted to ask about any known issues because some of our changes hint towards our use of Channel
Once I’ve narrowed the cause down and if it’s really related to Channel I’ll create a ticket
d

Dmitry Khalanskiy [JB]

11/29/2023, 2:59 PM
Since 1.7.0, we have new
Channel
implementation that's supposed to perform much better, and since you're saying the problem is the same between 1.6.X and 1.7.X, maybe that's not it. Who knows!
w

Waldemar Kornewald

11/29/2023, 2:59 PM
Yes there could also be other reasons
2 Views