PyGTK is getting better, stay tuned!

As you might know, INdT do take care of PyMaemo, the Python port for the Maemo platform, and as I work for INdT, I'm trying to help to improve Python on this platform, so maybe one day we get it included on Nokia 770 product.

Python VM/interpreter is quite light and fast, the showstopper is load time, mainly the import time and to be more specific, import time of C-written modules with many symbols, like PyGTK, which is really useful in Maemo.

First a basic intro on how to write Python modules in C: you need to provide a table with exported functions, these consist of a string, which will be the visible name in Python, the function pointer, parameter types and docstring. You can check more details at Extending and Embedding the Python Interpreter document.

So, how this can slow things down? The main problem is that these function pointers are address that will only be resolved by the dynamic linker when the library is loaded into RAM, and this take a lot of time.

This is not the only problem, there are others that affect us in the current version, for instance, PyGTK did initalize every GTK, GDK, ATK, Pango classes, even the unused, they did use to call classes _get_type() methods, like gtk_label_get_type(). For those not aware of GTK/Glib/GObject internals, GObjects are dynamic types, type checks are done in runtime by the GType system. When you first use a class you will call its _get_type() in some way, which will fill a static structure and register it with the GType system, which will maintain a complex structure and hash table underneath. When you use macros like GTK_LABEL() or GTK_TYPE_LABEL, you're calling functions and using the GType stuff. In regular C programs, you just do this for few classes, but PyGTK were doing this every time.

After some investigation and talking to Johan Dahlin, he did look at it and some great stuff started to show! For instance, the GType madness is gone! Now these functions are lazily called, so you just pay for what you use. Also planned is to be able to load every function in a lazy fashion, so import gtk would do almost nothing and thus be really fast. Johan is doing an excellent job and much this is already done, other are under development (like lazy-load Enum/Flags stuff) and some will be done in near future (like every function/method to be lazily imported).

Last, but not least, Rafael Ávila de Espíndola, my co-worker, is taking care of symbols visibility, adding compiler hints where appropriate to instruct it to hide some symbols, which also helps a lot

So much is being done in GTK and it will for sure improve our situation on Maemo and Desktop. Thank you Johan and Rafael!