[sugar] Activities are recompiled on every launch

Benjamin M. Schwartz bmschwar
Sun Mar 23 19:02:52 EDT 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

In order to run a python program (.py), the python interpreter must first
compile it to bytecode.  The bytecode is stored in a .pyc file, when
possible.  In the case of installed packages, like NumPy or Sugar, the
.pyc files are generated during installation (i.e. while the build is
being prepared at 1CC).  However, in the case of Activities, the .pyc
files are never generated.

~From [1]:
"When a script is run by giving its name on the command line, the bytecode
for the script is never written to a .pyc or .pyo file. Thus, the startup
time of a script may be reduced by moving most of its code to a module and
having a small bootstrap script that imports that module. It is also
possible to name a .pyc or .pyo file directly on the command line."

This trick unfortunately cannot work with Rainbow, because the Activity
runs as a user who does not have write access to the directory containing
the .py files.  The Python interpreter does not use any setuid tricks to
write .pyc files into system paths, nor does it cache bytecode in a
~/.python/ directory, etc.

"The module compileall  can create .pyc files (or .pyo files when -O is
used) for all modules in a directory."

This seems more appropriate. Perhaps all the .py files could be compiled
upon bundle installation.

Overhead measurement:
As a measure of overhead, I tried the following on my B4 running joyride-1790:

# time python -c "import py_compile"
real    0m0.108s
user    0m0.080s
sys    0m0.030s

# time python -c "import py_compile; py_compile.compile('pippy_app.py')"
real    0m0.259s
user     0m0.250s
sys    0m0.030s

# time python -c "import compileall"
real 0m0.110s
user 0m0.110s
sys 0m0.000s

# time python -c "import compileall;
compileall.compile_dir('/usr/share/activities/TamTamEdit.activity/',
force=True, quiet=True)"
real  0m3.902s
user  0m3.460s
sys 0m0.440s

All measurements were made multiple times to ensure cached reads.  The
measurements may overestimate the amount of time actually required, since
these times also include writing the .pyc files into the compressed
storage.  Nonetheless, I think that most activities spend about 0.2
seconds compiling on every launch.  The Journal takes just over 0.5
seconds, Record takes about 1.3s, and the TamTam activities (with a huge
amount of code) take 3.x seconds.

I suspect that we would be willing to trade the slightly increased size on
disk for the slight improvement in launch speed.

[1]: http://docs.python.org/tut/node8.html#SECTION008130000000000000000
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.7 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFH5uGcUJT6e6HFtqQRAiFLAJ97RJ74D3ivVT0FLjDwvEwxTvs3ZACfeV3D
SOxU8XL3bVtL+kAowgZRRX8=
=x8o6
-----END PGP SIGNATURE-----



More information about the Sugar-devel mailing list