kpreisser/jurassic

JIT-compile the generated IL code immediately, instead of during executing the script

Closed this issue · 0 comments

When starting a Script in Codabix, the script is compiled to IL code, then the node lock is acquired and the script is actually executed. For each function in the script, Jurassic creates a DynamicMethod using System.Reflection.Emit.ILGenerator. This means that the first time any script function is executed, the JIT Compiler generates native code for this function's IL code.

However, it can happen that JITing the IL code may take some time, especially in .NET 6.0 where a performance regression was introduced which increases the time to generate native code: dotnet/runtime#68953

For example, a customer had the issue that the Script was aborted due to the script timeout (15 s) being exceeded. But the issue was not the actual code which took so long; it was the JIT that needed this time to generate the native code for a function when it was called the first time by another script function.

Ideally, when compiling the script source code to IL code (with ScriptEngine.CompileScript()), we should also have the JIT to compile the IL to native code, before we actually execute the script. This ensures the Script Timeout is not applied while the JIT generates the code, and it means Codabix doesn't hang during that time (because of holding the node lock).