News for hackers. Stuff that matters.
Re:So you want to write a fast Python? by wbhart on Sunday July 10, 2011 @09:27PM (#2749120)
I have some questions:

* Is the Jit in PyPy standalone, i.e. can it be used by other projects independently of Python? Is it documented?

* How does PyPy compare with highly optimised C?

* Assuming that the benchmarks that used to be at the Computer Benchmarks Game for PyPy are somewhat representative of overall performance, why is there still such an enormous difference with optimised C?

* There used to be this thing called restricted python. If one limits oneself to using that only, are benchmarks much better?

* I read somewhere that some projects have merged or combined forces. There used to be this thing called Unladen Swallow. There was also Psyco. Have either of these merged with/been absorbed by PyPy? Are any other such projects still going?

* Cython is another fast project. If it became part of mainline Python, would PyPy become irrelevant?

Re:So you want to write a fast Python? by kingkilr on Sunday July 10, 2011 @09:41PM (#2749144)
I have some answers!

* Yes, the JIT is part of the RPython translation toolchain (more on this in a few) and can be used in other interpreters, for example we have Scheme, Javascript, Prolog, and Haskell in varying level of completeness (Prolog being the most complete).

* Highly optimized C? Not spectacularly, normal C, not bad, on numerical code we often hover around gcc -O1.

* They weren't representative, most of that code was heavily optimized for CPython, which has very different performance characteristics.

* RPython is the language the PyPy interpreter is written in, it's not really meant for general purpose code, it's designed mostly for VMs, but it does run at basically C-like speed.

* Unladen Swallow was Google's fork of CPython, it's basically dead at this point, retrospective here (http://qinsb.blogspot.com/2011/03/unladen-swallow-retrospect...), Psyco was an extension module for CPython that added a JIT, it's no longer maintained (but still works), it's creator is the lead developer of PyPy.

* Cython isn't Python (this could be a meme or something), it does not implement the entire language and thus isn't directly comparable (although we still often compare, because it is a competitor in the space of "making Python-like stuff faster").

Re:So you want to write a fast Python? by srean on Sunday July 10, 2011 @10:17PM (#2749209)
Hi Alex, mine might be a minority view/request, but I think RPython is an interesting and useful language in its own right and should be standardized and highlighted. Possibly separated out with a website of its own to emphasize its existence.

When I write Python it is rarely the case that I assign values of different types to the same identifier, or add attributes to a class outside of its definition, or monkey patch a module. In other words most of the time I am not really making use of the dynamic properties of the language. In this scenario Python is a language that saves me "finger typing", and I do not needs its dynamic typing as much. I am more than willing, in these scenarios, to trade some of that unused dynamism for better performance. I think there might be others who are similarly inclined.

The one thing that I miss in RPython is generators. Is there a particular difficulty because of which it was left out ? I assume so, because all the statically typed variants of Python leave it out as well, but I do not have enough background to know what that difficulty might be. The other is Numpy :) but I believe you guys are working on that.

@wbhart A lot of what you ask is answered in some detail in the PyPy blog http://morepypy.blogspot.com/ I wouldnt call PyPy less well known though.

Re:So you want to write a fast Python? by kingkilr on Sunday July 10, 2011 @10:27PM (#2749226)
http://stackoverflow.com/questions/6277174/are-generators-su... explains why we don't have generators in RPython
Re:So you want to write a fast Python? by dochtman on Monday July 11, 2011 @03:07AM (#2749776)
You should put that on the pypy site somewhere.
Re:So you want to write a fast Python? by StavrosK on Monday July 11, 2011 @11:03AM (#2750960)
I'd like to second this request, I would be more than happy to write code in a slightly more restricted way if it meant that I could generate C-fast modules from Python code. In my opinion, RPython is an even bigger deal than PyPy, as it's a very easy way to get C speed with Python syntax.

I have no idea why they haven't given the compiler some love so we can use it for our own projects. It sounds like it would become ubiquitous very, very fast.

Re:So you want to write a fast Python? by wbhart on Sunday July 10, 2011 @10:24PM (#2749222)
Generators probably need to be efficiently inlined to be fast? Maybe this depends on the capabilities of the VM. I think they can also be implemented with continuations which few languages implement due to being a headscrew. I'm just guessing though. I'd be interested in hearing an authoritative answer.

I fully agree re the highlighting and standardizing RPython.

Re:So you want to write a fast Python? by stephth on Monday July 11, 2011 @04:56AM (#2749974)
@srean, what do you use RPython for?
Re:So you want to write a fast Python? by wbhart on Sunday July 10, 2011 @10:06PM (#2749192)
Thanks for the concise and extremely helpful answers. I have some followups:

* What is the Jit itself written in?

* You mention that for numeric code PyPy hovers around gcc -O1. What are the other classes of code that you see PyPy as having to deal with? And how would it perform compared to normal C on those?

* Very interesting comments re the benchmark game. Did anyone ever optimise some of these tasks specifically for PyPy?

* Just a clarification re Cython. Yes, I believe it was forked from Pyrex which was mainly for nailing up C code I think. Though since that time my impression is it has become its own language, similar in parts to C/C++ and similar in parts to Python. They appear to have put a fair bit of work into making it fast. It is compiled though (to C), not interpreted. There was some talk about them trying to get it into CPython. But I'm not sure what was meant by that.

One final followup:

* Do you know if there are any plans to develop a new language built on top of the PyPy VM similar to Python but extended in some way? For example would it be feasible to implement a statically typed language with or without type inference on top of the same technology?

By the way, if you google virtual machine you get pages and pages of links to LLVM, JVM, Parrot and Dalvik (go figure) and the occasional reference to a few others. I'm really surprised that the PyPy VM is not better known!

Re:So you want to write a fast Python? by kingkilr on Sunday July 10, 2011 @10:26PM (#2749225)
* The JIT is written in RPython.

* Comparing code with C is pretty difficult for all but the easiest 1-1 ports (i.e. numeric stuff), so I don't have good comparison numbers for it.

* Yes, I tried to improve the submissions to the benchmark game, this led down a long path which resulted in the benchmark game only having a single implementation for each language (e.g. tracemonkey is no longer on there, only v8)

* We don't plan on writing a new language, we kind of like Python, but someone absolutely could.

* PyPy doesn't have a VM, we have a toolkit for writing VMs and adding JITs to them. Perhaps these two blog posts will make it more clear: http://morepypy.blogspot.com/2011/04/tutorial-writing-interp... http://morepypy.blogspot.com/2011/04/tutorial-part-2-adding-...

Re:So you want to write a fast Python? by wbhart on Sunday July 10, 2011 @10:51PM (#2749266)
I think I understand now. The PyPy implementation of Python is written using the RPython -> Jit interpreter framework that the PyPy project has. Clever.

Am I right in thinking that the drawback of this method is very long compile times for producing the interpreter?

Re:So you want to write a fast Python? by kingkilr on Sunday July 10, 2011 @10:59PM (#2749285)
Yes, compile times are rather long, about 40 minutes for the full interpreter on a recent, fairly good, laptop. On the other hand you don't need to do a compile all that frequently, because all the code is testable by executing directly atop another Python.
Re:So you want to write a fast Python? by StavrosK on Monday July 11, 2011 @11:24AM (#2751068)
So you develop the interpreter by interpreting it on itself before compiling it to itself by itself.

Uh...

Re:So you want to write a fast Python? by cscheid on Tuesday July 12, 2011 @07:28PM (#2757086)
You might have some fun reading about the Futamura projections :)
Re:So you want to write a fast Python? by StavrosK on Tuesday July 12, 2011 @07:59PM (#2757168)
Oh God, that stirs some memories. I'll read it again, thank you.
Re:So you want to write a fast Python? by StavrosK on Monday July 11, 2011 @11:22AM (#2751061)
Cython just has a syntax for annotating your code with types so it can work its magic and make it go fast, although you can add the annotations to a separate file and keep your code in pure Python.

I'm not sure if they're going to use Python 3's annotations to make this cleaner, that might be worth exploring...

Re:So you want to write a fast Python? by koenigdavidmj on Sunday July 10, 2011 @09:47PM (#2749157)
* Yes...I have already seen it for some "build your own language interpreter" examples. Not sure about the documentation quality.

* http://geetduggal.wordpress.com/2010/11/25/speed-up-your-pyt... lists an example where PyPy is in fact faster than C++ with STL. Not sure how it would compare to well-optimized C, but remember that JIT warmup time will hurt you for short lived jobs.

* Can't answer this one, sorry.

* RPython is still around---PyPy is written in it! Certainly it would be more friendly toward the JIT, which does best when the types of variables are consistent (which is required by RPython). Plus, RPython can be translated directly to native code and you can get rid of the JIT entirely.

* Unladen Swallow tried to do a lot of the same stuff that PyPy is doing, but on top of LLVM. It never did much better than CPython (within an order of magnitude), but if it did they were planning to merge it into CPython (of which it was a branch). PyPy is a followup to Psyco (which only ran on 32 bit x86) by the same people, but Psyco ran under CPython.

* Cython does not interpret Python---it compiles from a language that looks a lot like Python but with C-like semantics (for example, optional strong typing, and the ability to directly call C functions), and turns it into C or C++ code that can be compiled into a Python module. As such, it targets a slightly different use case.

Re:So you want to write a fast Python? by wbhart on Sunday July 10, 2011 @10:20PM (#2749213)
" http://geetduggal.wordpress.com/2010/11/25/speed-up-your-pyt.... lists an example where PyPy is in fact faster than C++ with STL. Not sure how it would compare to well-optimized C, but remember that JIT warmup time will hurt you for short lived jobs."

But note a little further down the page. In the hands of a C++ coder (or indeed Cython coder) things look very different.

Re:So you want to write a fast Python? by luismgz on Monday July 11, 2011 @08:48AM (#2750438)
1) Is the JIT standalone? No easy answer. Pypy is a complex project, not just a python implementation. Pypy is a complex toolchain, a framework, for implementing dynamic languages. With pypy, you could write your own implementation of ruby, scheme, php, whatever... And once your interpreter is complete, pypy will generate the JIT automatically for you (you won't need to implement it yourself, you just have to write the interpreter, and pypy will do the rest). Pypy is writen in Rpython (restricted python) but you can implement any vm with it, for any language you may want.

2) How does it compare to c? It's pretty competitive in highly algorithmic code, cpu intensive and numerical tasks. For anything else, it depends. Overall, it on average up to 4 times faster than regular python.

3) Why there's still a difference? Pypy is a work in progress. You should compare it to other projects such as v8 or tracemonkey.

4) Restricted python: It's the static subset of python used to implement pypy. It takes the place of c in cpython. Yes, it's much faster, but also more limited and less flexible. It's nicer than c, but not as cool as full python. You can also find Shedskin and Cython. Shedskin is a true static subset of python that compiles to c++. Cython is a python-like language that adds type declarations to the language to make it closer to c.

Unladen Swallow was a separate project and it's dead now. Psyco is the predecessor of pypy. It is no longer maintained.

Re:So you want to write a fast Python? by llambda on Sunday July 10, 2011 @09:31PM (#2749129)
Maybe I'm confused, but isn't Cython just a tool for writing C types and C functions directly in Python? Whereas PyPy, Jython, etc, are full runtime environments.