pat_shaughnessy
Joined 15 years ago
Joined 15 years ago
This basic algorithm is called "Basic Block Versioning." Maxime Chevalier-Boisvert from Shopify has some great presentations online about this; for example [1].
I believe ZJIT, the newer JIT engine, uses a different approach. I'm exploring that now.
[1] https://www.youtube.com/watch?v=zO9_uTaELCw — RubyConf 2021 - YJIT - Building a new JIT Compiler inside CRuby by Maxime Chevalier Boisvert.
Yes that's something I want to dig into and explore in this chapter... when exactly does Ruby's JIT compiler activate and optimize our code? And you're right: since Ruby will JIT blocks as if they were separate function many loops will be optimized using this simple heuristic.
I suppose this could work if, as you suggest, I manually called out to a lib_c function like printf instead.
The reason I used this particular SQL statement as an example is that ActiveRecord (a Ruby ORM) generates it from a fairly simple, common Ruby expression. I suppose ActiveRecord could be improved to drop the sort when you know there's one one possible match.
Plus I wasn't interested in comparing one DB vs. another, as much as I was interested in understanding how any DB works.
... and this notation almost seems more similar to the underlying math than SQL does.