r/Clojure • • May 18 '20

State of Loom

http://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part1.html
58 Upvotes

28 comments sorted by

3

u/im_caeus May 18 '20

ZIO, Cats, and anything Haskell I/O monad inspired is pretty cool. At the end of the day, monadic comprehensions could've make all this loom project a less ambitious more affordable challenge (and more versatile, as we could be writing "sequential" code, like JS generators, for anything that has a monadic behavior, like Optional type, immutable lists, Lazy computations, JSON decoders, explicit delimited continuations, and any custom type which could benefit from monadic comprenhensions)

Cool they are making progress, but hiding it under complex core constructs and saying to users "don't wonder how it works, it just does, use it" is a really shitty approach. I wouldn't think developers are not curious about the inner workings of it.

It's like kotlin coroutines. They work internally with "delimited continuations" (quoted, because they are not reusable, which means they are stateful, which means they are not THAT powerful, which means they are not actually delimited continuations (for example, they cannot be used with lists)), but WHO KNOWS?

15

u/pron98 May 18 '20 edited May 18 '20

Details about the implementations will come, but native threads are also implemented with delimited continuations, and yet that hasn't stopped anyone from using them, including those who haven't studied the kernel's inner workings. Virtual threads are another implementation of Thread, a familiar Java construct -- the basis for much of the Java platform's organisation, in fact -- and hardly a "complex" one.

Anyway, this isn't inspired by ZIO, Cats, or Haskell, but by Java's green threads, Erlang's processes and Go's goroutines.

As to monadic composition, the goal is not to just make the code look legible on the screen -- that's an important, but rather small part, as reading code is just one way of understanding it. It is to make the construct fit with all the platform services like exceptions, debugging, profiling and other serviceability components. No linguistic construct can help with those. That's stuff that needs to be built into Java's deep core, in the VM and core libraries.

2

u/im_caeus May 18 '20

It is to make the construct fit with all the platform services like exceptions, debugging, profiling and other serviceability components. No linguistic construct can help with those. That's stuff that needs to be built into Java's deep core, in the VM and core libraries.

This is totally true. Profiling and debugging when using libraries like Cats-effects or ZIO is very difficult. Even though ZIO put a great effort to add good stacktraces for that.

Virtual threads are another implementation of Thread, a familiar Java construct -- the basis for much of the Java platform's organisation, in fact -- and hardly a "complex" one.

This I do not agree with. When you go and check how coroutines are implemented in Kotlin, you'll see how complex they are. Java ones will have just as much complexity, and people will have to understand the concept of yielding, to resume later, which currently Threads, don't have. There's no "yield" keyword, or an open ability to say "let's yield, return a continuation reifying the state of this computation, and resume when the result is ready"

6

u/pron98 May 18 '20

Developers will need to understand the concepts of yielding to resume later just as much as they need to understand that with today's Thread, as it's the same abstraction, with JMM the relevant specification. The details of the implementations are as relevant as those in the kernel, and roughly the same percentage of developers who need to study the implementation of threads in the kernel, or of processes in Erlang or of goroutines in Go, will need to do it in Java.

5

u/lilactown May 18 '20

Java ones will have just as much complexity, and people will have to understand the concept of yielding, to resume later, which currently Threads, don't have.

My reading of the OP is that this wouldn't be necessary. Am I incorrect? Specifically, from the text:

First, we no longer need to avoid blocking, because blocking a (virtual) thread is not costly. We can use all the familiar synchronous APIs without paying a high price in throughput.

[...]

Instead of breaking the task down and running the service-call subtask in a separate, constrained pool, we just let the entire task run start-to-finish, in its own thread, and use a semaphore in the service-call code to limit concurrency — this is how it should be done.

Using virtual threads well does not require learning new concepts so much as it demands we unlearn old habits developed over the years to cope with the high cost of threads and that we’ve come to automatically associate with threads merely because we’ve only had the one implementation.

4

u/lilactown May 18 '20

Even more explicitly:

Virtual threads are preemptive, not cooperative — they do not have an explicit await operation at scheduling (task-switching) points. Rather, they are preempted when they block on I/O or synchronization.

1

u/im_caeus May 18 '20

Exactly. It really doesn't look like something easy to achieve, and so, I'm accurately assuming they're doing a lot of crazy stuff to achieve it. It'll automagically work. And I don't like things that automagically work.

But that's just my opinion.

3

u/bsless May 19 '20

I read it and still don't feel like there's anything "automagical" about it. Virtual threads are managed by some manager and are "mounted" on real threads for execution. In terms of implementation if you understand how go's goroutines or clojure's core.async go blocks are implemented it seems relatively simple. Not easy, though, as it's a mountain of a task, but the question of what and how to implement can probably be outlined on a page.

0

u/im_caeus May 19 '20

Yep, you convinced me that virtual threads actually have an explicit yield operation inside the virtual thread, and an explicit continue operation outside the virtual thread. You convinced me that those operations are not implicit and hidden behind the curtain of a very hacky implementation.

0

u/bsless May 19 '20

They say as much, that virtual threads' implementation is enabled by implementing continuations.

I don't see why you think it's hacky?

1

u/im_caeus May 19 '20

How wouldn't it be it all the blocking legacy code is now non-blocking?

3

u/lilactown May 19 '20

I'm accurately assuming

what a phrase.

I have a high level of confidence in the people working on the JVM, personally. I don't understand the implementation but if it passes muster, then my hope is that it will be ahead of anything we could reasonably implement in Clojure today.

4

u/nezda May 18 '20

When will this be released?

10

u/alexdmiller May 18 '20 edited May 18 '20

Current target is Java 17 I think - I swear I saw that somewhere but can't find the reference anywhere.

1

u/cbleslie May 19 '20 edited Sep 12 '25

trees shocking telephone swim summer resolute cheerful capable snails observation

This post was mass deleted and anonymized with Redact

1

u/leonoelOfficial May 19 '20

The virtual thread thing is a nice POC but I don't really see myself using it, it's too low-level to be used in application space directly and too high-level to serve as a basis for more advanced abstractions. I find a bit disappointing that the loom team chose to reimplement the thread abstraction with the exact same semantics and limitations, providing nothing but an alternative performance tradeoff. For now it's still far behind the current state of the art of JVM asynchronous frameworks, I understand that it's still early stage but the big picture still looks a bit fuzzy to me.

I will evaluate loom when the continuation mechanism will be fully available with cloning support, that's the most elementary block providing the widest range of possibilities, especially the ability to fork a computation arbitrarily across stack frames. TBH I don't quite understand why the loom team doesn't seem to want to release the continuation feature without releasing at the same time its own opinionated green threading tech. Continuations could benefit the entire ecosystem right now, unless I'm missing something the hard part of the job is pretty much done.

7

u/pron98 May 19 '20 edited May 19 '20

it's too low-level to be used in application space directly

Have you read part 2?

Continuations could benefit the entire ecosystem right now

Virtual threads would benefit 99% of the ecosystem, and in a better way than continuations, so we're starting with that.

However, if you give virtual threads a closer look you might find that custom schedulers are, in fact, better than exposing continuations directly. If and when we expose continuations, they will likely be confined to a single thread (virtual or not) because otherwise Thread.currentThread becomes effectful, and the compiler can't hoist it out of loops.

unless I'm missing something the hard part of the job is pretty much done.

You're missing almost everything :) Adding continuations to the JDK is the easy part of the job. The hard part is evaluating the industry's needs and constraints, and making sure there's a good product/market fit, and then making the appropriate user-facing design decisions. The goal isn't to show off Java's technical capabilities, but to serve the needs of users of one of the world's biggest, most mission-critical software platform in the best way and under their constraints, so that we deliver the most good to the greatest number of people. This is the really hard part. The technical parts -- continuations, monads -- that's easy student work.

1

u/leonoelOfficial May 19 '20

Have you read part 2?

Yes, and I'm definitely interested to see what you'll end up with. However, I'm also interested knowing what makes you think trio-style structured concurrency is a superior approach compared to functional effect systems, which solve the supervision problem just as much, have more maturity and introduce less accidental complexity.

Virtual threads would benefit 99% of the ecosystem, and in a better way than continuations, so we're starting with that.

If you want to get rid of monadic operators in a traditional streaming system like RX, you need a way to run the current continuation for each value produced by a given stream. Virtual threads are not forkable (yet ?), you can't resume them multiple times from a given suspension point, so they're not helpful here. Do you include all streaming systems in the remaining percent ?

If and when we expose continuations, they will likely be confined to a single thread (virtual or not) because otherwise Thread.currentThread becomes effectful, and the compiler can't hoist it out of loops.

Does that mean there's no chance we'll ever have multishot continuations ? Or that it will be implemented in the virtual thread layer ?

The hard part is evaluating the industry's needs and constraints, and making sure there's a good product/market fit, and then making the appropriate user-facing design decisions. The goal isn't to show off Java's technical capabilities, but to serve the needs of users of one of the world's biggest, most mission-critical software platform in the best way and under their constraints, so that we deliver the most good to the greatest number of people.

That's a commendable goal, but why not leverage the existing ecosystem ? There's already many mature green threading tech running in production now, and they solve real business problems even thought they're so painful to use due to lack of platform-level support (you did a great job describing this problem). They would benefit a lot from direct access to loom's low-level constructs, so what is the point restraining their usage ?

3

u/pron98 May 19 '20 edited May 19 '20

I'm also interested knowing what makes you think trio-style structured concurrency is a superior approach compared to functional effect systems, which solve the supervision problem just as much, have more maturity and introduce less accidental complexity.

I reject your claim, and I think it ultimately comes down to personal aesthetic preferences. I think that, given the low adoption rate for various reactive solutions despite having few alternatives, it is clear what the more popular preference is. Nevertheless, structured concurrency is new, which is why, if we adopt it, we'll do so very carefully.

you need a way to run the current continuation for each value produced by a given stream.

You need to run a piece of code for each value, not necessarily the current continuation. This can be done either with a simple loop reading from a channel, from attaching functional combinators to channels (again, depending on user preference), or by creating a new thread for each element.

Does that mean there's no chance we'll ever have multishot continuations ?

I don't know how to answer this question yet. But as someone who's enjoyed working with multishot continuations for years, I must admit that their marginal benefit over non-reentrant ones is quite small, and they are often used in very cool, but ultimately not very impactful cases. Having said that, they have some uses in things like automatic differentiation, so we'll judge their cost/benefit later.

but why not leverage the existing ecosystem ?

That's exactly what we're doing. The existing ecosystem around threading in Java is several orders of magnitude bigger than the relatively unpopular "reactive" ecosystem.

They would benefit a lot from direct access to loom's low-level constructs, so what is the point restraining their usage ?

As I said, I think that custom schedulers give you more than you even asked for. Study them for a bit :)

1

u/leonoelOfficial May 20 '20

You need to run a piece of code for each value, not necessarily the current continuation. This can be done either with a simple loop reading from a channel, from attaching functional combinators to channels (again, depending on user preference), or by creating a new thread for each element.

A thread is an abstraction of a processor, a channel is an abstraction of a pipe. They're both purely accidental complexity. Have you ever heard a customer say, I want a processor here, I want a pipe there ?

As I said, I think that custom schedulers give you more than you even asked for. Study them for a bit :)

That answer makes it very clear that you did not understand what I asked for.

Anyway, thank you for having taken the time to clarify your vision :

  • FP is an inferior paradigm because otherwise it would be more popular, and the lack of platform support has nothing to do with it.

  • There's no point exposing project loom's low-level constructs because virtual threads are the right level of abstraction for every problem.

I wonder what kind of feedback you're waiting for.

2

u/pron98 May 20 '20 edited May 20 '20

Have you ever heard a customer say, I want a processor here, I want a pipe there ?

All code is accidental complexity, in a sense. Have you ever heard a customer say, I want a monad here, I want a continuation there? The question is how easy it is to write, read, debug, profile and maintain code that satisfies the customer's needs. And no one says that you shouldn't write libraries on top of threads and queues to make this stuff as declarative as you like. That's what libraries are for, and the richness of Java's library ecosystem is a great source of pride. We just put the building blocks in the JDK. How to use them is up to you, and our users are spoiled for choice.

Anyway, thank you for having taken the time to clarify your vision ...

Neither of these is my opinion, let alone my "vision," and it's unclear to me where I "clarified" it to be so. I try not to pass a value judgment on aesthetic choices in a professional capacity -- the only way to judge a programming style to be superior or inferior would require some data I don't have -- and I believe threads are the right level of abstraction in the JDK for the specific problem presented in the article. Higher-level constructs might well be provided by libraries, and the JDK could well choose other constructs for other problems.

1

u/daver May 21 '20

So, we’re back to green threads again, ala the mid-1990s. Cool. I’m old enough to remember when OS threads were the new hotness. And then Erlang showed us how to do it right.

2

u/pron98 May 21 '20 edited May 21 '20

Sort of. Green threads + multicore + custom scheduling, and in addition to native threads. But yes, Erlang has absolutely been an inspiration.

1

u/daver May 21 '20

The JVM on Solaris had M on N threads way back when. Multi core was always there for boxes that supported it, but was expensive and not many did. Now multicore is normal. But yes, custom scheduling is new.

Should probably clarify that SMP was supported early (1990s), but not from day 1.

4

u/pron98 May 22 '20 edited May 22 '20

Yes, that was the case for a short while, although the term "green threads" was reserved for the M:1 case. It was a period of many changes and many VMs, so there wasn't really "the JVM." We had:

  • The Classic VM in JDK 1.0 for Solaris was M:1 ("Green Threads").

  • The Exact VM in JDK 1.1 and 1.2 for Solaris was M:N, but this wasn't done in the JVM but in the native thread-library on Solaris 8, T1 libthread. Solaris 9 switched to 1:1.

  • The HotSpot VM in JDK 1.3 for Solaris first supported Solaris 8's T1 with M:N and then, in 1.4 switched to 1:1 ("Native Threads"), making threading more consistent across Windows and Solaris.

1

u/daver May 27 '20

Okay, good history lesson. Thanks!

2

u/[deleted] May 19 '20

Really great stuff! I'm looking forward to the day I can use this in production 😊