Skip to content

Commit

Permalink
Add support for current() expression caching
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Oct 4, 2023
1 parent 378e54b commit d444802
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ private static Pair<XPathPathExpr, XPathExpression> getRelativeAndAbsolute(XPath
Queue<XPathExpression> subExpressions = new LinkedList<>(Arrays.asList(a, b));
while (!subExpressions.isEmpty()) {
XPathExpression subExpression = subExpressions.poll();
if (subExpression instanceof XPathPathExpr && ((XPathPathExpr) subExpression).init_context == XPathPathExpr.INIT_CONTEXT_RELATIVE)
relative = (XPathPathExpr) subExpression;
else if (subExpression instanceof XPathPathExpr && ((XPathPathExpr) subExpression).init_context == XPathPathExpr.INIT_CONTEXT_ROOT) {
absolute = subExpression;
if (subExpression instanceof XPathPathExpr) {
if (((XPathPathExpr) subExpression).init_context == XPathPathExpr.INIT_CONTEXT_RELATIVE)
relative = (XPathPathExpr) subExpression;
else {
absolute = subExpression;
}
} else if (subExpression instanceof XPathNumericLiteral || subExpression instanceof XPathStringLiteral) {
absolute = subExpression;
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/org/javarosa/core/model/SelectCachingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,43 @@ public void eqChoiceFilter_inRepeat_onlyEvaluatedOnce() throws Exception {
// Check that we do less than (size of secondary instance) * (number of choice lookups)
assertThat(evaluations, lessThan(8));
}

@Test
public void eqChoiceFiltersInRepeatsWithCurrentPathExpressionsAreOnlyEvaluatedOnce() throws Exception {
Scenario scenario = Scenario.init("Select in repeat", html(
head(
title("Select in repeat"),
model(
mainInstance(
t("data id='repeat-select'",
t("outer",
t("filter"),
t("inner",
t("select"))))),

instance("choices",
item("a", "A"),
item("b", "B")))),
body(
repeat("/data/outer",
input("filter"),
repeat("/data/outer/inner",
select1Dynamic("/data/outer/inner/select", "instance('choices')/root/item[value=current()/../../filter]"))
))));

scenario.answer("/data/outer[0]/filter", "a");
scenario.createNewRepeat("/data/outer[0]/inner");
scenario.answer("/data/outer[1]/filter", "a");
scenario.createNewRepeat("/data/outer[1]/inner");
scenario.createNewRepeat("/data/outer[1]/inner");

int evaluations = Measure.withMeasure(asList("PredicateEvaluation", "IndexEvaluation"), () -> {
scenario.choicesOf("/data/outer[0]/inner[0]/select");
scenario.choicesOf("/data/outer[0]/inner[1]/select");
});

// Check that we do less than (size of secondary instance) * (number of choice lookups)
assertThat(evaluations, lessThan(4));
}
//endregion
}

0 comments on commit d444802

Please sign in to comment.