tonsky / datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
Eclipse Public License 1.0
5.45k stars 304 forks source link

Query: shortcircuit query when result is guaranteed to be empty #459

Closed galdre closed 7 months ago

galdre commented 7 months ago

The -collect fn, run at the end of a query, correctly shortcircuits with the following comment: "one empty rel means final set has to be empty"

However, while processing the query clauses, there was no shortcircuit. Because the implementation of lookup-pattern is agnostic to existing rels, this means that the query engine will continue slicing the indexes for every tuple that could possibly match every clause until all clauses are exhausted.

This is a one-line change to add a shortcircuit in resolve-clause if any existing relation is empty. The check is cheap, and it should provide a substantial performance boost in many common cases.

Motivation: substantial performance boost for a real-world app

tonsky commented 7 months ago

Awesome! Thank you!

tonsky commented 7 months ago

Published 1.6.2