2008-09-19

Could Python use some of these new JavaScript VM tricks?

JavaScript is the new hotness. The big new trend in language works seems to be trying to come up with a new, fast JavaScript VM. See TraceMonkey, SquirrelFish Extreme (formerly just SquirrelFish), and V8. Like Python, JavaScript is highly dynamic. Could Python somehow learn from all of this work going into JavaScript and improve CPython's performance? Let's look at what each JavaScript VM does and see if it could potentially be applied to Python.

TraceMonkey's big trick is trace trees. When you do a loop, you get back to the top through jumps. Do this enough times and that jump is considered hot and of some use. When that decision is made, a trace is made from the top of the loop to the jump. That entire trace is then inlined.

Python could potentially do this if we ever figure out a good way to do inlining (Python's rich calling semantics make me wonder how easy it would be to do inlining, but I still think it is doable). I think the first step in trying to pull something like this off would be to get simple inlining working for functions and methods and build on that.

V8 does something called hidden classes. Basically, when a class is created a hidden version is made that makes attribute access be a memory offset. As more attributes are added, more hidden classes are created so each new hidden class allows for one more attribute to be accessed. The potential problem for doing this for Python is that namespace lookups are structured around using dicts. Being able to overload __dict__ is part of the design of the language. So if anything was done like this then a special namespace dict would be needed that could translate dict lookups to the proper memory offset lookup.

In the first announcement for SquirrelFish, the speedup came from moving off of AST execution and over to a bytecode interpreter. About the biggest difference between SquirrelFish and Python is the use of registers instead of a stack. I have always been curious as to whether using a register VM might be of some benefit to Python. SquirrelFish Extreme basically adds what V8 does with hidden classes.

There are two things that seem to be common amongst the VMs. One is using research done for the Self programming language. While the language itself is prototype-based, some of the work could still be applied to Python. And second, all of the interpreters use JIT compilation to native code when possible.

So what are the chances any of this will happen? Honestly, I don't know. The big advantage JavaScript development has over Python development is that JavaScript has major corporations sponsoring the work. Python, on the other hand, only has Guido is the only person who is paid to spend 50% of his time, when he has the time because it comes in spurts, to work on Python. Unless a huge number of us on the development team decide to take on some major VM work, we get a PhD student to jump in on this (and no, I am not that student unfortunately), or someone gets paid full-time to work on CPython and decides to tackle the performance question, I don't see any radical change to CPython's VM occurring. Here is to hoping I someday get paid to work on it. =)