Live data from Hacker News

GraalVM

graalvm.org

91–100 of 108 posts

Re: GraalVM

#91

Can someone please give a sober explanation of what GraalVM, is and how Truffle works? I think it is somewhat analogous to LLVM: Graal exposes a "language agnostic" API which allows multiple front-ends. There's a front-end for Ruby, JS, C++, others. But this API is higher level than LLVM: it knows about objects, and allows tricks like querying properties across language boundaries. But I can't imagine what this API l…

I'll try to give a coherent explanation.

You express JS semantics by writing an interpreter for JS in Java. You can't write it in any way you want. Truffle provides a class library for the construction of interpreters, in which you define nodes in a tree. Typically this will be something like the abstract syntax tree of your language but it doesn't have to be. Each node is a class you define, with an execute method (handwaving away some details here).

At the start, you parse source or binary code into a tree of these node objects and then call execute() on one of the root nodes. For example each method or function in your language might be an independent tree, and the root node would be the node at the start of the function. Execute then calls the execute methods of the sub nodes and combines the results together, as per any basic interpreter.

The node objects have fields that contain information about the program, for example:

"return a + 5"

might turn into 4 nodes, a return node, a + node, a variable read node and a constant numeric node which has a field containing 5.

The Truffle class library contains code that measures how often a root node is invoked. After a while some roots will get hot because they get invoked a lot. This method has become a hot spot.

What happens then is the Graal compiler starts compiling the execute method of the root node. It compiles in a different mode to how Java methods are normally compiled:

1. Any time the code reads a field, the compiler pretends it's a constant if it's been annotated with @CompilationFinal. Even if the field is a mutable variable, the compiler acts as if it's not, and will read the value of the field as a constant. This can then trigger constant folding and further optimisations.

2. Any method call is inlined. This proceeds recursively until everything is inlined, stopping only at methods marked as @TruffleBoundarys. The compiler ends up with a single huge method representing the entire interpreter contents of the guest language method. Any calls past a @TruffleBoundary are in effect calls into the language runtime.

Once this is done Graal starts optimising. After inlining the method may be enormous, however, a huge amount of the code in the interpreter can be removed by these optimisations.

Dynamic languages have behaviour too complex to fully compile to native code. The amount of code required would end up being enormous and very slow, as it'd constantly need to look up basic things, like whether you redefined what + means. Therefore Truffle supports a variety of techniques to make them run faster.

One is an Assumption object. You can create these in your nodes and check them in your execute methods. It's a boolean flag. When compiling the JITC assumes the assumption is true, and deletes any code that would have been called if it were false. Your execute methods can call a special method on an Assumption to set it to false however. When that happens HotSpot will de-optimise all the compiled methods and force the back to your Java interpreter.

Another is a transferToInterpreter() method. It's special and says, any code that could execute past the point where I call this method should not be compiled. It means you can keep code that handles obscure cases out of the compiled method. Again, if the compiled method would end up executing a transfer, a de-opt happens.

Another is specialisation. Whilst your interpreter executes it's allowed to change the node objects in the trees. For example because your interpreter observes that at that point in the code you only ever add numbers together, not numbers and strings. The node can do a type check and if it fails, de-optimise and swap itself back to a slower but more generic node. Truffle has a thing it calls a "DSL", really it's a bunch of Java annotations, that automates this whole process for you so you can define a template node class with a whole bunch of different execute methods. It then generates all the actual node classes and the code to do the type checks and swapping behind the scenes.

There's lots more in Truffle to do with supporting debuggers, profilers, language interop etc, but that's the gist of it.

All these techniques added together give you a high level API for building HotSpot or V8 style advanced speculating JITCs, with little more than a specially written interpreter. It's not entirely automatic, but it's far easier than any other framework out there.

Re: GraalVM

#92

Earlier quoted context omitted.

As a counter-point, Quarkus [1] has mature support for building Java apps as native binaries via GraalVM, with a large number of extensions that enable 3rd party libraries to be used in native binaries, and people use this successfully in production. Issues from the past, like lack of debug symbol support with GraalVM CE, have also been resolved by now. Shameless plug for one real-world usage: the search feature of m…

While what Quarkus promises sounds delightful, the actual dev experience is still not something I'd recommend. The amount of libraries one can use is still very limited. The compiler errors when one chooses to step outside of the approved list of supported libraries, are very cryptic (or non-existent). In my case, I tried to use Apache Freemarker for templating. I could've tried Quarkus' own templating library, but i…

Note that Quarkus starts very fast on Java 14 too. With JDK 14 their hello world app starts in about 600msec on my laptop. HotSpot got optimised a lot over the years and Quarkus gets some of its speed boost by just doing less dynamic stuff at startup.

Re: GraalVM

#93
post #90

Earlier quoted context omitted.

That's the worrying thing to me. Oracle's business model seems to be to keep prices reasonable just as long as it takes to build up a customer base, and then start squeezing. I'm fascinated by GraalVM, but I'm hesitant to take even one step down a path that leads to Oracle using my actual software implementation platform to shake me down for money. I'd actually rather take the development effort and performance hit o…

Most of GraalVM is open source. If anything, their biggest commercial error may be open sourcing too much. GraalVM EE is quite expensive for what it adds over the open source versions.

Is there a summary anywhere of what parts/features are and aren't open source?

Re: GraalVM

#94
post #64
post #52

Earlier quoted context omitted.

> The reason Java never got to the desktop (including browser) I believe was the startup time. I think it's mainly the fact that it has always been very difficult to create executable files. Running java requires you to install the given version of JRE instead of just downloading an application and starting it.

It was always very easy, just package the JRE with the application, use an installer like any other desktop application, or buy one of those commercial AOT compilers available since the start of century.

But that takes a lot of space

You could not fit it on a floppy

Re: GraalVM

#95
post #64

Earlier quoted context omitted.

It was always very easy, just package the JRE with the application, use an installer like any other desktop application, or buy one of those commercial AOT compilers available since the start of century.

But that takes a lot of space You could not fit it on a floppy

The 8 and 16 bit home computer scene are way past us.

Re: GraalVM

#96
post #74

Earlier quoted context omitted.

> Java never got to the desktop (including browser) What do you mean by this? There are a lot of desktop Java applications...

I'd like to hear about them, because apart from enterprise applications, there aren't that many left. Electron is the new desktop Java.

I wouldn't call them dominant by any stretch, but I wouldn't call them uncommon. Ones I use somewhere between daily and semi-regularly:

* Pycharm * Datagrip * Charles * JDiskReport * TexturePacker * Android Studio and associated tools * Apache Directory Studio * Zed attack proxy

That's just stuff that I've used recently and on an ongoing basis... I feel like I see quite a bit more of it.

Re: GraalVM

#97
post #80
post #79

Earlier quoted context omitted.

Does Apple sue its customers?

Depends on how far people doing Hackintosh could be considered customers, and there are plenty of examples with Apple lawyers visiting the courtroom, but don't let that get into the way of hating Oracle.

No more than the people who pirate Windows, I would think.

Re: GraalVM

#98
post #62
post #54

I have hard time being deeply invested in GraalVM considering that the most interesting stuff tends to be kept proprietary (e.g. profile-guided optimizations).

Do you also have the same issue with LLVM, given the goodies that Apple, Sony, ARM and many others don't upstream?

Oracle is a law firm, others are technology firms.

Re: GraalVM

#99
post #6

Earlier quoted context omitted.

Java’s linker is based on an open world assumption, where you can add arbitrary stuff to the classpath and dynamically link it at runtime. This defeats most compiler optimizations, so you’re left with the JIT. GraalVM makes a closed world assumption, so it can do things like dead code elimination (for library functions that don’t get called), and apply static analysis to inline virtual method calls, perform constant…

> This lets it greatly outperform the JVM, at the expense of some rarely used functionality. Outperform in what metric? Startup time? Granted. Anything else? Not so much. >Basically, it’s like switching from the JIT to a C compiler’s -O3. Yeah and that would be pretty bad (it's not a good analogy to begin with). "-O3" doesn't have anything that a JIT couldn't have. The only advantage is again, startup time. JIT compi…

Hehehe the old AOT vs JIT debate.

JITs can do great optimizations in theory. But in practice they have only so much time to do optimizations.

And startup time, binary distribution size, memory footprint matter too.

Post reply on HN