[ann] Minimal Python project

Edward K. Ream edream at tds.net
Tue Jan 14 06:34:06 EST 2003


> What makes you think C is particularly suited to producing fast code?
Are you running a PDP-11?

These questions may be flippant, and the answers are relevant to the
question of how fast Python code optimized by psyco may become.  Before
going further I should say that I have designed, built and tested a
commercial optimizing C compiler.  It produced very good code and did so
faster than the Borland C compiler.

1. The main reason for thinking C is particularly suited to producing fast
code is that in C the types of all objects are known at compile time.  This
means that the compiler may generate code tailored to the particular type of
operands.  Indeed, it is instructive to compare what any descent C compiler
can do easily with what psyco can do.  Let us look at the code discussed on
the psyco web site at: http://psyco.sourceforge.net/psyco/node3.html.

The essence of what psyco is doing in this example is (cleverly!)
discovering the types of Python objects and generating code to handle _those
particular type of objects_.  A C compiler has no need of cleverness: the C
programmer declares the types of those objects.  Thus a C compiler starts
out with the information that psyco must discover at run time.

Moreover, an examination of the code emitted by psyco shows that many common
optimizations available to a C compiler are not available to psycho.  These
optimizations are not available because there isn't enough time: the time
psycho would spend discovering those optimizations (and rewriting the newly
optimized code!) would slow down the running time of the program too much.
C compilers can take a leisurely time discovering and implementing
optimizations because the running time of the compilation does not affect
the running time of the resulting program.  This is emphatically not true
with psyco or any JIT.

2. Your second question implies that the quality of code emitted by a C
compiler (or any other compiler) somehow depends strongly on the match
between the language and the architecture of the machine on which the
compiled code will run.  This is simply not true; the range of optimizations
available even to medium-quality C compilers are good enough to produce good
to excellent code any CISC or RISC machine.  Moreover, there is an interplay
between machine design and the C language itself.  Hardware designers are
well aware that lot's of C code will run on their machines.  In particular,
almost all Python code eventually gets executed as C code because the
interpreter is C code and almost all of the libraries are either coded in
directly in C or in Python which then gets interpreted by the interpreter.

Conclusions

psyco _can_ improve the speed of Python's C interpreter applied to Python
code.  Psyco does this by emitting assembly language code "specialized" to
handle the actual type of the Python objects presented to it.  You may think
of this speedup as arising from better algorithms which take advantage of
more knowledge.  You may also think of this speedup as arising from giving
the interpreter a "memory".

However, examination of the assembly code emitted by psyco shows that it
will be slower than the equivalent C code (that is, hand-written C code
corresponding the Python code).  Moreover, psyco's compilation process
happens at runtime (in a clever multi-step process) and all this compilation
is pure overhead compared with running the code through a C compiler.

Making  psyco available generally to Python programmers promises a _huge_
win for all Python programmers.  It certainly is possible to improve the
performance of Python's present C interpreter using psycho's techniques.
However, it will almost certainly never be possible to speed up _other_ C
code by translating that code into Python.  I would expect that running
psyco on Python code will at best run half as fast as the equivalent
hand-written C code.  The reasons are clear:  psyco has neither the time nor
the information to do as well as the best hand-written C code compiled by a
descent compiler.  But it is precisely this kind of C code that we expect to
C in the Python libraries!  Translating library C code to Python has no
chance of improving the Python's speed unless we also significantly improves
the algorithms used.

Edward

P.S.  Just for the record let me try to be as clear as possible.  Even the
most useful, interesting and clever engineering techniques have areas in
which they apply and areas in which they do not apply.  I have simply been
pointing out that psyco can not improve _all_ C code.  This is actually a
weak and not-very-interesting statement.  (And it was not, I think, the
meaning intended in the original announcement.)  Again: psyco promises to be
of great benefit to all Python programmers.

EKR
--------------------------------------------------------------------
Edward K. Ream   email:  edream at tds.net
Leo: Literate Editor with Outlines
Leo: http://personalpages.tds.net/~edream/front.html
--------------------------------------------------------------------






More information about the Python-list mailing list