How do databases execute expressions?
notes.eatonphil.com
notes.eatonphil.com
You aren't using LLVM IR then? I found that if you are a bit careful, LLVM is perfectly capable to lower IR to vector instructions in ARM/X86 automatically.
I've experimented with lowering into x86 "manually" as well, and it did provide gains, but the portability aspects are concerning. And for much of the desirable speedups you ime want more optimization than trivial lowering would give you.
Either way, without the tree walk -> VM move, it'd have been much harder to lower into x86 directly. And you'd still need a fallback for other platforms.
Yes (to me my comment seemed to make that clear, but...).
Didn't really remember how long ago that was, so I dug up the commit. Main commit went in ~6.5 years ago. There were lots of related changes but the main commit is https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit...
I wouldn't expect VMs to become the default design in databases, but it seems like it's getting increasingly common as an IR for query compilation. The ability to have a (comparatively simpler) interpreter for the VM also means you can apply simple fuzzing to great effect: if the results of interpretation vs compilation ever diverge, there's a bug.
I do think that a VM could mess up CPU branch prediction so I can see that as being a problem?
Some people have gone the route of emitting LLVM IR (or similar) for query plans, but this has a cost, too, so only makes sense for repeatedly used queries.
> I do think that a VM could mess up CPU branch prediction so I can see that as being a problem
It definitely happens. It can be partially addressed with things like computed gotos, but it's far from perfect. At least for postgres, it's just that the tree walk overhead was far higher.
The branch prediction handling for opcode dispatch loops definitely improved in CPUs - for Intel somewhere around Haswell (it's been a while, so this is a guess). I remember that, at the time, the gains of using computed goto on my aging workstation (nehalem) were far bigger than a few uarchs later, even when controlling for cache size etc.
Semi related anecdote: Both opcode dispatch switches and computed goto based dispatch are good for stressing compilers. I found newly introduced >= quadratic behavior in all the major c compilers in the years since.
The below summarisation was good
Tree Interpretation: Best for lightweight or embedded systems where resource usage is a concern.
Virtual Machines: Suitable for general-purpose databases that need to handle a variety of query complexities.
JIT Compilation: Ideal for databases that handle complex or long-running queries, where the overhead of JIT can be offset by performance gains.