Jérémie,
you definitely can precompile scripts. There might be a better/simpler/more
reliable way, but what I use for years is essentially this (somewhat simplified
for better readability):
===
@Lazy() static emptyBinding=new Binding()
@Lazy() static shell={
def cc=new CompilerConfiguration()
cc.scriptBaseClass=ExpressionScript.name
// ... further setup as need be, compilation customizers etc
new GroovyShell(new GroovyClassLoader(),emptyBinding,cc)
}()
@Lazy(soft=YES) static compiledExpressionCache={ [:] }()
static private final cacheLock=new Object()
static evaluate(Map bind,String expression,representedObject) {
ExpressionScript script
def result=nil
synchronized (cacheLock) {
def scripts=compiledExpressionCache[expression].get()
if (!scripts) {
try { script=shell.parse(expr) }
catch (exc) {
result="ERROR: $exc.message IN EXPRESSION '$expression'"
}
shell.classLoader.clearCache()
if (script) compiledExpressionCache[expression]=new
java.lang.ref.SoftReference([script])
} else {
script=scripts.find { ExpressionScript sc ->
sc.representedObject==null // represented object used
(also) as a flag that the script is being processed
}
if (!script) {
script=scripts[0].getClass().newInstance()
scripts<<script
}
}
script.binding=new Binding(bind) // beware: setting bindins ZEROES
representedObject!
script.representedObject=representedObject
}
if (!result) try { result=script.run() }} finally {
script.binding=emptyBinding // also zeroes representedObject
}
result
}
===
All the best,
OC
> On 23. 7. 2022, at 15:02, Jérémie <[email protected]> wrote:
>
> Hello,
>
> I am using Groovy 3 in my project, but I had to turn off the new parser
> because of its performance.
> In my benchmark, the loading time of most of my scripts are multiplied by 12.
> I didn't succeed in reducing this time by using the system properties, so I
> had to turn it off.
>
> In Groovy 4, the old parser has been removed (?). Are the perf of the new
> parser the same as of Groovy 3 ?
> If not, is there a way to precompile these scripts ? (they are not real
> groovy classes but custom Script classes).
>
> Regards,
> Jérémie