Skip to content

Instantly share code, notes, and snippets.

@headius
Created August 27, 2012 19:34
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save headius/3491618 to your computer and use it in GitHub Desktop.
Save headius/3491618 to your computer and use it in GitHub Desktop.
JVM + Invokedynamic versus CLR + DLR

Too much for teh twitterz :)

JVM + invokedynamic is in a completely different class than CLR + DLR, for the same reasons that JVM is in a different class than CLR to begin with.

CLR can only do its optimization up-front, before executing code. This is a large part of the reason why C# is designed the way it is: methods are non-virtual by default so they can be statically inlined, types can be specified as value-based so their allocation can be elided, and so on. But even with those language features CLR simply cannot optimize code to the level of a good, warmed-up JVM.

The JVM, on the other hand, optimizes and reoptimizes code while it runs. Regardless of whether methods are virtual/interface-dispatched, whether objects are transient, whether exception-handling is used heavily...the JVM sees through the surface and optimizes code appropriate for how it actually runs. This gives it optimization opportunities that CLR will never have without adding a comparable profiling JIT.

So how does this affect dynamic languages? In several crucial ways.

The CLR's DLR (Dynamic Language Runtime) does optimize dynamic calls by generating code at runtime. This code builds up something like a PIC (polymorphic inline cache) to dispatch to the appropriate target method as quickly as possible. Unfortunately, because the CLR can only do optimization passes ahead of time, before execution, the optimizations in those PICs only apply to the PIC itself; they do not inline to the dynamic call site, and therefore no dynamic calls ever inline. With this comes the loss of the vast majority of optimizations that require inlining, and you end up with better-than-reflection but far-worse-than-inlined performance for dynamic languages.

On the JVM, the story is completely different. Invokedynamic allows the actual in-code call site to be rebound to a chain of "method handles" that eventually lead to the appropriate target method. These handles can implement a direct call, a guarded call, or many complicated patterns of dispatching and inline caching. All that's required is that the logic of making a call be representable as a series of method handles, and the JVM can optimize the original call site as if that logic existed there from the beginning. Further, rebinding call sites at runtime causes the JVM to reoptimize the same code, reevaluating profiles and regenerating native code to match the new "shape" of the site.

None of what invokedynamic does is possible on current CLR implementations, since you obviously can't optimize a late-bound language unless you can delay optimization until runtime.

@xentek
Copy link

xentek commented Aug 27, 2012

💣

@cwinters
Copy link

Not that it matters one way or the other, but I'm just curious: how much of the JVM discussion is specific to Hotspot's implementation and how much stems from the specification?

@headius
Copy link
Author

headius commented Aug 27, 2012

I should have clarified. By JVM here I mean specifically Hotspot, and by CLR I mean Microsoft's implementation of it in .NET. Other JVMs and CLRs will have varying amounts of optimization, but I believe profiling JIT is much more common on the JVM side (e.g. there are several profiling JITs for JVMs, but none I know of for CLR implementations).

@piers7
Copy link

piers7 commented Aug 28, 2012

Is there a missing 'not' in last para?

@headius
Copy link
Author

headius commented Aug 28, 2012

I don't think so...can you show me where you think it should go?

@EssenceSharp
Copy link

Polymorphic inline caching and the Hotspot technology both originated from the Self language (Ungar, Stanford.) They used both at once. See: http://researcher.ibm.com/researcher/view.php?person=us-davidungar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment