Skip to content

Commit f5aeee3

Browse files
committed
add a small cache to the regex implementation
1 parent 28edab6 commit f5aeee3

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

core/queryalgebra/evaluation/src/main/java/org/eclipse/rdf4j/query/algebra/evaluation/impl/evaluationsteps/RegexValueEvaluationStepSupplier.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class RegexValueEvaluationStepSupplier {
3838
private static final class ChangingRegexQueryValueEvaluationStep implements QueryValueEvaluationStep {
3939
private final Regex node;
4040
private final EvaluationStrategy strategy;
41+
private Value parg;
42+
private Value farg;
43+
private Pattern pattern;
4144

4245
private ChangingRegexQueryValueEvaluationStep(Regex node, EvaluationStrategy strategy) {
4346
this.node = node;
@@ -56,16 +59,33 @@ public Value evaluate(BindingSet bindings) throws QueryEvaluationException {
5659

5760
if (QueryEvaluationUtility.isStringLiteral(arg) && QueryEvaluationUtility.isSimpleLiteral(parg)
5861
&& (farg == null || QueryEvaluationUtility.isSimpleLiteral(farg))) {
62+
63+
Pattern pattern = getPattern((Literal) parg, farg);
64+
5965
String text = ((Literal) arg).getLabel();
60-
String ptn = ((Literal) parg).getLabel();
61-
// TODO should this Pattern be cached?
62-
int f = extractRegexFlags(farg);
63-
Pattern pattern = Pattern.compile(ptn, f);
6466
boolean result = pattern.matcher(text).find();
6567
return BooleanLiteral.valueOf(result);
6668
}
6769
throw new ValueExprEvaluationException();
6870
}
71+
72+
private Pattern getPattern(Literal parg, Value farg) {
73+
if (this.parg == parg && this.farg == farg) {
74+
return pattern;
75+
}
76+
77+
String ptn = parg.getLabel();
78+
int f = extractRegexFlags(farg);
79+
Pattern pattern = Pattern.compile(ptn, f);
80+
81+
// cache the pattern object and the current parg and farg so that we can reuse it if the parg and farg are
82+
// reused or somehow constant
83+
this.parg = parg;
84+
this.farg = farg;
85+
this.pattern = pattern;
86+
87+
return pattern;
88+
}
6989
}
7090

7191
public static QueryValueEvaluationStep make(EvaluationStrategy strategy, Regex node,

0 commit comments

Comments
 (0)