Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[automation] Cache parsed script in order to improve performance #2057

Merged
merged 1 commit into from
Jan 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class DSLScriptEngine implements javax.script.ScriptEngine {
private final @Nullable DSLScriptContextProvider contextProvider;
private final ScriptContext context = new SimpleScriptContext();

private @Nullable Script parsedScript;

public DSLScriptEngine(org.openhab.core.model.script.engine.ScriptEngine scriptEngine,
@Nullable DSLScriptContextProvider contextProvider) {
this.scriptEngine = scriptEngine;
Expand Down Expand Up @@ -119,11 +121,17 @@ public Object eval(String script) throws ScriptException {
return null;
}
} else {
s = scriptEngine.newScriptFromString(script);
s = parsedScript;
if (s == null) {
s = scriptEngine.newScriptFromString(script);
parsedScript = s;
}
}
IEvaluationContext evalContext = createEvaluationContext(s, specificContext);
return s.execute(evalContext);
} catch (ScriptExecutionException | ScriptParsingException e) {
// in case of error, drop the cached script to make sure, it is re-resolved.
parsedScript = null;
throw new ScriptException(e.getMessage(), modelName, -1);
}
}
Expand All @@ -134,7 +142,6 @@ private DefaultEvaluationContext createEvaluationContext(Script script, IEvaluat
XExpression xExpression = ((ScriptImpl) script).getXExpression();
if (xExpression != null) {
Resource resource = xExpression.eResource();
IEvaluationContext evaluationContext = null;
if (resource instanceof XtextResource) {
IResourceServiceProvider provider = ((XtextResource) resource).getResourceServiceProvider();
parentContext = provider.get(IEvaluationContext.class);
Expand Down