Hi Mart
I'm back with some news about wpython. I completed all the work that I was
committed to do till the end of the year. I made a lot of changes to the
code, that I'll report here.
First, I added several conditional compilation sections that enable or
disable almost every optimization I introduced into the project. Everything
is controlled by a new include file, wpython.h, which holds a lot of
#DEFINEs for each one of them.
Every #DEFINE has a brief explanation, and some report an example with
Python code disassembled, showing what happens.
It can be useful both to document the code (also to access to the interested
parts), and to let people test the effect of all optimizations. There are
also a couple of #DEFINEs which are useful to enable or disable all
superinstructions, or to make wpython work like CPython (with all new
optimizations and superinstructions disabled).
Full tracing support required a big effort, due to the missing
SETUP_LOOP/POP_BLOCK instructions used in FOR_ITER blocks. It was a pain in
the neck to let them work, but I think I have found a good solution fot it.
If I remember correctly, Collin asked in the past about performance with
testing enabled. I believe that speed is comparable to CPython, since I can
trace FOR_ITER blocks enter/exit with very little time spent intercepting
them; stack unrolling (for forward jumps cases) is fast too.
Restoring Python object model required much of the work. I reverted all the
changes that I made to many PyObjects, and just added some accessory code
only to a few of them. There are no more hacks, and code is quite polite;
only CodeObject required one line of code change in the hash function, to
let it calculate hash correctly for the constants tuple (because it can hold
lists and dictionaries now, which usally aren't hashable).
Every file in Include/ and Objects/ that I modified has only 1 diff (except
frameobject.c, for tracing code), so it's easy so see what is changed and
the extra helper functions that I added to introduce lists and dictionaries
in the consts tuple.
In the meanwhile I've added a little optimization for lists and dictionaries
used in for loops. Writing this:
def f():
for x in ['a', 'b', 'c']: print x
generates the following (word)code with the previous wpython:
LOAD_CONST (['a', 'b', 'c'])
DEEP_LIST_COPY
GET_ITER
FOR_ITER
because ['a', 'b', 'c'] is a mutable object, and a copy must be made before
using it.
Now it'll be:
LOAD_CONST (['a', 'b', 'c'])
GET_ITER
FOR_ITER
So code is reduced and memory consumption too, because there's no need clone
the list. The trick works only for lists and dictionaries that holds
non-mutable objects, but I found it's a common pattern in Python code.
I've also updated the source to the latest Python 2.x version, 2.6.4.
All tests pass, both with Debug and Release code, on Visual Studio Express
with 32 bit code (I can't compile 64 bits versions with it).
There are only a few open issues.
test_syntax.py required some changes in the doctest (adding full filesystem
path) to let them pass correctly. It's really strange, but... works now!
test_compile.py has 2 tests disabled in test_compile_ast:
#['<forblock>', """for n in [1, 2, 3]:\n print n\n"""],
#[fname, fcontents],
that's because there's no support for constants (except Num_kind and
Str_kind) in the current ASTs code. However code compiles well, except that
it cannot make use of the new constant folding code.
I haven't updated Doc/library/dis.rst, which is exactly the same of CPython.
I'll do it when I stop introducing or changing opcodes.
Right now wpython requires manual patching of Include/Python-ast.h, with the
following lines:
enum _expr_kind {BoolOp_kind=1, BinOp_kind=2, UnaryOp_kind=3, Lambda_kind=4,
[...]
List_kind=18, Tuple_kind=19, Const_kind=20};
enum _expr_const {no_const=0, mutable_const=1, content_const=3,
pure_const=7};
struct _expr {
enum _expr_kind kind;
union {
[...]
struct {
object c;
enum _expr_const constant;
} Const;
} v;
int lineno;
int col_offset;
};
They are need to let ast.c handle constants for the new constant folding
code.
I greatly appreciate any help to let it be generated automatically with ASDL
grammar.
That's all about the new code. Now the weird and stupid part. A few days I
got a new gmail account, but accidentally I removed the google account that
I've used to create the wpython at Google Code. I definitely lost project
ownership, so I can't tag the old code and put the new one in trunk.
I'll thank very much if someone that works or has contacts with Google can
ask for moving ownership from my old account (cesare at pronto do it) to my
new (the one which I've using now to write this mail), so I'll commit ASAP.
Alternatively, I need to create a new project at Google Code.
I hope that the community will appreciate the work (when I'll upload it :-).
I know that it's a young project, but I think it's mature enough to take a
look at it.
Last but not least, think about it like a starting point. I have many ideas
on how to optimize several other parts of Python, and the wordcode structure
gives me rooms to do it in an elegant and efficient way thanks to the
superinstructions (when needed).
For the next release I plan to cleanup opcode.h and ceval.c, grouping some
instructions into single superinstructions (CALL_FUNCTIONs and IMPORT_NAME),
adding a few opcodes, and tweaking a bit the VM main loop (primarily
targeted to reduce the jump-table that compilers produce for the big switch
statement).
Then I'll consider porting it to python 2.7 and/or python 3.1/3.2 if
there'll be interest and feedbacks about it.
I'm also at your disposal to discuss any detail about wpython source code,
since I know that it isn't a simple patch to apply to some files.
Cheers,
Cesare
2009/11/4 Mart Sõmermaa
Thanks for the recap and for the good work on wpython!
Best, eagerly waiting for the results of your work to land in mainline python, MS
Cesare Di Mauro schrieb:
Hi Mart
I'm back with some news about wpython. I completed all the work that I was committed to do till the end of the year. I made a lot of changes to the code, that I'll report here..
For those of us without an elephant's memory, could you please also recap what exactly wpython is, and maybe what the goals and intentions are? cheers, Georg
On Thu, Nov 26, 2009 at 3:41 PM, Georg Brandl
Cesare Di Mauro schrieb:
Hi Mart
I'm back with some news about wpython. I completed all the work that I was committed to do till the end of the year. I made a lot of changes to the code, that I'll report here..
For those of us without an elephant's memory, could you please also recap what exactly wpython is, and maybe what the goals and intentions are?
It's a Python implementation that uses wordcode instead of bytecode. http://code.google.com/p/wpython/ I don't see any benchmarks though. -- --Guido van Rossum (python.org/~guido)
2009/11/27 Guido van Rossum
It's a Python implementation that uses wordcode instead of bytecode.
http://code.google.com/p/wpython/
I don't see any benchmarks though.
You'll find some at page 28 herehttp://wpython.googlecode.com/files/Beyond%20Bytecode%20-%20A%20Wordcode-bas... . Mart made more interesting oneshttp://www.mail-archive.com/python-dev@python.org/msg43282.htmlwith Unladen benchmarks. Cesare
Cesare Di Mauro wrote:
You'll find some at page 28 herehttp://wpython.googlecode.com/files/Beyond%20Bytecode%20-%20A%20Wordcode-bas... ..
Mart made more interesting oneshttp://www.mail-archive.com/python-dev@python.org/msg43282.htmlwith Unladen benchmarks.
The PDF document sounded interesting and I was tempted to test WPython. Unfortunately it doesn't compile on my box: $ make gcc -pthread -c -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Python/ast.o Python/ast.c Python/ast.c:30: warning: ‘enum _expr_const’ declared inside parameter list Python/ast.c:30: warning: its scope is only this definition or declaration, which is probably not what you want Python/ast.c:335: warning: ‘enum _expr_const’ declared inside parameter list Python/ast.c:335: error: parameter 2 (‘constant’) has incomplete type Python/ast.c: In function ‘Const’: Python/ast.c:341: error: ‘Const_kind’ undeclared (first use in this function) Python/ast.c:341: error: (Each undeclared identifier is reported only once Python/ast.c:341: error: for each function it appears in.) Python/ast.c:342: error: ‘union <anonymous>’ has no member named ‘Const’ Python/ast.c:343: error: ‘union <anonymous>’ has no member named ‘Const’ Python/ast.c: In function ‘set_context’: Python/ast.c:457: error: ‘Const_kind’ undeclared (first use in this function) Python/ast.c: At top level: Python/ast.c:591: warning: ‘enum _expr_const’ declared inside parameter list Python/ast.c:590: error: conflicting types for ‘seq_for_testlist’ Python/ast.c:29: note: previous declaration of ‘seq_for_testlist’ was here [...] $ gcc --version gcc (Ubuntu 4.4.1-4ubuntu8) 4.4.1 $ uname -a Linux hamiller 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64 GNU/Linux
2009/11/27 Christian Heimes
Cesare Di Mauro wrote:
You'll find some at page 28 here<
http://wpython.googlecode.com/files/Beyond%20Bytecode%20-%20A%20Wordcode-bas...
..
Mart made more interesting ones<http://www.mail-archive.com/python-dev@python.org/msg43282.html with Unladen benchmarks.
The PDF document sounded interesting and I was tempted to test WPython. Unfortunately it doesn't compile on my box:
[...]
That's because Include/Python-ast.h file is autogenerated from the ASDL grammar file the first time that you try to compile wpython. You need to replace it with the one bundled with wpython. It's a known problem that I'll address ASAP. Cesare
2009/11/27 Christian Heimes
Cesare Di Mauro wrote:
You'll find some at page 28 here<
http://wpython.googlecode.com/files/Beyond%20Bytecode%20-%20A%20Wordcode-bas...
..
Mart made more interesting ones<http://www.mail-archive.com/python-dev@python.org/msg43282.html with Unladen benchmarks.
The PDF document sounded interesting and I was tempted to test WPython. Unfortunately it doesn't compile on my box:
$ make gcc -pthread -c -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Python/ast.o Python/ast.c
Python/ast.c:30: warning: ‘enum _expr_const’ declared inside parameter list Python/ast.c:30: warning: its scope is only this definition or declaration, which is probably not what you want
Python/ast.c:335: warning: ‘enum _expr_const’ declared inside parameter list Python/ast.c:335: error: parameter 2 (‘constant’) has incomplete type
Python/ast.c: In function ‘Const’:
Python/ast.c:341: error: ‘Const_kind’ undeclared (first use in this function)
Python/ast.c:341: error: (Each undeclared identifier is reported only once Python/ast.c:341: error: for each function it appears in.)
Python/ast.c:342: error: ‘union <anonymous>’ has no member named ‘Const’
Python/ast.c:343: error: ‘union <anonymous>’ has no member named ‘Const’
Python/ast.c: In function ‘set_context’:
Python/ast.c:457: error: ‘Const_kind’ undeclared (first use in this function)
Python/ast.c: At top level:
Python/ast.c:591: warning: ‘enum _expr_const’ declared inside parameter list Python/ast.c:590: error: conflicting types for ‘seq_for_testlist’
Python/ast.c:29: note: previous declaration of ‘seq_for_testlist’ was here [...]
$ gcc --version gcc (Ubuntu 4.4.1-4ubuntu8) 4.4.1 $ uname -a Linux hamiller 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64 GNU/Linux
I have created a new project at Google Code: http://code.google.com/p/wpython2/ using Mercurial for the repository. The master (Python 2.6.4) code is located into the default repository: https://wpython2.googlecode.com/hg/ The wpython (version 1.0) clone is in: https://wpython10.wpython2.googlecode.com/hg/ Sources are available in: http://code.google.com/p/wpython2/downloads/list wpython 1.0 is an almost complete replacement for Python 2.6.4 (except for Doc/library.dis.rst, which I'll update later, when I stop adding or changing opcodes). I have changed the ASDL grammar (in Parser/Python.asdl) so that there's no need to overwrite Include/Python-ast.h, and I've added full support for constants to the AST code (I left Num_kind and Str_kind untouched right now, but I plan to remove them in the next release, since Const_kind is able to hold any kind of constant object). Now you shouldn't have problems compiling it. Cesare
participants (4)
-
Cesare Di Mauro
-
Christian Heimes
-
Georg Brandl
-
Guido van Rossum