Question

After doing much reading on invokeDynamic, i am still a bit confused.One repeating theme seems to be how Clojure doesn't really need it, or at least need it less than other dynamic language implementations on the JVM (Jruby,JPython,Groovy etc.).I didn't understand all the details but it seems that having type annotations is the main reason, which simply eliminate the dynamic dispatch problem.

1- Is it safe to describe invoke-dynamic as way to efficiently implement complex method dispatch scenarios ? (is there more to it ?)

2- Shouldn't having a jit eliminate the need for invoke-dynamic ? The problem seems to arise from the lack of runtime type informations and a jit should have this information.

3-JRuby seems to booth have a jit and use invokdynamic,Why ?

Était-ce utile?

La solution

The main reason is that Clojure doesn't "need" invokedynamic is that it doesn't actually do that much dynamic dispatch. In particular:

  • Whenever Clojure has enough type information (e.g. calling a Java class with a type hint) it does direct method dispatch
  • All functions are called using interface method dispatch (via IFn)
  • The compiler does intelligent inlining in many cases (e.g. primitive functions) avoiding any kind of dispatch
  • Protocol functions are called through interface method dispatch

This covers the vast majority of dispatch cases.

There are of course a few dispatch cases where invokedynamic could prove useful (e.g. multimethods) and it could probably be used to optimise some other parts of Clojure (e.g. var lookup) so Clojure will be able to get some benefits.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top