Skip to content

Commit be01745

Browse files
[MOD] Optimizations: mayBe(Array|Map|Function) checks unified
1 parent f5ab2ce commit be01745

33 files changed

Lines changed: 62 additions & 77 deletions

basex-core/src/main/java/org/basex/query/expr/Arith.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public Expr optimize(final CompileContext cc) throws QueryException {
5959
final boolean numbers = type1.isNumberOrUntyped() && type2.isNumberOrUntyped();
6060

6161
final Type type = calc.type(type1, type2);
62-
final boolean noArray = !st1.mayBeArray() && !st2.mayBeArray();
63-
final boolean oneOrMore = noArray && st1.oneOrMore() && st2.oneOrMore();
62+
final boolean noArrays = !st1.mayBeFunction() && !st2.mayBeFunction();
63+
final boolean oneOrMore = noArrays && st1.oneOrMore() && st2.oneOrMore();
6464
exprType.assign(type, oneOrMore ? Occ.EXACTLY_ONE : Occ.ZERO_OR_ONE);
6565

6666
Expr expr = emptyExpr();
@@ -72,7 +72,7 @@ public Expr optimize(final CompileContext cc) throws QueryException {
7272
if(expr == this && Function.COUNT.is(expr1) && calc == Calc.ADD && Function.COUNT.is(expr2)) {
7373
expr = cc.function(Function.COUNT, info, List.get(cc, info, expr1.arg(0), expr2.arg(0)));
7474
}
75-
if(expr == this && numbers && noArray && st1.one() && st2.one()) {
75+
if(expr == this && numbers && noArrays && st1.one() && st2.one()) {
7676
// example: number($a) + 0 → number($a)
7777
final Expr ex = calc.optimize(expr1, expr2, info, cc);
7878
if(ex != null) {

basex-core/src/main/java/org/basex/query/expr/CmpG.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public final Expr optimize(final CompileContext cc) throws QueryException {
105105
}
106106

107107
// choose best implementation
108-
if(st1.zeroOrOne() && !st1.mayBeArray() && st2.zeroOrOne() && !st2.mayBeArray()) {
108+
if(st1.zeroOrOne() && !st1.mayBeFunction() && st2.zeroOrOne() && !st2.mayBeFunction()) {
109109
// simple comparisons
110110
if(!(this instanceof CmpSimpleG)) expr = copyType(new CmpSimpleG(expr1, expr2, op, info));
111111
} else if(op == CmpOp.EQ && sc().collation == null && !st2.zeroOrOne() && (
@@ -242,7 +242,7 @@ final boolean eval(final Item item1, final Item item2) throws QueryException {
242242
public final CmpG invert() {
243243
final Expr expr1 = exprs[0], expr2 = exprs[1];
244244
final SeqType st1 = expr1.seqType(), st2 = expr2.seqType();
245-
return st1.one() && !st1.mayBeArray() && st2.one() && !st2.mayBeArray() ?
245+
return st1.one() && !st1.mayBeFunction() && st2.one() && !st2.mayBeFunction() ?
246246
new CmpG(info, expr1, expr2, op.invert()) : null;
247247
}
248248

basex-core/src/main/java/org/basex/query/expr/CmpIR.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public Expr optimize(final CompileContext cc) throws QueryException {
109109
expr = expr.simplifyFor(Simplify.NUMBER, cc);
110110

111111
final SeqType st = expr.seqType();
112-
single = st.zeroOrOne() && !st.mayBeArray();
112+
single = st.zeroOrOne() && !st.mayBeFunction();
113113

114114
// position() = 1 to 2 → pos: 1, 2
115115
if(Function.POSITION.is(expr)) return cc.replaceWith(this, IntPos.get(min, max, info));

basex-core/src/main/java/org/basex/query/expr/CmpR.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public Expr optimize(final CompileContext cc) throws QueryException {
120120
expr = expr.simplifyFor(Simplify.NUMBER, cc);
121121

122122
final SeqType st = expr.seqType();
123-
single = st.zeroOrOne() && !st.mayBeArray();
123+
single = st.zeroOrOne() && !st.mayBeFunction();
124124

125125
// position() = .1e0 → false()
126126
if(Function.POSITION.is(expr)) {

basex-core/src/main/java/org/basex/query/expr/CmpSR.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Expr optimize(final CompileContext cc) throws QueryException {
6464
expr = expr.simplifyFor(Simplify.STRING, cc);
6565

6666
final SeqType st = expr.seqType();
67-
single = st.zeroOrOne() && !st.mayBeArray();
67+
single = st.zeroOrOne() && !st.mayBeFunction();
6868

6969
return expr instanceof Value ? cc.preEval(this) : this;
7070
}

basex-core/src/main/java/org/basex/query/expr/CmpV.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Expr optimize(final CompileContext cc) throws QueryException {
5555
if(expr == this) {
5656
// restrict type
5757
final SeqType st1 = exprs[0].seqType(), st2 = exprs[1].seqType();
58-
if(st1.oneOrMore() && !st1.mayBeArray() && st2.oneOrMore() && !st2.mayBeArray()) {
58+
if(st1.oneOrMore() && !st1.mayBeFunction() && st2.oneOrMore() && !st2.mayBeFunction()) {
5959
exprType.assign(Occ.EXACTLY_ONE);
6060
}
6161
}
@@ -121,7 +121,7 @@ private boolean test(final Item item1, final Item item2) throws QueryException {
121121
public Expr invert() {
122122
final Expr expr1 = exprs[0], expr2 = exprs[1];
123123
final SeqType st1 = expr1.seqType(), st2 = expr2.seqType();
124-
return st1.one() && !st1.mayBeArray() && st2.one() && !st2.mayBeArray() ?
124+
return st1.one() && !st1.mayBeFunction() && st2.one() && !st2.mayBeFunction() ?
125125
new CmpV(info, expr1, expr2, op.invert()) : null;
126126
}
127127

basex-core/src/main/java/org/basex/query/expr/Convert.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final SeqType castType() {
4747
if(type instanceof ListType) {
4848
type = type.atomic();
4949
occ = Occ.ZERO_OR_MORE;
50-
} else if(occ == Occ.ZERO_OR_ONE && est.oneOrMore() && !est.mayBeArray()) {
50+
} else if(occ == Occ.ZERO_OR_ONE && est.oneOrMore() && !est.mayBeFunction()) {
5151
occ = Occ.EXACTLY_ONE;
5252
}
5353
return SeqType.get(type, occ);
@@ -60,7 +60,7 @@ public final SeqType castType() {
6060
*/
6161
final Boolean castable(final SeqType castType) {
6262
final SeqType est = expr.seqType();
63-
if(!est.mayBeArray()) {
63+
if(!est.mayBeFunction()) {
6464
final long es = expr.size();
6565
if(es != -1 && (es < castType.occ.min || es > castType.occ.max)) return false;
6666

@@ -80,7 +80,7 @@ final Boolean castable(final SeqType castType) {
8080
final Expr simplify(final SeqType castType, final CompileContext cc) {
8181
final SeqType est = expr.seqType();
8282
Expr arg = null;
83-
if(est.one() && !est.mayBeArray() && castType.type.instanceOf(BasicType.NUMERIC)) {
83+
if(est.one() && !est.mayBeFunction() && castType.type.instanceOf(BasicType.NUMERIC)) {
8484
// xs:int(string(I))
8585
// xs:int(xs:double(I)) → xs:int(I)
8686
arg = FnNumber.simplify(expr, cc);

basex-core/src/main/java/org/basex/query/expr/Expr.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Iter atomIter(final QueryContext qc, final InputInfo ii) throws QueryExce
102102
final SeqType st = seqType();
103103
if(st.type.instanceOf(BasicType.ANY_ATOMIC_TYPE)) return iter;
104104
long size = iter.size();
105-
if(size != -1 && st.mayBeArray()) size = -1;
105+
if(size != -1 && st.mayBeFunction()) size = -1;
106106
return new AtomIter(iter, qc, ii, size);
107107
}
108108

basex-core/src/main/java/org/basex/query/expr/Lookup.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Expr optimize(final CompileContext cc) throws QueryException {
5555
final SeqType kt = keys.seqType();
5656
final SeqType st = map ? ((MapType) tp).valueType() : ((ArrayType) tp).valueType();
5757
Occ occ = st.occ;
58-
if(inputs.size() != 1 || keys == WILDCARD || !kt.one() || kt.mayBeArray()) {
58+
if(inputs.size() != 1 || keys == WILDCARD || !kt.one() || kt.mayBeFunction()) {
5959
// key is wildcard, or expressions yield no single item
6060
occ = occ.union(Occ.ZERO_OR_MORE);
6161
} else if(map) {
@@ -75,7 +75,7 @@ public Expr optimize(final CompileContext cc) throws QueryException {
7575
private Expr opt(final CompileContext cc) throws QueryException {
7676
final Expr input = exprs[0], keys = exprs[1];
7777
final long is = input.size();
78-
final long ks = keys.seqType().mayBeArray() || keys.has(Flag.NDT) ? -1 : keys.size();
78+
final long ks = keys.seqType().mayBeFunction() || keys.has(Flag.NDT) ? -1 : keys.size();
7979
if(ks == 0) return keys;
8080

8181
final Type it = input.seqType().type;

basex-core/src/main/java/org/basex/query/expr/TypeCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Expr optimize(final CompileContext cc) throws QueryException {
5252
cardinality = nst.instanceOf(st);
5353

5454
// refine type check (ignore arrays as coerced result may have a different size)
55-
if(!et.mayBeArray() || !type.instanceOf(BasicType.ANY_ATOMIC_TYPE)) {
55+
if(!et.mayBeFunction() || !type.instanceOf(BasicType.ANY_ATOMIC_TYPE)) {
5656
// occurrence indicator:
5757
// exactly-one/one-or-more → exactly-one
5858
final Occ nocc = et.occ.intersect(st.occ);

0 commit comments

Comments
 (0)