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). 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
GenDeferredas 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
GenDeferredwhereFis anyAsynctype andGisSyncIOwhen interopping with unsafe code. This makes it possible to complete the deferred "in-place" viaSyncIO#unsafeRunSyncwithout requiring aDispatcherand 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
DeferredandQueueto interop with unsafe code.Credit to @ChristopherDavenport for first exploring this idea in https://github.com/davenverse/condemned.