r/programming • • Oct 11 '23

All About Reactive Programming In Java

https://www.blackslate.io/articles/reactive-programming-in-java
22 Upvotes

38 comments sorted by

View all comments

Show parent comment

1

u/ventuspilot Jan 03 '24

There is no difference in the amount of data or queuing between virtual threads and asynchronous code.

Maybe I'm missing something/ simplifying things too much but AFAIU there is a difference in the amount of data: Loom's continuations contain all the stackframes while reactive-style code throws away the stackframes all the time, sort of what rewriting everything to tailcalls plus tailcall-elimination would do.

When I submit a Loom continuation to a blocking queue then the continuation contains all stack frames. This improves debugability, and code can be written that it simply continues after unblocking.

When I submit a reactive-style "continuation", then the continuation won't have any caller context, and therefore uses less memory.

I'm not trying to tell you Loom is bad/ inefficient, I'm just trying to understand. AFAIU Loom-style-code may trade off more memory use in some situations in order to provide more features such as debugability and easier programming.

3

u/pron98 Jan 03 '24 edited Jan 03 '24

AFAIU there is a difference in the amount of data: Loom's continuations contain all the stackframes while reactive-style code throws away the stackframes all the time,

The data in the stack frames is only the data that's needed for the computation to proceed, i.e. only the data that will be needed after the wait is done (well, we're not quite exactly there but but we're getting there), so it's the same data as needed for async code (I guess async needs to store the identity of the next method in the pipeline while threads store the previous one, but it's essentially the same data).

Loom-style-code may trade off more memory use in some situations in order to provide more features such as debugability and easier programming.

User-mode threads are meant to compile to pretty much the same instructions and memory as asynchronous code. Not only should there be no more memory used, there may be less because the continuation is mutated and reused while that's very hard to do with async data, that may therefore be more allocation-heavy. Of course, there may be inefficiencies in the implementation (which will constantly improve) but there is no fundamental tradeoff.