Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shouldn't having a jit reduce the need for invokeDynamic?

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 ?

like image 401
GreyGeek Avatar asked Feb 02 '26 15:02

GreyGeek


1 Answers

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.

like image 118
mikera Avatar answered Feb 04 '26 05:02

mikera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!