Now I stay San Francisco for business trip. The city become Christmas season and very happy atmosphere. Special ice-skating rink is appeared in the park and the office building that ngmoco:) is in has many illuminations.
I joined PyPy-ja advent calendar . Yesterday @aodag wrote the entry. Today is my turn.
Do you know PyPy? It is a very impressive Python implementation. Its speed become faster and faster. 1.6 was already faster than CPython but 1.7 improved too.
But there is the fastest Python implementation that is faster than PyPy. Following result of the benchmark program that calculates Fibonacci number. Each first lines are the result of the calculation (F31), and second lines are the time to calculate. I uses CPython 2.7.2 and PyPy 1.7 and "it".

PyPy runs 8.6 times faster than CPython. But "that" implementation runs 5 times faster than PyPy. It is more than 40 times faster than original CPython. Do you believe the result?
That secret of that Python is included in PyPy. Its name is RPython, shorten form of "Restricted Python". Zope has same name Python environment, but it is different one. The code of benchmark is following. This entry uses smipple. Thank you for Ian Lewis! He is super cool Python programmer.
But the interpreter doesn't exist actually.
But you can't use RPython interpreter. RPython is the specification for the subset of Python. All RPython program can run on Python and PyPy. One more thing, PyPy provides the tool translate.py. This is a secret of this blog entry. I will introduce translate.py.
What is RPython?
RPython is created for PyPy. As you heard, PyPy is written in Python. But its super exciting benchmark result isn't achieved on Guido's CPython. PyPy interpreter source code is converted by translate.py into C or LLVM or CLR(.net), Java source code. RPython can gain unbelievable speed instead of any flexibility of lightweight language.
I am interested in the feature that it can translate into native or Java or .net execution like py2exe. So I tried it for PyPy-ja advent calendar.
How to program in RPython?
Following code sample is a sample that familiar with all programers - Hello World.
- The target function is the special function it makes decision the entry point.
- __name__ block is not executed. It is useful to keep compatibility with other Python interpreters.
- The entry point function must return integer.
After typing these command and going to Starbucks and drinking coffee, you get super fast native binary program.
I don't research the detail, but it supports many options to improve the speed, like Just-In-Time compiling and Garbage-Collection. Above result of benchmarking was generated by simple option(just -O2). Above sample uses CPython, but if you use pypy, you can save your time.
Programming in RPython is an eXtreme Programming.
This "eXtreme" is a same meaning of eXtreme Ironing. Almost all Python code can't be accepted as is. I felt as if I programming in C++. There many pit falls. I will show the some of them.
Function in other functions
You can't nested function definition:
Multiple inheritance
RPython can't use multiple inheritance. If you want to share the methods, you must set _mixin_ flag into secondary classes. And you can't use super() function. But it is not important any more (because it doesn't use C3 algorithm to create sequence of parent classes).
Official supported modules are limited.
In official document, only os, math, time modules are supported.
@property decorator
It can't support nicer syntax - property.
Meta-Programming
We can't access to __dict__ property. It means dir() function is not supported. If you want to write testing framework, you must add each test methods by hand like older style CppUnit.
Function's parameters are binded specific data type
Variables in Python don't have type information. So you can write generic algorithm like C++'s template. But in RPython, variables have type information and they are binded when first assignment. It includes parameters of function.
My impression of RPython
I felt it is difficult.
At first, if it supports regular expression, I will be able to use it for small tool developing. So I tried to port rxpy (pure Python regular expression it has compatibility with re module). But it is super extreme hard task for me (and I couldn't finish). Above pit falls were found when porting rxpy. And the worst point of RPython is it takes much resources. Compiling above simple took about 1.5 minuits. And I heard building PyPy needs more than 4GB memory.
There is only a little instruction. And no one guarantees the specification of RPython. So we have to try and error to use RPython. But if I can understand annotation system better than now and more useful modules they support RPython programming are released, it will be more useful environment.
But I was moved to the performance. So I want to try RPython little by little to expand the capability of RPython. At first I will read the source code of existing programming languages they are implemented in RPython.
Next person who write PyPy-ja advent calendar is @yanolab.