[code-reflection] Integrated: Make downcall method handles for ORT API functions static

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Feb 18 19:28:25 UTC 2025


On Tue, 18 Feb 2025 15:50:19 GMT, Adam Sotona <asotona at openjdk.org> wrote:

> It looks good to me.
> 
> I'm wondering if there are any guides what patterns can be better optimized. OnnxRuntime is a singleton, so the optimization-wise differences between static final and singleton instance final are not clear to me.
> 
> Thanks!

Ideally, having final instance fields in a singleton should work. However, plain final instance fields are not "trusted" by the JVM. So the chain of reference (singleton -> function method handle) is not treated as a "true constant" and, as such, the downcall method handle is not inlined in the caller.

Once [stable values](https://openjdk.org/jeps/502) land, this can be fixed: the singleton can be a `static final StableValue<ONNXRuntime>` which contains many `StableValue<MethodHandle>` - one per function (or better, by using a stable list of method handles). Since stable values are... stable, the JVM can trust them, meaning the chain of references is treated as a true constant.

-------------

PR Comment: https://git.openjdk.org/babylon/pull/322#issuecomment-2666383663


More information about the babylon-dev mailing list