Is there a reason why JSR-223 scripting is so slow...
# scripting
b
Is there a reason why JSR-223 scripting is so slow, or is there a better script engine available? I've been running some tests and it takes about second to run even simple expressions when using
KotlinJsr223JvmLocalScriptEngineFactory
, there have been reports of similar issues: https://stackoverflow.com/questions/48777423/running-kotlin-through-jsr-223-is-incredibly-slow https://youtrack.jetbrains.com/issue/KT-17463
a
New scription should be signigicantly better than JSR-223, but documentation is rather limited. You can have an impression from KEEP: https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md
b
Thanks, I’ll check it out
a
@roman.belov used a not-yet-released version of scripting in their jupyter kernel. I used scripting a bit and it is really nice, though I've never measured performance.
b
Yeah, I guess it is not yet available
a
The scripting framework is available for a long time, it lacked some features for REPL. Those are probably added in 1.3.70
b
Hm, maybe that is the one I am already using? It is the Kotlin implementation of JSR-223 for evaluating strings at runtime with
javax.script.ScriptEngineManager
Currently on
1.3.61
a
Kotlin has both JSR-223 implementation and stand-alone scripting with its own API. I think that use of JSR-223 engine is discouraged unless you really want to use different languages.
b
Ah, okay this makes sense
a
I am not sure, but I think that JSR-223 requires you to interpret string, so you need to recompile script each time you launch it, whereas kotlin-script allows to cache pre-compiled scripts.
b
I think they both expose a JSR-223 API, maybe caching only works on the Kotlin API, but it is a bit confusing because they are very similarly named on Maven Central: https://search.maven.org/search?q=kotlin-scripting vs. https://search.maven.org/search?q=g:org.jetbrains.kotlin%20script are actually different APIs under the hood
This one is faster and only requires one dependency:
Copy code
implementation("org.jetbrains.kotlin:kotlin-scripting-jsr223-embeddable:$kotlinVersion")
The second one is slower and more cumbersome to use:
Copy code
implementation("org.jetbrains.kotlin:kotlin-script-runtime:$kotlinVersion")
implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlinVersion")
implementation("org.jetbrains.kotlin:kotlin-script-util:$kotlinVersion")
But both can be used through the JSR-223 API:
Copy code
import javax.script.ScriptEngineManager

fun eval(code: String) =
    ScriptEngineManager().getEngineByExtension("kts").eval(code)