News for hackers. Stuff that matters.
Re:Graal and Truffle could accelerate programming language d... by nostrademons on Tuesday July 19, 2016 @01:27PM (#12123216)
There's actually a fairly long history of cross-language VMs, with various degrees of success. What usually happens is that they work fine for languages that look, semantically, basically like the native language on the VM. So LLVM works well as long as your language is mostly like C (C, C++, Objective-C, Rust, Swift). Parrot works if you language is mostly like Perl 6 (Perl, Python, PHP, Ruby). .NET works if your language is mostly like C# (C#, F#, Boo, IronPython). The JVM works if your language is mostly like Java (Java, Clojure, Scala, Kotlin). Even PyPy has frontends for Ruby, PHP, Smalltalk, and Prolog.

"Mostly" in this case means semantically, not syntactically. It's things like concurrency model; floating point behavior; memory layout; FFI; semantics of basic libraries; performance characteristics; and level of dynamism. You can write a Python frontend for both the JVM and CLR, and it will look like Python but act like Java and .NET, respectively, with no guarantee that native CPython libraries will work on it.

The problem is that this is where basically all the interesting language-design research is. I wouldn't use Rust for the syntax; I'd use it because I want properties like complete memory safety, manual control over memory allocation, easy (and fast!) access to C libraries, and short startup time. These are all things that Truffle explicitly does not deliver.

It's a great tool if you work in the JVM ecosystem and want to play around as a language designer. But most of the interesting languages lately have created their own ecosystems, and they succeed by solving problems so fundamental that people will put up with having to learn a new ecosystem to gain the benefits they offer.

Re:Graal and Truffle could accelerate programming language d... by eregon on Tuesday July 19, 2016 @02:09PM (#12123575)
To add to the examples, Truffle currently has interpreters for unmanaged languages such as C/Fortran (with Sulong), dynamic languages such as Ruby and JS, statistics/math like R and functional like Clojure. The interopability is done in a way that does not need a common representation or object layout.

> I wouldn't use Rust for the syntax; I'd use it because I want properties like complete memory safety, manual control over memory allocation, easy (and fast!) access to C libraries, and short startup time. These are all things that Truffle explicitly does not deliver.

Truffle itself does not do it, but it is possible to have a Truffle interpreter with these requirements. Memory safety can be enforced at the frontend compiler level like Rust does I believe. Memory can be allocated directly with Unsafe or calling libc functions. GNFI [1] allows for fast calls to native libraries. Startup can be improved radically with SubstrateVM, but it is currently closed-source. There are many ways to improve the JVM startup, but not necessarily convenient (but then you don't need to wait to compile your program either to start running it).

[1] http://dl.acm.org/citation.cfm?id=2500832

Re:Graal and Truffle could accelerate programming language d... by nostrademons on Tuesday July 19, 2016 @02:35PM (#12123749)
The big question is "Would you want to use this in a mission-critical project?" Emscripten lets you write webapps in C, for example, but outside of sharing common libraries across server/Android/iOS/web clients, few people use it.

Pretty much every professional job I've had used polyglot programming of some sort - when I was in financial software it was mixed Java/C/Fortran numerics, when I founded my first startup it was polyglot Javascript/ActionScript, and at Google it was a large server that was first Python/C++ and then Java/C++. The boundary between languages is always problematic. And the reason for that is because you choose memory layout to make certain trade-offs around the access patterns for that data. Do you inline data structures in a vector, or chase pointers? Do objects carry their type information with them so you can manipulate them dynamically, or do they throw it away at compile-time for greater efficiency? Do you get O(1) string indexing or native UTF-8? Do you allocate on the stack or the heap? Do you copy, borrow, move, or COW?

And then when you go to switch representations to call into another language, you pay a cost to convert all the data structures you might be touching. In many cases, that cost could be more than you saved by using optimized representations in the first place.

Re:Graal and Truffle could accelerate programming language d... by trishume on Tuesday July 19, 2016 @02:56PM (#12123934)
That's exactly how Truffle's cross-language-interface works though. Instead of paying the high cost of conversion, data stays in its existing representation and it is the interface code that is recompiled to fit.

An example is JRuby+Truffle where in its C extensions, pointers to Ruby objects act to the C code as if they are pointers to MRI data structures, but behind the scenes Graal compiles code that accesses them like the JRuby objects that they are.

The C-memory to Java GC transition doesn't work as well, but you can get around that by implementing C-memory using the Java GC like the Managed-C paper does, alternative implementations only have to keep the same behaviour, not the same underlying data representation.

Re:Graal and Truffle could accelerate programming language d... by nostrademons on Tuesday July 19, 2016 @03:03PM (#12123990)
Interesting. What happens if the native memory layout doesn't support a feature that the extension code requires? For example, what if a JRuby+Truffle script calls into a C library, which allocates a struct and invokes a Ruby callback with it, which then wants to access the fields via reflection? C doesn't normally include type information with its structs, so how would the Ruby code know the memory layout of the object?

What if it's not the C extension in your project that does the allocation, but a closed-source third-party library that your C code calls into? Do you need to recompile all source code to generate the appropriate typemaps?

Re:Graal and Truffle could accelerate programming language d... by thomaswue on Tuesday July 19, 2016 @06:52PM (#12125563)
Yes. Sulong assumes that at least the LLVM bitcode including debug information is available. It does not work with third party machine code binaries where no source information is available.
Re:Graal and Truffle could accelerate programming language d... by thomaswue on Tuesday July 19, 2016 @02:52PM (#12123902)
The idea of Truffle language interoperability is to avoid switching the representation at the language boundary. Objects carry their type information with them that includes the semantics on how to access their properties. This also allows Truffle to fake up the existence of an object without actually performing allocations (e.g., a table in raw buffer format can be interpreted as an array of objects). This clear separation of logical and physical layout enables efficient data representation in the context of higher level languages that typically suffer from pointer chasing and object header overheads.
Re:Graal and Truffle could accelerate programming language d... by nostrademons on Tuesday July 19, 2016 @02:56PM (#12123931)
Right, and my point is that this won't work unless you dictate a common memory layout for all Truffle objects. And if you do that, then you lose the ability to make trade-offs about object representation that depend upon access patterns that only the programmer can know. The whole reason we have different programming languages is because not all domains of computation face the same trade-offs.
Re:Graal and Truffle could accelerate programming language d... by thomaswue on Tuesday July 19, 2016 @03:01PM (#12123977)
It works without common memory layout between Truffle languages. It even simplifies the ability to use diverse physical memory layouts within the same language. The programmer specifies the logical layout based on the semantics of a language. The runtime decides how to map this logical layout onto the physical hardware. It can take into account the trade-offs the programmer decided to choose.
Re:Graal and Truffle could accelerate programming language d... by nostrademons on Tuesday July 19, 2016 @03:09PM (#12124045)
Right, and my point is that the reason people continue to use C++ or Rust over JVM languages is because there are some use-cases where the JVM's decision about how to map the logical layout onto physical memory causes unacceptably high memory usage and/or cache misses, or prevents them from taking advantage of clever serialization formats (Cap'n Proto, for example, loses much of its performance benefits on the JVM without the use of sun.misc.unsafe). Will Truffle allow the programmer to override Graal's decisions on this and manually specify memory layout? And if so, how are conversions between different language formats specified?

If you already buy the JVM's premise of "just let the compiler do it, we'll figure out the most efficient representation", then this is a non-issue. But there are still programmers out there who believe that Rust or C++ or Go or CPython or whatever presents a better memory layout for the tasks that they wish to accomplish, or language designers who think they can do better than all of the above, and these are the users that Graal+Truffle is trying to win over. What's the story for them?

Re:Graal and Truffle could accelerate programming language d... by thomaswue on Tuesday July 19, 2016 @03:32PM (#12124201)
The default when running static languages like C++ or Rust or Go via Sulong is to stick to the memory layout chosen by the programmer. There is no conversion to typical JVM memory layouts. Nor does running on the JVM has major restrictions for the framework (we are working on a version that does not run on the JVM in the SubstrateVM project). What Graal+Truffle allows is for library writers and language designers to invent new representations without any restrictions on the layout and have them interact with the rest of the system. We want programmers to use the best language for the task. And even combine multiple languages within one program. Foreign objects can be passed around freely as parameters and local variables, but there are restrictions when building combined data structures - e.g., if you have a highly optimized native data structure, it is not possible to install a pointer to a JavaScript object in it without performance loss.
Re:Graal and Truffle could accelerate programming language d... by nostrademons on Tuesday July 19, 2016 @04:03PM (#12124424)
Interesting. So, if I'm understanding this correctly - Truffle allows a language designer to specify a certain logical layout for data structures, along with hints for how this will get converted to a physical memory layout. Language implementors also get the full power of Graal for lowering their AST to machine code and other compiler tasks. On cross-language boundaries, it generates automatic accessors for other languages to access that data, using the logical layout to identify how particular fields need to be pulled out and manipulated, but not requiring that the full data structure be converted across the foreign-call boundary. One consequence of this is that nesting & embedding of data structures may require an explicit conversion, since if an object is a hashtable in Javascript but a packed series of fields in C, it's obviously not going to fit.

Sorta like SWIG++? If you could do SWIG but never require that an end-user write a typemap or debug a crash themself, there'd probably be a big market for that.

Re:Graal and Truffle could accelerate programming language d... by thomaswue on Tuesday July 19, 2016 @05:06PM (#12124902)
Yes, this is an excellent description.
Re:Graal and Truffle could accelerate programming language d... by kentonv on Wednesday July 20, 2016 @01:03AM (#12127004)
> Cap'n Proto, for example, loses much of its performance benefits on the JVM without the use of sun.misc.unsafe

Not actually true. `ByteBuffer` lets you read integers from arbitrary offsets, which is all Cap'n Proto really wants.

Re:Graal and Truffle could accelerate programming language d... by stcredzero on Tuesday July 19, 2016 @01:56PM (#12123487)
I agree with your entire comment. However, I also agree the point the op authors make about tooling being reimplemented time and time again, taking years to get anywhere near the point represented by the 1st 9-point wishlist. What the programming field could do, which would be awesome, would be to implement language category backends that follow your "mostly like" categorization. These backends would be engineered to satisfy the wish lists, but be tailored to certain specific domains. (System programming, hard real-time, soft-real-time, high level dynamic programming...) From what I can see, we're a good part of the way there already. (Graal and Truffle, LLVM, GUILE)

"Little languages" can have significant advantages over APIs in terms of flexibility, optimization, and conceptual fit with specific domains. Why not create infrastructure to not only de-duplicate work creating tooling for languages while ensuring everyone always had 1st-class tooling and making "little languages" as easy to create as APIs?

Re:Graal and Truffle could accelerate programming language d... by infinite8s on Wednesday July 20, 2016 @06:24AM (#12127958)
We already that in the form of Lisp and Smalltalk. Sadly that's not what the majority of programmers want to use.
Re:Graal and Truffle could accelerate programming language d... by stcredzero on Wednesday July 20, 2016 @12:44PM (#12130449)
We need something with the capabilities of Lisp and Smalltalk but with syntax more like Ruby or Python.
Re:Graal and Truffle could accelerate programming language d... by Cacti on Tuesday July 19, 2016 @02:04PM (#12123539)
We should be clear here by what we mean by language.

It's perfectly possible and manageable to create cross-language VMs and to translate pretty accurately between languages. However, what breaks down, as you alluded to, is that people expect the standard libraries, and the libraries of others, to work interchangeably, and that is orders of magnitudes more complex (mostly because you are, by definition, working closer to the metal).

So, I need to ask, how much research is being done in giving the standard library as much consideration as the language? Do we have a grasp on what it would take to treat the stdlib as a "first class" citizen?

Re:Graal and Truffle could accelerate programming language d... by tomphoolery on Tuesday July 19, 2016 @06:46PM (#12125527)
> Do we have a grasp on what it would take to treat the stdlib as a "first class" citizen?

One of the solutions to this problem could be having package management in your language be a first-class citizen, then treating each part of the stdlib as a separate package. If using stdlib code is just as simple as using 3rd-party libraries, then why need a stdlib at all?

Re:Graal and Truffle could accelerate programming language d... by nostrademons on Tuesday July 19, 2016 @08:53PM (#12126108)
It's because languages are communication tools as well as compilers, and the stdlib is part of the common vocabulary that the rest of the language ecosystem shares.

Without a decent stdlib, you end up with the pre-STL C++ situation, where every library & framework declares its own string, vector, and hashmap classes and you need slow, verbose, and error-prone routines to convert between them. The main benefit of having these in the stdlib isn't that you don't have to re-implement them, it's that every third-party lib knows exactly which class represents the concepts of text, sequences, and dictionaries.

The same goes for many other types, eg. Promises, Paths, URIs, Dates, Input/OutputStreams, Iterators, etc. Indeed, when one of the stdlib types is misdesigned (eg. java.util.Date), you get an utter mess as the community converges on an alternative (eg. JodaTime) and then the alternative API is folded back into the stdlib (Java 8).

Luckily, we're learning just which types need to be in the stdlib and which can be outsourced to a third-party package manager. In particular, a lot of serialization/parsing formats are better off in a package manager, as long as the language defines an annotation system that can be used to define which fields must be saved. And giant systems like webservers, webframeworks, or RPC formats are often better off as a third-party library.

Re:Graal and Truffle could accelerate programming language d... by kbenson on Tuesday July 19, 2016 @01:59PM (#12123502)
> It's a great tool if you work in the JVM ecosystem and want to play around as a language designer.

Except that it's not really exposing much of the JVM from what the article mentions, it just uses the JVM as a base. That is, Truffle languages can talk to other truffle languages, but I'm not sure how easy it is for a truffle language to talk to a JVM language.

From everything I read, it seems like people thought Parrot was a good idea so decided to rewrite/reimagine it using the JVM instead of C as a base.

Re:Graal and Truffle could accelerate programming language d... by zzzcpan on Tuesday July 19, 2016 @03:00PM (#12123966)
I liked Parrot, but it didn't move in the right direction. No useful AOT and JIT from the start kind of turned me down.
Re:Graal and Truffle could accelerate programming language d... by kbenson on Tuesday July 19, 2016 @03:40PM (#12124264)
Well, those were always planned features, they just never got around to them, or they never got enough attention before the project basically died out.

As for not moving in the right direction, it was probably far too ambitious to try to develop something like Parrot and something like Perl 6 at the same time, with dependencies on each other. Both projects would have been better served if they did their own things, and Parrot was just seen as "a good candidate for a second implementation" from the Perl 6 perspective, rather than the primary target. It's hard to target a VM that's not done, and it's hard as a VM to support a language that isn't finalized (and has crazy requirements).

Re:Graal and Truffle could accelerate programming language d... by pjmlp on Tuesday July 19, 2016 @01:55PM (#12123472)
Well you have the mainframes model that used kernel JITs for all their official languages, like the OS/400 nowadays IBM i.

On IBM i the language surface is RPG, Cobol, C, C++ and Java.

On other ones you could eventually consider the micro-coded CPUs as a kind of cross language VMs.

Re:Graal and Truffle could accelerate programming language d... by nickpsecurity on Tuesday July 19, 2016 @06:52PM (#12125561)
The ctoss-language compatibility was a feature of OpenVMS systems. The AS/400 family had AOT and/or JIT for all object code in system. They also had PL/S that subsetted PL/I by removing runtime. These tech seem to combine all those capabilities in one stack.
Re:Graal and Truffle could accelerate programming language d... by thaumasiotes on Wednesday July 20, 2016 @03:13AM (#12127356)
> What usually happens is that they work fine for languages that look, semantically, basically like the native language on the VM.

> The JVM works if your language is mostly like Java (Java, Clojure, Scala, Kotlin).

I'm really curious why you're characterizing Clojure as "mostly like Java"?

Re:Graal and Truffle could accelerate programming language d... by qwertyuiop924 on Tuesday July 19, 2016 @03:39PM (#12124251)
Indeed. What if you're writing a Scheme interpreter? You're screwed.
Re:Graal and Truffle could accelerate programming language d... by rev_null on Wednesday July 20, 2016 @09:15AM (#12128756)
As a specific example, Graal is built on the JVM. I'd be interested to see if they have support for languages that require tail call optimization (haskell, scheme) as the JVM cannot work with TCO languages.
Re:Graal and Truffle could accelerate programming language d... by cygx on Wednesday July 20, 2016 @04:34PM (#12132281)
Re:Graal and Truffle could accelerate programming language d... by jackmott on Tuesday July 19, 2016 @02:00PM (#12123510)
F# isn't much like C#, and in fact it does suffer a bit for it, as some of the back end causes compromises in the F# language. Mutable fields in records have secret baggage, functions with arithmetic operators can't be automatically genericized unless you mark them inline, etc
Re:Graal and Truffle could accelerate programming language d... by __s on Tuesday July 19, 2016 @06:00PM (#12125268)
My biggest let down with F# is that NullReferenceException is still a thing. Also a C# function is called as if they're typed to take tuples but then you can't create a tuple & pass it along