threatgrid / asami

A graph store for Clojure and ClojureScript
Eclipse Public License 1.0
637 stars 29 forks source link

AOT query compilation? #100

Open vvvvalvalval opened 3 years ago

vvvvalvalval commented 3 years ago

Problem: Clojure Datalog engines tend to have parsing and interpretation overhead, which is frequently significant, and I suspect Asami is no exception.

For example, a Datomic Datalog query will typically have a 'startup time' of 100µs, even if it does nothing more than an index lookup that would take 1µs when done manually.

Since there's already query planning, it would be very nice to be able to compile queries AOT (potentially with partial arguments) into a function. eval is likely not needed - planning ahead-of-time into a dynamic composition of functions is probably quite enough to trim most of the overhead.

quoll commented 3 years ago

Query planning is based on the current contents of the database, which means that AOT can't be done on the plan. If the user needs a particular plan, then an argument can be passed in to skip the planning stage.

Queries are provided as either a string, sequence or a map. If it's a string, then that needs parsing. If it's a sequence, then it needs to be split up into the clauses (:find, :in, :where, etc). That's the internal representation. I'm not sure how much AOT can be done after that.

This may be why simple queries in Asami are happening much faster than in Datomic.

Operations to do a simple lookup via {:find ?v :where [:my/entity :value ?v]}

It's possible to have some of this done ahead of time, but there's not a lot of scope.

quoll commented 3 years ago

Hi Valentin. Do you have suggestions on what to do here? I'm prepared to explore it, but don't see what to do with it for the moment. One possibility is to compile certain types of queries into functions that can be called on the storage. Another is to cache query plans, since the same queries tend to be run frequently, though this needs to be careful, as changing the data in the database will invalidate an existing plan.

If no one has a suggestion in a couple of weeks, then I'll consider closing this issue.

vvvvalvalval commented 3 years ago

@quoll I have no immediate suggestion - if you're not comfortable with long-standing issues feel free to close this one.