Skip to content

Commit

Permalink
Merge pull request #102 from jlitola/optimize-rendering-performance
Browse files Browse the repository at this point in the history
Adds Template.renderUnguarded for faster rendering.
  • Loading branch information
bkiers authored Sep 12, 2018
2 parents 2205794 + 6766f82 commit c60964c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/main/java/liqp/ProtectionSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ public void incrementIterations() {
throw new ExceededMaxIterationsException(this.maxIterations);
}
}

public Boolean isRenderTimeLimited() {
return this.maxRenderTimeMillis != Long.MAX_VALUE;
}
}
46 changes: 34 additions & 12 deletions src/main/java/liqp/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,15 @@ public String render(boolean convertValueToMap, String key, Object value, Object
* @return a string denoting the rendered template.
*/
public String render(final Map<String, Object> variables) {
return render(variables, Executors.newSingleThreadExecutor(), true);

if (this.protectionSettings.isRenderTimeLimited()) {
return render(variables, Executors.newSingleThreadExecutor(), true);
} else {
if (this.templateSize > this.protectionSettings.maxTemplateSizeBytes) {
throw new RuntimeException("template exceeds " + this.protectionSettings.maxTemplateSizeBytes + " bytes");
}
return renderUnguarded(variables);
}
}

public String render(final Map<String, Object> variables, ExecutorService executorService, boolean shutdown) {
Expand All @@ -291,18 +299,9 @@ public String render(final Map<String, Object> variables, ExecutorService execut
throw new RuntimeException("template exceeds " + this.protectionSettings.maxTemplateSizeBytes + " bytes");
}

final NodeVisitor visitor = new NodeVisitor(this.tags, this.filters, this.parseSettings);

Callable<String> task = new Callable<String>() {
public String call() throws Exception {
try {
LNode node = visitor.visit(root);
Object rendered = node.render(new TemplateContext(protectionSettings, renderSettings, parseSettings.flavor, variables));
return rendered == null ? "" : String.valueOf(rendered);
}
catch (Exception e) {
throw new RuntimeException(e);
}
public String call() {
return renderUnguarded(variables);
}
};

Expand All @@ -324,6 +323,29 @@ public String call() throws Exception {
}
}

/**
* Renders the template without guards provided by protection settings. This method has about 300x times
* better performance than plain render.
*
* @param variables
* a Map denoting the (possibly nested)
* variables that can be used in this
* Template.
*
* @return a string denoting the rendered template.
*/
public String renderUnguarded(final Map<String, Object> variables) {
final NodeVisitor visitor = new NodeVisitor(this.tags, this.filters, this.parseSettings);
try {
LNode node = visitor.visit(root);
Object rendered = node.render(new TemplateContext(protectionSettings, renderSettings, parseSettings.flavor, variables));
return rendered == null ? "" : String.valueOf(rendered);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}

// Use toStringTree()
@Deprecated
public String toStringAST() {
Expand Down

0 comments on commit c60964c

Please sign in to comment.