-
Notifications
You must be signed in to change notification settings - Fork 172
Chained filters optimization #1274
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
Open
hs-lsong
wants to merge
8
commits into
master
Choose a base branch
from
chained-filters-optimization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6be60f4
Chained filters optimization.
hs-lsong 9b5e8b0
Make filter chain optimization configurable and disable for eager mode
hs-lsong 63ff83c
Add performance test for filter chain optimization
hs-lsong 95094de
Add documentation for filter chain optimization
hs-lsong 5877a05
Remove documentation files from PR
hs-lsong a357765
Add parity test for filter chain optimization and fix unknown filter …
hs-lsong 372c0a8
Separate unit tests from performance tests for filter chain optimization
hs-lsong eb83265
Simplify appendStructure by delegating to AstParameters
hs-lsong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
src/main/java/com/hubspot/jinjava/el/ext/AstFilterChain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| package com.hubspot.jinjava.el.ext; | ||
|
|
||
| import com.hubspot.jinjava.interpret.DisabledException; | ||
| import com.hubspot.jinjava.interpret.JinjavaInterpreter; | ||
| import com.hubspot.jinjava.interpret.TemplateError; | ||
| import com.hubspot.jinjava.interpret.TemplateError.ErrorItem; | ||
| import com.hubspot.jinjava.interpret.TemplateError.ErrorReason; | ||
| import com.hubspot.jinjava.interpret.TemplateError.ErrorType; | ||
| import com.hubspot.jinjava.lib.filter.Filter; | ||
| import com.hubspot.jinjava.objects.SafeString; | ||
| import de.odysseus.el.tree.Bindings; | ||
| import de.odysseus.el.tree.impl.ast.AstNode; | ||
| import de.odysseus.el.tree.impl.ast.AstParameters; | ||
| import de.odysseus.el.tree.impl.ast.AstRightValue; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import javax.el.ELContext; | ||
| import javax.el.ELException; | ||
|
|
||
| /** | ||
| * AST node for a chain of filters applied to an input expression. | ||
| * Instead of creating nested AstMethod calls for each filter in a chain like: | ||
| * filter:length.filter(filter:lower.filter(filter:trim.filter(input))) | ||
| * | ||
| * This node represents the entire chain as a single evaluation unit: | ||
| * input|trim|lower|length | ||
| * | ||
| * This optimization reduces: | ||
| * - Filter lookups (done once per filter instead of per AST node traversal) | ||
| * - Method invocation overhead | ||
| * - Object wrapping/unwrapping between filters | ||
| * - Context operations | ||
| */ | ||
| public class AstFilterChain extends AstRightValue { | ||
|
|
||
| protected final AstNode input; | ||
| protected final List<FilterSpec> filterSpecs; | ||
|
|
||
| public AstFilterChain(AstNode input, List<FilterSpec> filterSpecs) { | ||
| this.input = Objects.requireNonNull(input, "Input node cannot be null"); | ||
| this.filterSpecs = Objects.requireNonNull(filterSpecs, "Filter specs cannot be null"); | ||
| if (filterSpecs.isEmpty()) { | ||
| throw new IllegalArgumentException("Filter chain must have at least one filter"); | ||
| } | ||
| } | ||
|
|
||
| public AstNode getInput() { | ||
| return input; | ||
| } | ||
|
|
||
| public List<FilterSpec> getFilterSpecs() { | ||
| return filterSpecs; | ||
| } | ||
|
|
||
| @Override | ||
| public Object eval(Bindings bindings, ELContext context) { | ||
| JinjavaInterpreter interpreter = getInterpreter(context); | ||
|
|
||
| if (interpreter.getContext().isValidationMode()) { | ||
| return ""; | ||
| } | ||
|
|
||
| Object value = input.eval(bindings, context); | ||
|
|
||
| for (FilterSpec spec : filterSpecs) { | ||
| String filterKey = ExtendedParser.FILTER_PREFIX + spec.getName(); | ||
| interpreter.getContext().addResolvedValue(filterKey); | ||
|
Comment on lines
+69
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be tested for parity with regular filter parsing and evaluation |
||
|
|
||
| Filter filter; | ||
| try { | ||
| filter = interpreter.getContext().getFilter(spec.getName()); | ||
| } catch (DisabledException e) { | ||
| interpreter.addError( | ||
| new TemplateError( | ||
| ErrorType.FATAL, | ||
| ErrorReason.DISABLED, | ||
| ErrorItem.FILTER, | ||
| e.getMessage(), | ||
| spec.getName(), | ||
| interpreter.getLineNumber(), | ||
| -1, | ||
| e | ||
| ) | ||
| ); | ||
| return null; | ||
| } | ||
| if (filter == null) { | ||
| return null; | ||
| } | ||
|
|
||
| Object[] args = evaluateFilterArgs(spec, bindings, context); | ||
| Map<String, Object> kwargs = extractNamedParams(args); | ||
| Object[] positionalArgs = extractPositionalArgs(args); | ||
|
|
||
| boolean wasSafeString = value instanceof SafeString; | ||
| if (wasSafeString) { | ||
| value = value.toString(); | ||
| } | ||
|
|
||
| try { | ||
| value = filter.filter(value, interpreter, positionalArgs, kwargs); | ||
| } catch (ELException e) { | ||
| throw e; | ||
| } catch (RuntimeException e) { | ||
| throw new ELException( | ||
| String.format("Error in filter '%s': %s", spec.getName(), e.getMessage()), | ||
| e | ||
| ); | ||
| } | ||
|
|
||
| if (wasSafeString && filter.preserveSafeString() && value instanceof String) { | ||
| value = new SafeString((String) value); | ||
| } | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| protected JinjavaInterpreter getInterpreter(ELContext context) { | ||
| return (JinjavaInterpreter) context | ||
| .getELResolver() | ||
| .getValue(context, null, ExtendedParser.INTERPRETER); | ||
| } | ||
|
|
||
| protected Object[] evaluateFilterArgs( | ||
| FilterSpec spec, | ||
| Bindings bindings, | ||
| ELContext context | ||
| ) { | ||
| AstParameters params = spec.getParams(); | ||
| if (params == null || params.getCardinality() == 0) { | ||
| return new Object[0]; | ||
| } | ||
|
|
||
| Object[] args = new Object[params.getCardinality()]; | ||
| for (int i = 0; i < params.getCardinality(); i++) { | ||
| args[i] = params.getChild(i).eval(bindings, context); | ||
| } | ||
| return args; | ||
| } | ||
|
|
||
| private Map<String, Object> extractNamedParams(Object[] args) { | ||
| Map<String, Object> kwargs = new LinkedHashMap<>(); | ||
| for (Object arg : args) { | ||
| if (arg instanceof NamedParameter) { | ||
| NamedParameter namedParam = (NamedParameter) arg; | ||
| kwargs.put(namedParam.getName(), namedParam.getValue()); | ||
| } | ||
| } | ||
| return kwargs; | ||
| } | ||
|
|
||
| private Object[] extractPositionalArgs(Object[] args) { | ||
| List<Object> positional = new ArrayList<>(); | ||
| for (Object arg : args) { | ||
| if (!(arg instanceof NamedParameter)) { | ||
| positional.add(arg); | ||
| } | ||
| } | ||
| return positional.toArray(); | ||
| } | ||
|
|
||
| @Override | ||
| public void appendStructure(StringBuilder builder, Bindings bindings) { | ||
| input.appendStructure(builder, bindings); | ||
| for (FilterSpec spec : filterSpecs) { | ||
| builder.append('|').append(spec.getName()); | ||
| AstParameters params = spec.getParams(); | ||
| if (params != null && params.getCardinality() > 0) { | ||
| params.appendStructure(builder, bindings); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append(input.toString()); | ||
| for (FilterSpec spec : filterSpecs) { | ||
| sb.append('|').append(spec.toString()); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public int getCardinality() { | ||
| return 1 + filterSpecs.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public AstNode getChild(int i) { | ||
| if (i == 0) { | ||
| return input; | ||
| } | ||
| int filterIndex = i - 1; | ||
| if (filterIndex < filterSpecs.size()) { | ||
| FilterSpec spec = filterSpecs.get(filterIndex); | ||
| return spec.getParams(); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do all the tests pass if this is set to
true?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, just did it.