Issues in compiling Python to machine code

Ryan Paul segphault at sbcglobal.net
Sun May 23 10:14:42 EDT 2004


On Sun, 23 May 2004 21:23:19 +1000, Johnathan Doe wrote:

> I've been thinking about what the issues would be in compiling
> Python into native machine code, and since type information is
> important in Python, it seems possible that Python code can
> be compiled into native machine code (albeit with a lot of
> extra effort).
> 
> For instance, type information is discovered when something is
> assigned to a variable or an anonymous piece of data is used
> in a program.  Compiling Python bytecode into native machine
> code could involve a pass over the bytecode to look at all the
> uses of a variable or anonymous variable, call some Python
> function to discover its variable, then record it in the symbol
> table.
> 
> Another issue is what to do with objects.  Finding the offset
> to a pointer which is the object's constructor, for instance,
> if that's the way it's done internally in Python.
> 
> And then freeing the memory used by an object when it's no
> longer needed.  Perhaps freeing memory for an object can be
> done by inserting machine code to return memory back to the
> heap after the first pass over the bytecode and recording the
> place a variable is last used (hopefully then it won't be
> used in some other way later of course... perhaps it can
> record all cases where that variable is referred to by another
> name and track them, too.)
> 
> Seems like compiled Python would need a lot of support code,
> in any case, that gets run at certain times:  before the program
> starts, whenever a variable is accessed, before an object is
> used (to allocate memory), whenever something happens to data
> within an object (allocate/reallocate/destroy), and so on...
> 
> Are there any reasons why Python would be *impossible* to compile?
> 
> Thanks :)

we have talked about this here before, I think. Native compilation wont
improve runtime speed of a dynamically typed language. Despite this, there
are actually a few projects out there endevouring to do it anyway. If you
want better speed from a python application, try a customized python
runtime engine like Psyco.

--SegPhault




More information about the Python-list mailing list