Caching jsr223 compilation


#1

I am using .kts as config files in my project, and I have quite a number of such files.

The first time I load a .kts it takes about 10 seconds to complete. The second time it takes about 2 seconds. This is quite unsatisfactory as 10 seconds of the startup time is wasted on initializing .JSR223.

Of course some values can be cached, but it is not possible for my case as some of my config files (e.g. translation files) involve lambdas.

Is it possible to cache the compiled bytecode, or otherwise optimize this loading process? The only result I can find on Google is https://stackoverflow.com/q/48777423/3990767, which does not seem to provide any good solutions.


#2

There is nothing available yet out of the box, and it seems non-trivial to provide a full-functioning JSR-223 engine with caching, because it is, in fact, a REPL, so you need to cache a sequence of scripts executed with the same bindings. We might investigate possibilities to implement it at some point though.

Things are easier if you can use non-JSR-223 scripting. In this case, there is an interface for caching and a couple of sample implementations that you can take.


#3

For configuration files it may be better if there was no compilation, but interpretation instead. I’m aware that this is not offered by the Kotlin system currently, but it is technically possible to execute an AST as is (interpret it), possibly with late resolution of dependencies. I don’t think this is anywhere on the Kotlin roadmap though, and I’m not sure it should be.


#4

I don’t exactly get what you mean. How would it be possible to construct an efficient lambda from the interpretation if there is no compilation? If it is just a config with simple values, I could just have cached the values directly.