Theses ideas is not good or bad, but are just experimentation to see the cost of a feature on the final binary size.
All the tests came from the original execution of cython on kivy/graphics/texture.pyx
. 40927 bytes.
A fresh compile using the default options give:
original: 899262
stripped: 187200
This is just a theory where all the cython functions would be declared and compiled aside, and shipped into the final binary. Instead of having N times the functions in the final binary, we would have it only once. Kivy with the default options compile 22 cython extensions. Cython is also used in other python-for-android recipes as android, ffmpeg, pyjnius, twisted, audiostream, lxml, cymunk, etc.
Here, the test consist to remove all the static functions from the generated .c, and rename the static proto to extern:
original: 770481
stripped: 170848
The gain is:
original: 128781
stripped: 16352
With lot of approx, the saving on the Kivy library itself would be:
stripped: 16352 * 22 = 359744
I tested the idea 2 upon the previous .c, with the size:
original: 770481
stripped: 170848
First try, remove filename / lineno / clineo when a call failed:
:%sno/__pyx_filename = __pyx_f[\d\*]; __pyx_lineno = \d\*; __pyx_clineno = \(__LINE__\|\d\*\);//g
-> 856 lines removed
The size result for the first try is:
original: 662671
stripped: 150368
Next try, remove traceback generation:
:%sno/__Pyx_AddTraceback(\.\*);//g
-> 51 lines removed
The size of the second try is:
original: 652348
stripped: 150368
The gain is:
original: 118133
stripped: 20480
This is not a new idea, as it was already used by default on python-for-android and kivy-ios... before. This is not the case anymore, as Cython was having an issue using -D. This should be restored on both toolchain by default now.
The original .c with docstrings is:
original: 899262
stripped: 187200
After removing the docstrings (cython -D)
original: 886517
stripped: 176544
The gain is:
original: 12745
stripped: 10656
Original stripped size was | 187200 |
- idea 1 (externalize cython lib) | - 16352 |
- idea 2 (remove traceback info) | - 20480 |
- idea 3 (remove docstrings) | - 10656 |
Final stripped size | 139712 |
⇒ ~25% file size gain, without changing the gcc compilation
Original stripped size was | 187200 |
-Os stripped size is | 127408 |
- idea 1 (externalize cython lib) | - 16352 |
- idea 2 (remove traceback info) | - 20480 |
- idea 3 (remove docstrings) | - 10656 |
Final stripped size | 79920 * |
- The ideas have been tested on -O2, not -Os. The impact would be smaller as well.
⇒ ~42% file size gain, using all the ideas + -Os