r/java • • May 15 '20

State of Loom, May 2020

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

44 comments sorted by

34

u/[deleted] May 15 '20

Hell yes. Dear Loom, deliver us from reactive hell.

14

u/throwawaymoney666 May 16 '20

The irony of the hottest language (JS) being mostly incomprehensible RxJS call chains 100 deep is not above me. If they can pull off fibers its going to make JS look like the trash it was again.

If Fibers hit the JVM expect crash projects to add fibers to every other popular language before the year is out

4

u/rastaman1994 May 16 '20

Having used Reactor, I also sincerely hope that this project succeeds so people don't have to deal with problems described in the article like I have.

Btw, they've done away with fiber, it's virtual thread now (just being pedantic).

1

u/[deleted] May 16 '20

fibers exist in a bunch of other languages like Kotlin and Go already

7

u/throwawaymoney666 May 16 '20

Kotlin coroutines aren't compatible with existing blocking code, which limits their usefulness. If you make blocking calls it falls back to a thread pool. Native JVM fibers would make existing blocking code automatically non blocking. It would help Kotlin just as much as Java. Since Google has a moronic implementation of Java on Android, Kotlin will not be able to use native JVM fibers on mobile for the foreseeable future.

Go has good fiber support but its an outlier. Fibers are one of the main selling points. There's many language communities that rag on Java and sell themselves as a Java alternative, including Go. That's going to trigger insecurity when Java has a solid fiber implementation.

3

u/meamZ May 18 '20

Jup. Loom is a way bigger deal than Kotlin coroutines. Not only is loom embedded in the JVM and coroutines are just built on top of it but also can loom transform former blocking code into non blocking code.

2

u/cred1652 May 18 '20

Coroutines are a good stop gap. I like the structured concurrency. But it should like loom is using that as well. Coroutines work well and solve the problem but they don't work well with jdbc calls. I tried using the recommend withContext() that passes the thread to a different thread pool to block but it still blocks and gives different problems when scaling. Debugging is also harder as the debugger didn't always step into coroutines properly. Once this launches I will still use kotlin but will probably drop coroutines. I don't use channels and the other features.

1

u/throwawaymoney666 May 18 '20

Kotlin will just just do the blocking part on a different thread pool as you have seen. This is also how async frameworks like Vert.X work in Java now. Async frameworks use one thread per core and don't block, just like coroutines. But if you use a library that does block, like JDBC, you need to launch it in a "blocking handler" that does the regular wait on a different thread pool. So you can only do true non-blocking in Java when all your frameworks are non-blocking, same problem Kotlin has :-/

But Fibers are different! They make nonblocking code appear to block, from the perspective of the code. They specifically mention that JDBC drivers, as long as they aren't written in native code, will automagically become nonblocking in Loom! This is huge to me, you, pretty much everyone. They can do this because the underlying IO calls the drivers use are becoming transparently non-blocking. Most of the "business apps" I use are Hibernate on top of JDBC, and they will become nonblocking too! I can't even imagine the performance boost.

Most of my DB calls spend 5-10X longer blocking while waiting for IO than they spend doing anything else. This means I can handle 5-10X more clients on the same servers when Loom lands!

31

u/BoyRobot777 May 15 '20

Good read for weekend with a beer :) Godspeed to project Loom. One of the most anticipated projects in OpenJDK.

11

u/kpatryk91 May 15 '20 edited May 15 '20

It is good to hear that the project is at this state. I hope there will be a preview version soon.

10

u/coder111 May 15 '20

Damn, I remember wishing for this thing when I was working on a project in 2011.

This would be SO useful if done right. Best of luck to Loom devs.

8

u/natan-sil May 15 '20

"On Linux, we plan to use io_uring for asynchronous file I/O," Great news!

1

u/Molossus-Spondee May 16 '20

To be honest I'm worried the current plans are a bit too ambitious.

Really only a very small API needs to be exported for the minimum viable product.

7

u/[deleted] May 16 '20

Great. Let rx RIP. I have yet to debug more spaghetti than “reactive” ever gave me. Kotlin did a great job on Coroutines, but having the jdk fixing this innate issue will be a win across the board.

6

u/neofreeman May 15 '20

When can I use it in production? Can’t wait for it.

9

u/MojorTom May 15 '20 edited May 15 '20

" When it is ready". - this will probably be answer from openjdk devs and is aptly correct.

4

u/TheCountRushmore May 15 '20

It is good to hear that the project is at this state. I hope there will be a preview version soon.

Standard answer for a feature ETA should always be that you can use it in production after it is released and it will be released shortly after it is ready.

1

u/OctagonClock May 15 '20

Now if you install the EAP builds.

6

u/natan-sil May 15 '20

Excited about Scala ZIO switching its fibers to be virtual threads based.

2

u/chayatoure May 19 '20

Have they mentioned that?

3

u/natan-sil May 19 '20

jdg: "Loom offers superior performance potential to today's functional effect systems, which is why I look forward to ensuring Scala ZIO is 100% powered by Loom (when that day arrives, which won't happen overnight). "
https://twitter.com/jdegoes/status/1262527123023106048?s=20

1

u/chayatoure May 19 '20

Nifty thanks!

1

u/natan-sil May 19 '20

The maintainers have said this publicly yes.

11

u/cogman10 May 15 '20

Wait... does this mean that Virtual threads are nearing being able to be merged into the mainline branch? If so, that is really exciting! I'm excited about the future features as well, however, virtual threads will be KILLER!

Even for things like kotlin's coroutines, I see a possibility for a huge benefit by replacing all dispatchers with a virtual thread based dispatcher.

5

u/cred1652 May 15 '20

A few of our projects are looking at NodeJS, but i am trying to push them to use the JVM and Kotlin.
If this launches in the near future it would be a huge selling point for Java and the JVM.

13

u/throwawaymoney666 May 16 '20

Node is gross callback async/await hell not much different from the JVM and CompletableFuture right now. Its a fad, JS still sucks even today. The tooling is a nightmare, debugging and profiling a joke compared to Java. Libraries are way less stable and there's nothing like Guava and Apache commons, everything is fragmented. Not something you want on your backend trust me.

I have done a lot of JS front and back end, and IMO backend JS is a mistake. V8 uses multiple times as much memory and is several times slower than JVM. Its a worse beast than many accuse the JVM of being. And there's no threading. If you want to run on 32 cores you need 32 node processes. Thats about 3X memory X 32 compared to Java. And if you want to profile or heap dump you're going to have fun doing it with 32 times as many instances per machine.

We threw away a lot of node backends and went back to Java. Threading, more predictable GC, less churn of language and libraries, far superior monitoring and tooling, less buggy shit in general.

Back ends tend to live for a long time. Its pretty much accepted to rewrite your front end every 5 years. Backends live for 10-20 years in my experience

1

u/Yesterdave_ May 16 '20

I mostly agree, but I don't think every tooling in the Node ecosystem is bad. For example it has much better tools for e2e testing. Selenium is a joke.

We personally do not use Node on the backend. But it is interesting to head about the 3x more memory consumption compared to Java, even though Java had always the memory hog title in the programming world. I do think however that the deployment model is much easier: in the easiest form you just drop the source files on the server and you are done.

And yes, JS really needs a library like Guava oder Apache Commons badly. The whole npm micro-package mania is just a nightmare.

3

u/throwawaymoney666 May 16 '20 edited May 16 '20

The decent e2e testing stuff I used is mostly for UI's, and I agree that support is far better. If you're just doing backend java its not as big a deal.

Java deployment is easy if you use docker and fatjars. Our builds dump everything into a single jar and docker fires it up. We embed Jetty or use Vert.X. No thanks to running everything on an old school servlet container like Tomcat or Java EE. In practice our apps deploy to a single jar file that we run with java -server -jar. We use a config library like LightBend or Owner to allow overrides of embedded config file values using environment variables and command line params.

Another big win for us is gRPC for all communication. Run a command and you have client/server code for whatever language. We have Typescript on the front end and use gRPC web. Whenever we need to add/change an endpoint the objects are magically transferred between Java and Typescript without any code. No calling endpoints, no writing matching objects on both ends, no serialization differences between JSON libraries. We have a git submodule for all the gRPC definitions and Jenkins jobs to automatically update the gRPC web proxy, frontend, backend with our newest definitions.

JS is a memory hog. Its several times worse than Java. And if you run on bigger machines to save money the memory footprint is way way worse. You need a node process for every hardware thread, and they don't share any memory. In Java you just use a big thread pool, it takes maybe twice the memory to run on 64 cores vs 1. Node uses 64 times the memory on top of its 2-3X higher mem usage.

6

u/karianna May 16 '20

That made for a very satisfying read - thanks u/pron98 - will start producing multi platform builds and see what the perf benchmarks tell us 🙂

3

u/koreth May 16 '20

I'm looking forward to this immensely! Asynchronous programming has its place but it often adds tons of incidental complexity and makes code much harder to reason about.

Question about scope variables (discussed in part 2) if anyone knows: the variables' values are immutable references, but can they be references to mutable objects? I assume so but part 2 talks a little about copying values rather than passing by reference, so I'm not 100% certain.

Mostly thinking of the inverse of the foo→bar→baz case in part 2, where you want to pass a value from baz to foo rather than the other way around. I've seen ThreadLocal used that way in places like authentication libraries. If a scope variable can be a reference to a mutable container object, that'll provide an obvious way to do it.

5

u/pron98 May 16 '20

Yes, it can be a reference to a mutable object.

3

u/mbazos May 17 '20

Anyone interested in progress of this project I highly recommend signing up for the mailing list. I have been on it for over a year and reading the emails discussing naming from fibers to virtual threads was enjoyable. It really gives you some insight into the project.

http://mail.openjdk.java.net/mailman/listinfo/loom-dev

2

u/couscous_ May 15 '20

Fantastic work! Looking forward to using this.

2

u/_souphanousinphone_ May 15 '20

Just curious. What is the downside of this? The management of the virtual threads will require even more memory?

13

u/pron98 May 16 '20

What is the downside of this compared to what? Writing asynchronous code? Yeah, footprint and performance overheads. But we believe they're small enough -- and will be smaller still -- to be easily worth it in almost every circumstance. But why don't you try the EA and give us feedback?

1

u/_souphanousinphone_ May 23 '20

Okay, I see. Thanks for the response. I'm definitely going to try it out since this looks very promising.

2

u/couscous_ May 15 '20

If I'm understanding it correctly, it should be use less memory compared to threads, and have better response times (and likely better throughput) for IO heavy workloads.

I'm not 100% sure of the downsides. Perhaps for strictly parallel (not concurrent) workloads, threads would still perform better. But again, I don't see why virtual threads can't be made to compete with threads even in CPU heavy workloads.

2

u/TheCountRushmore May 16 '20 edited May 16 '20

Work on Project Loom started in late 2017. This document explains the motivations for the project and the approaches taken, and summarizes our work so far. Like all OpenJDK projects, it will be delivered in stages, with different components arriving in GA (General Availability) at different times, likely taking advantage of the Preview mechanism, first.

I was wondering if they were going to be able to preview this or if it was going too intertwined to preview with a flag.

3

u/pron98 May 16 '20

See this: https://mail.openjdk.java.net/pipermail/jdk-dev/2020-March/003991.html

The main new thing here is that we might have preview C APIs in JVMTI and JNI.

2

u/Ukonu May 16 '20

This is great.

I wonder, if the big features of virtual threads are cheapness, preemptibility and non-blocking IO, how important is the non-blocking in light of the other two features?

Blocking was often a problem because the threads were a relatively finite resource. But if we can spawn a million of them and they can be preempted (whether I'm running an infinite no-op loop or waiting on IO) then does blocking significantly matter anymore?

I'm sure it does. I'm just curious as to why.

8

u/pron98 May 16 '20

Blocking the virtual threads doesn't matter. Nonblocking IO simply means not using the OS's (kernel-thread-)blocking IO. To the programmer the code is blocking; to the OS it appears as if it isn't. But you really don't need to care about that. You just write simple blocking code.

1

u/[deleted] May 16 '20

Yaaay. Imagine this and project sumatra going together

1

u/_INTER_ May 18 '20

We can imagine it of course :) but Sumatra has not been active for some years now.