clojure-dev

Ingy döt Net 2024-05-22T19:43:17.008679Z

Somebody told me about importdynamic but looking online it support for it in Clojure seems to have been discussed but not added. Just wondering if it's currently usable or planned.

Alex Miller (Clojure team) 2024-05-22T19:49:04.308609Z

never heard of it - link?

2024-05-22T19:53:02.218929Z

You must mean the jvm invokedynamic instruction

Ingy döt Net 2024-05-22T19:54:18.251509Z

Yes invokedynamic . Apologies.

2024-05-22T20:00:37.950169Z

the instruction is understood by the jvm regardless, so it is usable in that sense

👍 1
2024-05-22T20:01:13.552869Z

there is nothing in a released version of the clojure language that generates that bytecode when compiled

Ingy döt Net 2024-05-22T20:01:46.869619Z

Okay thanks. I think I can work with that. Sorry I didn't look into it enough to ask a better question.

2024-05-22T20:03:42.941339Z

there have been various experiments trying to use it to improve the functioning of existing features in clojure, but nothing conclusive enough to rewrite bits of the compiler to use it seems to have been found

2024-05-22T20:06:19.937099Z

years ago when invokedynamic was first becoming available headius (jruby guy) joked that clojure was "cheating" because so much of it is actually static, so doesn't need invokedynamic to by fast on the jvm

Alex Miller (Clojure team) 2024-05-22T20:06:27.523009Z

the next alpha will use invokedynamic to adapt Clojure IFn's to Java functional interfaces

2024-05-22T20:07:20.684789Z

the flip side of that is it doesn't necessarily get any kind of boost from (at the time) a new feature

Alex Miller (Clojure team) 2024-05-22T20:09:20.433839Z

it was not particularly fast when it was introduced, but they've really improved the ability of methodhandles produced by invokedynamic to get optimized and I expect we will be using it more.

Alex Miller (Clojure team) 2024-05-22T20:10:53.503649Z

https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L1732-L1765 is the new code emitting indy for IFn adapters

Alex Miller (Clojure team) 2024-05-22T20:11:04.500169Z

well, the bottom of it at least

Alex Miller (Clojure team) 2024-05-22T20:12:54.717769Z

that's bootstrapping with LambdaMetaFactory, basically mimicking what Java does with lambdas to adapt a static IFn invoker (fixed static methods in FnInvokers) to look like the target functional interface SAM method

Alex Miller (Clojure team) 2024-05-22T20:16:20.483389Z

which really exposes that Java is all lies, and the jvm is awesome

👍 5
😂 4
2024-05-22T20:18:08.930589Z

the are jvm/java things that are annoying to use with clojure because clojure doesn't have some way in the language to make invokedynamic bytecodes, particularly if I recall some of the methods on varhandles and methodhandles can't actually be called without using invokedynamic

Alex Miller (Clojure team) 2024-05-22T20:23:17.017219Z

yeah, the "signature polymorphic" https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/invoke/MethodHandle.html#invoke(java.lang.Object...) that do dynamic spreading. we came close to fixing that in the course of doing some of this work but were trying not to get distracted

Alex Miller (Clojure team) 2024-05-22T20:27:17.248959Z

you can get around that one by calling asSpreader or by calling invokeWithArguments, but there are some perf consequences

Alex Miller (Clojure team) 2024-05-22T20:30:05.501169Z

we actually reimplemented reflection with cached MethodHandles and spreaders and about 95% of that was pretty great (and it's 100x faster than current reflective calls), but the other 5% was tricky enough that we ultimately bailed out (focus focus)

😮 4
Alex Miller (Clojure team) 2024-05-22T21:01:46.030859Z

really made us ask, what if reflection was almost as fast as direct? would you even care most of the time?

🤔 2
Ludger Solbach 2024-05-23T17:24:23.726949Z

I wouldn't, at least most of the time.

2024-05-23T17:27:47.063569Z

the major reason to avoid reflection(in large production code bases) isn't performance, it is because you want no ambiguities about which method is called

Ludger Solbach 2024-05-23T17:45:02.616649Z

To be more precise, I wouldn't mind reflection, if it was almost as fast as direct in non performance critical code in Java interop, where there often is no real ambiguity.

Alex Miller (Clojure team) 2024-05-23T18:20:27.527099Z

If there is ambiguity at runtime then it’s probably an error either way