InvokeDynamic: Is it possible to pass call site bytecode to the bootstrap method and then execute it

StackOverflow https://stackoverflow.com/questions/17248914

  •  01-06-2022
  •  | 
  •  

Question

Let's say I have a custom Java compiler or bytecode agent.

Is it possible to pass call site bytecode to my bootstrapping handler in a way that won't require me to generate an anonymous class to "host" the bytecode inside a synthetic method?

I.e once I have passed the bytecode I want to execute it with as little overhead as possible (assuming I have already got the stack and local variable array just right for the bytecode to work)

Était-ce utile?

La solution 2

Okay so I finally found what I need:

http://www.docjar.com/html/api/sun/invoke/anon/AnonymousClassLoader.java.html

It's not standardised yet and might get ditched but it allows a much lighter touch.

John Rose talks about it on his blog:

https://blogs.oracle.com/jrose/entry/anonymous_classes_in_the_vm

Another option might be to convert the calling byte code to MethodHandles and just pass that through.

Autres conseils

The way that InvokeDynamic works is that the first time the instruction is executed, the appropriate bootstrap method is called. The CallSite object returned by the bootstrap method is executed on this and all subsequent calls to the instruction. The idea is that the VM can handle this more efficiently than ad hoc methods that look up the method every time.

However, this doesn't get around the requirement that all bytecode must be inside a class to be executed. That's just the way the JVM platform works. I'm not sure why you think that creating a new class will incur overhead however. It can be inlined just as well as any other method can. The bigger concern is running out of space in the memory allocated for code due to the extra classes, but I believe Java 7 also provides some library calls to help with that.

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