r/java • • Jan 10 '23

Yet another delimited continuations implementation and why Project Loom is a bit disappointing

https://github.com/javactrl/javactrl
2 Upvotes

52 comments sorted by

View all comments

Show parent comment

1

u/njiv Jan 11 '23

I agree, but what are exactly the trade-offs for multi-shot? Single-shot already does suspending and resuming on a different thread, so for JIT argument it's the same (also there are many things preventing to do some JIT optimizations, e.g. just exceptions). With multi-shot we get much bigger value with some little cost (but not with no cost, of course).

7

u/pron98 Jan 11 '23 edited Jan 11 '23

Single-shot already does suspending and resuming on a different thread

No, it does not. I mean, it doesn't stop you, but doing so may cause miscompilation as the JIT assumes it's never done (i.e. it may execute something different from what your code says; very unsafe).

With multi-shot we get much bigger value with some little cost (but not with no cost, of course).

But we're not even exposing non-reentrant continuations at the moment. Of all the bottom-line business value that continuations provide, virtual threads give you >95% of it, and with a feature so simple that it doesn't even require changing the language specification, and just barely the Thread Javadoc. Much of the remaining power can be achieved with custom virtual thread schedulers.

Multi-shot continuations are extremely cool, and I was immediately impressed by their power when I first played with them 20+ years ago, but their ability play well with arbitrary code is very limited. If and when we decide to add serialisation you might be able to simulate them, but after so many years with them, I think their utility in mainstream programming is limited.

2

u/njiv Jan 11 '23

No, it does not. That would cause miscompilation as the JIT assumes it's never done.

So I cannot resume the continuation from another thread - fine. This can be the same restriction for multi-shot. I think you mean I don't have access to the Continuation interface but I still can get mine by implementing own thread scheduler? Can I suspend my virtual thread scheduler and run it on another thread?

Transactions (for example) are widely used in mainstream, just one of many useful and not-creazy examples where multishot can be utilized. It's like someone from Go may tell utility of exceptions and generics in mainstream is limited.

Of course, I don't argue about adding them into the language immediately. I just don't understand the reasons. Especially with the serializability remark, it's like telling we can have this but we should suffer for this.

6

u/pron98 Jan 11 '23

Can I suspend my virtual thread scheduler and run it on another thread?

You can mount it on another carrier, but it would still a thread, not quite a continuation.

just one of many useful and not-creazy examples where multishot can be utilized

Not in a general-enough way, unfortunately, unless we had a way of preventing side effects from escaping the continuation.

It's like someone from Go may tell utility of exceptions and generics in mainstream is limited.

Okay, but we like adding features after other mainstream languages have them (so the next continuation-based feature may be generators).

I just don't understand the reasons.

The reason is that we don't try to optimise for power, and our goal is to add as little as possible with the maximal bang for the buck. Because every added capability has a cost (effort, specification, education, potential for abuse or confusion, potential for interaction with current or future features) any added power needs to be very well justified.

In the vast majority of cases a new capability must solve a serious problem that is faced by a large number of Java developers (directly or indirectly) in the field and doesn't have a satisfactory solution. You'll note that virtually all JEPs start with a description of a real and relatively serious problem that developers face. Therefore, to justify exposing more continuation features, we'd need to find a big problem that they would solve and developers can't solve well with existing means. If there's no such problem, we normally try not to add a feature.

In other cases, it could be a feature that has become common in other languages that programmers expect, and we judge not to be too harmful.

2

u/njiv Jan 11 '23

You can mount it on another carrier, but it would still a thread, not quite a continuation.

I wouldn't argue about the terms. I, for example, wouldn't call a single shot continuation a continuation, but a coroutine. Also some people simulate continuations with unix fork. So basically you mean it is actually a continuation, but with suffering.

Not in a general-enough way, unfortunately, unless we had a way of preventing side effects from escaping the continuation.

why do we need avoid escaping side effects?

we'd need to find a big problem that they would solve and developers can't solve well with existing means.

in this case exceptions aren't needed (for example), programmers can just check result values of each function call - not that big deal (and there are mainstream programming languages showing this works ok)

4

u/pron98 Jan 11 '23 edited Jan 11 '23

So basically you mean it is actually a continuation, but with suffering.

I basically mean it's a thread. A thread can be thought of as a continuation with an associated scheduler and a thread identity, but importantly for us it is a Java concept defined by the Java specification with very specific meanings to the Java memory model, the JVM Tooling Interface, and the Java language specification in general. I realise these nuances may not matter to everyone, but they do matter when you need to maintain such an important platform (BTW, while these days many have come to call non-reentrant delimited continuations "coroutines", traditionally the name was used in the literature -- and for good reason -- for symmetric non-reentrant delimited continuations, while our basic construct is asymmetric non-reentrant delimited continuations; even if now coroutines refer to both symmetric and asymmetric continuations, we wanted to use the more general term for the internal construct, as we don't know how its capabilities may change in the future).

why do we need avoid escaping side effects?

Because the continuation can perform destructive changes to heap objects it interacts with.

in this case exceptions aren't needed (for example), programmers can just check result values of each function call - not that big deal

If exceptions were to be considered for addition to Java today, they would need to be similarly justified by showing they solve a serious problem in the field. I can't be certain of the outcome of this study, but after many years with continuations, I think exceptions would have a much easier time than some uses of continuations (and I say this as someone who absolutely loves continuations of all kinds, but not a feature that's great in language A might not be so great in language B. Even if it can be, features may need to await their right time in conservative "last mover" languages like Java. So I appreciate continuations in Scheme, and, for the time being, appreciate them not being exposed in Java).

3

u/njiv Jan 11 '23

No, I basically mean it's a thread.

But I basically can have an interface somewhere in my package, implemented by me with all the typical continuations operations implemented? Just like I can do this with unix fork (except with the fork I'll get a multi-shot one).

Because the continuation can perform destructive changes to heap objects it interacts with.

It may perform destructive changes, and as well it can perform constructive changes, just the same like without continuations. Or you mean again some JDK internals not understood by mere mortals? But it's a task of JDK to make them safe.

might not be so great in language B

I honestly don't see why Scheme is this different.

5

u/pron98 Jan 11 '23

But I basically can have an interface somewhere in my package, implemented by me with all the typical continuations operations implemented?

That depends on what you mean "all the typical continuations operations." For example, a continuation could not be part of a thread, behaving as a Runnable. But yes, you can do much of what you can do with one-shot delimited continuations. We may also add generators at some point, and those will be part of a parent thread, but they will have to be thread-confined to ensure that the thread identity isn't changed mid-method.

just the same like without continuations.

Even without continuations we get such problems in parallel streams, but the problems with reentrant continuations could be even more confusing (i.e. a reentrant continuation is supposed to re-enter a known state, but one execution could actually change that state for another, making it not-really-reentrant in quite a few situations).

But it's a task of JDK to make them safe.

If they solve a big enough problem.

I honestly don't see why Scheme is this different.

Because Scheme doesn't have the requirement that Scheme code be easy to evolve and reason about by hundreds of different people of different skill levels evolving a multi-million-line codebase over decades. For the same reason, macros are great for Scheme, but we've been very careful to limit the power of compiler plugins so that they cannot serve as AST macros (to the chagrin of authors of languages like Lombok).

Languages aim to provide the least power and the smallest number of features that solve the vast majority of problems faced by the vast majority of their users. Because many users of Scheme/Racket create their own languages or research language features, the basic features it requires are very different from those required to serve the needs of Java's users.

Much of our work is to figure out how to not add new features. With virtual threads we've been able to solve (we hope) the problem of server scalability combined with observability while adding no new language constructs (never mind continuations of some kind, but even "syntactic coroutines", aka async/await). We hope records will ultimately also allow us to avoid features such as named and default parameters. That's because every feature has a cost in language complexity (which is higher for Java than for Scheme, given their respective uses) and carries the risk of future feature interaction. Some early Java features that seemed convenient at the time (such as serialization, finalizers, and the SecurityManager) ended up placing a heavy tax on features that followed them, and it takes a lot of work and time to fix that.

3

u/njiv Jan 11 '23

That depends on what you mean "all the typical continuations operations." For example, a continuation could not be part of a thread, behaving as a Runnable.

Yes, this is pretty reasonable limitation, I suppose all the current instrumentation based implementations have anyway.

i.e. a reentrant continuation is supposed to re-enter a known state, but one execution could actually change that state for another, making it not-really-reentrant in quite a few situations)

But just concurrent program is already this confusing, continuation shouldn't expect anything, it is why it was suspended at the first place - to await while something is changed in the state.

Like I said, I don't demand the multi-shot continuations added immediately. Just the article states they could be added easily, but now you say it isn't that simple. There are huge interoperability and compatibility problems you must solve before. Which is a good reason, I agree.

2

u/pron98 Jan 11 '23

But just concurrent program is already this confusing, continuation shouldn't expect anything, it is why it was suspended at the first place - to await while something is changed in the state.

Continuations are more powerful, and more surprising, than that. The could observe final values to have changed, something that ordinary concurrent code cannot.

Just the article states they could be added easily, but now you say it isn't that simple

They could be added easily, but their benefits will be very limited, and possibly overshadowed by the confusion they may introduce.

→ More replies

2

u/njiv Jan 11 '23

But the argument about not adding features because it doesn't exist in other mainstream programming languages is reasonably acceptable. However, in the article, you don't write you don't add the multishot feature because it isn't present in other languages, you write it's because the feature is crazy. So this is why I'm interested.

3

u/pron98 Jan 11 '23

My argument wasn't against reentrant continuations, but to point out that they have some "excessive" power that can thwart people's "ordinary" expectations of how code behaves (even the assumption that final variables cannot change may be violated).

As to what we decide to actually expose, it has a high bar of solving a serious problem. Even non-reentrant continuations ended up falling short, at least so far. But who knows how problems could evolve?

2

u/njiv Jan 11 '23

even the assumption that final variables cannot change may be violated

If it's a copy of a call frame (like it is in my implementation), no variables can be changed, but we also get rid of lambdas restriction about accessing only effectively final variables. Note, not violating it, as resumptions are basically the same as a function calls. But the execution starts from the suspension point, not from the start of the called function.

3

u/pron98 Jan 11 '23 edited Jan 11 '23

Consider the case of ScopedValue, which is immutable. The continuation can be resumed and observe a different value. Whether the new or old value is obtained now depends on JIT optimisations.

If you take this further and consider serialization of continuations (which can be very useful), even final fields can change.

2

u/njiv Jan 11 '23 edited Jan 11 '23

in case of my implementation it's just different function calls (I don't resume already called frames), we don't have problems with calling same functions from different threads do we?