43

What changes to the JVM would most benefit the Scala compiler and runtime?

The dynamic languages will benefit greatly in performance from the introduction of the InvokeDynamic byte code scheduled to arrive in JVM 7 and Scala will probably benefit from tail recursion (not sure if it will appear in JVM 8 or later).

What other changes could Scala, with its present feature set, benefit from in the JVM? Are these changes on the horizon?

Specifically, are there changes to the JVM that would improve performance with closures and functions-as-objects?

Thom Wiggers
  • 6,938
  • 1
  • 39
  • 65
Ralph
  • 31,584
  • 38
  • 145
  • 282

4 Answers4

26

Basically, everything that John Rose has been campaigning for :)

  • Fixnums - To eliminate the cost of boxing/unboxing primitives.

  • Method Handles - Would speed up higher-order functions and allow the JVM to optimise them more effectively. SAM types can sometimes require an awkward flip/flopping between monomorphic and megamorphic call sites that prevents inlining.

  • Continuations - To support asynchronous/concurrent design, as per node.js

  • Interface Injection - Simplify mixin composition and the implementation of roles, as well as eliminating the need for generating some intermediate classes and making structural types possible without reflection in many cases.

  • Tail-call optimisation - Should be a no-brainer :)

Reification is often quoted as something that would benefit Scala's pattern matching, but this would come at a high cost in terms of interop, given the different variance schemes that the two languages use. At this point, I believe that reification may actually cause more harm than it would do good.

I also think it unreasonable to expect anything that would break backwards compatibility in Java. That just ain't gonna happen.

cessationoftime
  • 896
  • 8
  • 17
Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
  • 9
    Indeed: putting reified generics into the VM is *only* going to help Java. Period. It is *exactly* the wrong thing to do. The opposite way is the right way: disentangle the JVM from Java by making the bytecode untyped and letting each language implement their own type system instead of forcing the Java type system on all JVM languages. If you're feeling brave, implement a pluggable type system for JVM bytecode. (Note that having a cheap way to *represent* types in bytecode would be a good thing.) – Jörg W Mittag Apr 19 '11 at 01:51
13

There are a couple of features of Scala that would be better implemented in the JVM, such has:

  • Generics that are accessible at runtime. At the moment, scalac saves the types of generics as hidden fields (if the class in question is a case class). This makes generics in case classes unnecessarily expensive though.

  • Declaration-site variance. Scala specifies the variance of type parameters at the definition site, while Java does so at the call site. This is very unlikely to get fixed though, since it would break all existing Java generic code.

  • Tail call optimization. Scalac can do some tail call optimization on it's own, but only in the simplest (self-recursive) case. Any other tail calls will use stack space like in the JVM.

  • Removal of null pointers. Scala can already handle null refs with Option[A], but because of being on the JVM, the reference to the option itself could be null, or it's parameter could be null. So you don't get a guarantee of non-null-ness like in say Haskell.

EDIT: Added declaration-site variance to list.

keiter
  • 3,534
  • 28
  • 38
  • reification would be a bad thing, unless it were implemented in terms of declaration-site variance. This is *highly* unlikely, given that Java works with use-site variance. reification implemented as per Java's variance would actively harm interop. – Kevin Wright Apr 18 '11 at 22:10
  • @Kevin: Good point! Forgot about that momentarily. – keiter Apr 18 '11 at 22:54
  • Also, good luck on getting nulls dropped... You'll need it :) – Kevin Wright Apr 19 '11 at 17:52
10

Value types would help quite a bit performance wise for tuples and case classes. Escape analysis helps reduce heap allocations of such objects a bit, but at the moment the JVM can't inline some method calls aggressively enough thus can't eliminate heap allocations for these small immutable objects. This leads to heap trashing and 3-4x higher execution time.

Value types also helps increase data locality and reduce memory usage. For example, in a simple Array[(Int, Int)] each array entry will have an overhead of one pointer + object header. With value types this overhead could be eliminated completely.

Jesper Nordenberg
  • 2,104
  • 11
  • 15
  • I've heard rumours that they're coming too. Oracle is just too interested in Java making $$$ from the HPC crowd, who need stack-allocated complex numbers and matrices. – Kevin Wright Apr 20 '11 at 09:47
  • 1
    @Kevin Wright: I heard that Mono is actually quite popular in HPC circles. It's more popular than the JVM, since the CLI *does* have value types and it's more popular than Microsoft's implementation of the CLI, because the CLI allows array indices to be either 32 or 64 bit and Mono chose 64 while MS chose 32. Those HPC guys just *love* arrays with billions of cheap values in them. Given that (and also the existence of [Fortress](http://ProjectFortress.Java.Net/)), it just makes sense for Oracle, even if just to piss off IBM :-) – Jörg W Mittag Apr 21 '11 at 12:16
4

People often focus on InvokeDynamic - without realizing that the MethodHandles framework has plenty of other advantages, even if the method references that MH provides are never dynamically invoked.

MethodHandles are almost like a performant form of "Reflection done right".

Any language which makes heavy use of reflection may well be able to get benefits from using MethodHandles inside the language runtime.

kittylyst
  • 5,640
  • 2
  • 23
  • 36