It seems like Urabe, Shyouhei succeeded to write an efficient optimizer for Ruby: https://github.com/ruby/ruby/pull/1419
Since Ruby and CPython design are similar, maybe we can pick some ideas. It seems like the optimizer is not done yet, the PR is not merged yet.
I don't understand how the optimizer works. An interesting commit: https://github.com/ruby/ruby/pull/1419/commits/d7b376949eb1626b9e5088f907db4...
basic optimization infrastructure
This commit adds on-the-fly ISeq analyzer. It detects an ISeq's purity, i.e. if that ISeq has side-effect or not. Purity is the key concept of whole optimization techniques in general, but in Ruby it is yet more important because there is a method called eval. A pure ISeq is free from eval, while those not pure are stuck in the limbo where any of its side effects _could_ result in (possibly aliased) call to eval. So an optimization tend not be possible against them.
Note however, that the analyzer cannot statically say if the ISeq in question is pure or not. It categorizes an ISeq into 3 states namely pure, not pure, or "unpredictable". The last category is used when for instance there are branches yet to be analyzed, or method calls to another unpredictable ISeq.
An ISeq's purity changes over time, not only by redefinition of methods, but by other optimizations, like, by entering a rarely-taken branch of a formerly-unpredictable ISeq to kick analyzer to fix its purity. Such change propagates to its callers.
optimize.c: new file.
optimize.h: new file.
common.mk (COMMONOBJS): dependencies for new files.
iseq.h (ISEQ_NEEDS_ANALYZE): new flag to denote the iseq in question might need (re)analyzing.
I had this link in my bookmark for months, but I forgot it. This email is not to forget it again ;-) Someone may find it useful!