-
Notifications
You must be signed in to change notification settings - Fork 530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce GenDeferred
#2810
Introduce GenDeferred
#2810
Conversation
I didn't look too closely, but I think I've already did this in #1583 (it wasn't merged). |
@durban thanks for chiming in, yes the concept looks the same :) apologies that I wasn't aware of your prior art. One year on, with at least two applications, I wonder if opinions have changed? 😅 |
I hope this and the follow up could land in cats-effect. |
My opinion haven't changed: I still think it's a good idea 😄 (Although as it turned out, my original use case required even more than this...) |
Chiming in with another closed PR in the same vicinity: #1889 is a bit unfocused, but among other things it takes a slightly different approach of just letting you change the effect type of an existing |
Wow, another one! Thanks for the pointer. It seems you were actually able to execute the changes for |
Ah yes - not sure whether the |
Yes, I held off since we are getting a real |
Quick comment I've already mentioned on Discord: I don't have issues with the idea, but I'd rather we didn't have the subtyping hierarchy |
Big thanks to Daniel for patiently walking me through exactly what happens when you complete a A key part of completing a cats-effect/kernel/shared/src/main/scala/cats/effect/kernel/Deferred.scala Lines 157 to 158 in f423ac7
To do so, you fulfill a callback
The callback function itself is ultimately defined here.
Which involves scheduling the semantically-blocked fiber in question.
Which takes us to here. cats-effect/core/shared/src/main/scala/cats/effect/IOFiber.scala Lines 1284 to 1287 in f423ac7
And finally, to here (assuming our fiber is indeed assigned to the WSTP). cats-effect/core/jvm/src/main/scala/cats/effect/unsafe/WorkStealingThreadPool.scala Lines 418 to 431 in f423ac7
And suddenly, it's obvious why it makes a huge difference whether the current thread we are running on belongs to the compute pool (where In summary: the context shift is fundamental as Daniel has been saying all along. So, better to get on the compute pool ASAP. Sadly, this still leaves JavaScript in an awkward place since there is no WSTP-fastpath. The initial Note: edited to clarify the final hop to actually scheduling on the WSTP. |
@armanbilge What if we have a JS specialization of |
Sure, but what would it do / how would it work? |
Trying a different idea in #2835. |
This PR introduces
GenDeferred
as a generalization ofDeferred
:Conceptually, this enables two independent effect systems to communicate/synchronize via a shared data structure.
In practice, there are often situations where it is useful to instantiate a
GenDeferred
whereF
is anyAsync
type andG
isSyncIO
when interopping with unsafe code. This makes it possible to complete the deferred "in-place" viaSyncIO#unsafeRunSync
without requiring aDispatcher
and avoiding a thread-shift to the compute pool to run this trivial operation.I would like to follow-up with a similar
GenQueue[F, G, A]
such that an unbounded queue can be implemented forAsync[F]
andSync[G]
.Example use-cases include fs2-grpc (cc @ahjohannessen) and fs2-io.js which both rely heavily on
Deferred
andQueue
to interop with unsafe code.Credit to @ChristopherDavenport for first exploring this idea in https://github.com/davenverse/condemned.