diff --git a/bench/datascript/bench/datascript.cljc b/bench/datascript/bench/datascript.cljc index 98c32947..e1e54307 100644 --- a/bench/datascript/bench/datascript.cljc +++ b/bench/datascript/bench/datascript.cljc @@ -110,6 +110,19 @@ [?e :sex :male]] @*db100k))) +(defn bench-q5-shortcircuit [] + (bench/bench + (d/q '[:find ?e ?n ?l ?a ?s ?al + :in $ ?n ?a + :where [?e :name ?n] + [?e :age ?a] + [?e :last-name ?l] + [?e :sex ?s] + [?e :alias ?al]] + @*db100k + "Anastasia" + 35))) + (defn bench-qpred1 [] (bench/bench (d/q '[:find ?e ?s @@ -226,6 +239,7 @@ "q2" bench-q2 "q3" bench-q3 "q4" bench-q4 + "q5-shortcircuit" bench-q5-shortcircuit "qpred1" bench-qpred1 "qpred2" bench-qpred2 "pull-one-entities" bench-pull-one-entities @@ -277,6 +291,7 @@ (bench-q2) (bench-q3) (bench-q4) + (bench-q5-shortcircuit) (bench-qpred1) (bench-qpred2) (bench-pull-one-entities) @@ -298,4 +313,4 @@ (bench-rules-long-30x3) (bench-rules-long-30x5) (bench-freeze) - (bench-thaw)) \ No newline at end of file + (bench-thaw)) diff --git a/src/datascript/query.cljc b/src/datascript/query.cljc index 9ae4ad33..0cea2d15 100644 --- a/src/datascript/query.cljc +++ b/src/datascript/query.cljc @@ -791,12 +791,14 @@ (update context :rels collapse-rels relation)))))) (defn resolve-clause [context clause] - (if (rule? context clause) - (if (source? (first clause)) - (binding [*implicit-source* (get (:sources context) (first clause))] - (resolve-clause context (next clause))) - (update context :rels collapse-rels (solve-rule context clause))) - (-resolve-clause context clause))) + (if (->> (:rels context) (some (comp empty? :tuples))) + context ; The result is empty; short-circuit processing + (if (rule? context clause) + (if (source? (first clause)) + (binding [*implicit-source* (get (:sources context) (first clause))] + (resolve-clause context (next clause))) + (update context :rels collapse-rels (solve-rule context clause))) + (-resolve-clause context clause)))) (defn -q [context clauses] (binding [*implicit-source* (get (:sources context) '$)]