Deprecated: Function create_function() is deprecated in /var/www/blog/inc/libs/clearbricks/common/lib.l10n.php on line 640

Deprecated: Function create_function() is deprecated in /var/www/blog/inc/libs/clearbricks/common/lib.http.php on line 249
Don't trust the compiler: part #1 - Grumpycoder
Flux RSS

Don't trust the compiler: part #1

We will be starting a serie about the compiler myths. What people assume the compiler is doing, while the reality is way different. The first part is about static class initialisation.

When writting 3d code, programmers usualy create some kind of 3d vector class, that is used to abstract part of the 3d math. Such a class will generaly be declared like this:

class VECTOR3D {
    VECTOR3D(float fX, float fY, float fZ, float fW);
    float _data[4] align(16);

Because 3d math rely a lot on a using pre-defined vectors like origin (0.0f, 0.0f, 0.0f, 0.0f) or unit vectors, the programmer will have a tendency of declaring those multi-purpose like this:

const VECTOR3D someReferenceVector(1.0f, 0.0f, 0.0f, 0.0f);

Or potentialy, in the case of arrays:

const VECTOR3D someReferenceArray[] = {
    VECTOR3D (1.0f, 0.0f, 0.0f, 0.0f);
    VECTOR3D (0.0f, 1.0f, 0.0f, 0.0f);

Here, the programmer expects those vectors to be pre-initialized in read only memory, but is that what the compiler will actualy generates?

Assuming the VECTOR3D class is made of an array of 4 floats (quite logical) per vector, aligned on 16 bytes, one would assume that the compiler will generate just that: an array of 4 floats, aligned on 16 bytes, and in .rodata (read only memory) because it is const. The reality is way different. Looking at the compiler output, we will discover with horror that the compiler will generate:

  • an array of 4 floats aligned on 16 bytes in the .bss area (ie, R/W unitialized memory)
  • an array of floats
  • a bunch of code that will get executed before main that will copy the array of floats in the array located in bss.

Thus, the code will be larger, init will be longer, and the const just become a gimmick (the vector doesn't reside in read only memory, and is not protected like we espected). It may doesn't seem like much, but when working with very limited memory (like the 256kb on a Cell SPU), it may become an issue if you have a lot of arrays like that one.

The actual solution to this problem is to create a class that doesn't have any constructor, nor destructor, and with members publicly accessible:

class VECTOR3D {
    float _data[4] align(16);

Now, we can declare an actual constant vector:

const VECTOR3D someReferenceVector = {1.0, 0.0f, 0.0f, 0.0f);

or for an array:

const VECTOR3D someReferenceArray[] = {
    {1.0f, 0.0f, 0.0f, 0.0f},
    {0.0f, 1.0f, 0.0f, 0.0f}

The difference from before is that the compiler will generate the vectors in read only memory as we first expected, and there will be no initialization code generated and called in the global constructor call of the application. It may not seems like a big win, but at the scale of a SIMD intensive application (thus, with lots of pre-initilized read-only vectors), this can save you a few hundreds Kb of memory. Also, in case of spu where the initialialization code might get called each time the code will be loaded into the spu, this can save you a lot of cycles.

To conclude, never trust the compiler. It will not magically understands what you have in mind and makes the most optimal code. This is most true in C++, were a simple line of code can hide a maze of complex operations. Be curious of what the compiler outputs, the better you know and understand the compiler, the better code you can write, hinting the compiler about what you actually want it to generate.

Fatal error: Uncaught Error: [] operator not supported for strings in /var/www/blog/inc/core/class.dc.postmedia.php:61 Stack trace: #0 /var/www/blog/inc/core/ dcPostMedia->getPostMedia(Array) #1 /var/www/blog/cache/cbtpl/29/59/2959320ca61719bec433f91e69098e86.php(137): dcMedia->getPostMedia('4', NULL, 'attachment') #2 /var/www/blog/inc/libs/clearbricks/template/class.template.php(268): include('/var/www/blog/c...') #3 /var/www/blog/inc/public/class.dc.template.php(231): template->getData('post.html') #4 /var/www/blog/inc/public/lib.urlhandlers.php(122): dcTemplate->getData('post.html') #5 /var/www/blog/inc/public/lib.urlhandlers.php(523): dcUrlHandlers::serveDocument('post.html') #6 /var/www/blog/inc/libs/clearbricks/url.handler/class.url.handler.php(166): dcUrlHandlers::post('2009/09/14/Don-...') #7 /var/www/blog/inc/public/lib.urlhandlers.php(187): urlHandler->callHandler('post', '2009/09/14/Don-...') #8 /var/www/blog/inc/public/prepend.php(156): dcUrlHandlers->getDocument() #9 /var/www/blog/inde in /var/www/blog/inc/core/class.dc.postmedia.php on line 61