[Bf-committers] Code optimisation

Bill Baxter bf-committers@blender.org
Thu, 22 Apr 2004 21:30:48 -0400


Yes, I believe that will be done automatically by any half-decent 
compiler.  It's called common subexpression elimination CSE (or CSEE).  
I think it's pretty much covered on day one of any advanced compilers 
course.  It's pretty much the lowest hanging fruit there is when it 
comes to code optimizations, next to removing name dependencies.

Besides, it's utterly pointless to optimize something like this until 
you profile and find out it is actually a bottleneck.  As Amdahl's Law 
tells us, if the code you optimize is only responsible for 0.1% of the 
execution time, then even if you make that code infinitely fast, the 
best you can do is speed up the program by 0.1%.  Finally, even if it 
*is* really a bottleneck, then after you think you've optimized it you 
better profile again to make sure it actually runs faster.  Often the 
compiler will be smarter than you think since it might know about actual 
register counts and other architectural details about the machine that 
affect performance of code.  Sometimes "optimizations" can restrict the 
compiler's freedom to allocate registers how it wants.

--bb

Kenneth Styrberg wrote:

> Hi, I did some optimisation in interface_draw.c. I havn't tested how 
> much it improves performance, at least the code looks  a tiny winy bit 
> cleaner. Maybe all compilers do this optimisation at compile time?
>
> //styken
>
>
> RCS file: 
> /cvsroot/bf-blender/blender/source/blender/src/interface_draw.c,v
> retrieving revision 1.14
> diff -u -r1.14 interface_draw.c
> --- interface_draw.c    28 Jan 2004 12:16:05 -0000    1.14
> +++ interface_draw.c    22 Apr 2004 21:55:45 -0000
> @@ -228,7 +228,8 @@
>      * an alignment group or not. 0 = not middle, 1 = is in the middle.
>      * Done to allow cleaner drawing
>      */
> -     +   +    float temp = y2 - (y2 - y1) / 3.0;
>     /* *** SHADED BUTTON BASE *** */
>     glShadeModel(GL_SMOOTH);
>     glBegin(GL_QUADS);
> @@ -252,8 +253,8 @@
>         else M_LIGHT;
>     }
>
> -    glVertex2f(x2,(y2-(y2-y1)/3));
> -    glVertex2f(x1,(y2-(y2-y1)/3));
> +    glVertex2f(x2,temp);
> +    glVertex2f(x1,temp);
>     glEnd();
>   
> @@ -268,8 +269,8 @@
>         else M_LIGHT;
>     }
>    -    glVertex2f(x1,(y2-(y2-y1)/3));
> -    glVertex2f(x2,(y2-(y2-y1)/3));
> +    glVertex2f(x1, temp);
> +    glVertex2f(x2, temp);
>     glVertex2f(x2,y2);
>     glVertex2f(x1,y2);
>
> @@ -390,21 +391,23 @@
> /* small side double arrow for iconrow */
> static void ui_default_iconrow_arrows(float x1, float y1, float x2, 
> float y2)
> {
> +    short temp = (short)(y2 - (y2 - y1) / 2.0);
> +
>     glEnable( GL_POLYGON_SMOOTH );
>     glEnable( GL_BLEND );
>     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
>        glShadeModel(GL_FLAT);
>     glBegin(GL_TRIANGLES);
> -    glVertex2f((short)x2-2,(short)(y2-(y2-y1)/2)+1);
> -    glVertex2f((short)x2-6,(short)(y2-(y2-y1)/2)+1);
> -    glVertex2f((short)x2-4,(short)(y2-(y2-y1)/2)+4);
> +    glVertex2f((short)x2-2, temp + 1);
> +    glVertex2f((short)x2-6, temp + 1);
> +    glVertex2f((short)x2-4, temp + 4);
>     glEnd();
>            glBegin(GL_TRIANGLES);
> -    glVertex2f((short)x2-2,(short)(y2-(y2-y1)/2) -1);
> -    glVertex2f((short)x2-6,(short)(y2-(y2-y1)/2) -1);
> -    glVertex2f((short)x2-4,(short)(y2-(y2-y1)/2) -4);
> +    glVertex2f((short)x2-2, temp - 1);
> +    glVertex2f((short)x2-6, temp - 1);
> +    glVertex2f((short)x2-4, temp - 4);
>     glEnd();
>        glDisable( GL_BLEND );
> @@ -438,6 +441,8 @@
> /* left/right arrows for number fields */
> static void ui_default_num_arrows(float x1, float y1, float x2, float y2)
> {
> +    short temp = (short)(y2 - (y2 - y1) / 2.0);
> +
>     glEnable( GL_POLYGON_SMOOTH );
>     glEnable( GL_BLEND );
>     glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
> @@ -445,18 +450,18 @@
>     glShadeModel(GL_FLAT);
>     glBegin(GL_TRIANGLES);
>    -    glVertex2f((short)x1+5,(short)(y2-(y2-y1)/2));
> -    glVertex2f((short)x1+10,(short)(y2-(y2-y1)/2)+4);
> -    glVertex2f((short)x1+10,(short)(y2-(y2-y1)/2)-4);
> +    glVertex2f((short)x1 + 5, temp);
> +    glVertex2f((short)x1 + 10, temp + 4);
> +    glVertex2f((short)x1 + 10, temp - 4);
>     glEnd();
>
>     /* right */
>     glShadeModel(GL_FLAT);
>     glBegin(GL_TRIANGLES);
>
> -    glVertex2f((short)x2-5,(short)(y2-(y2-y1)/2));
> -    glVertex2f((short)x2-10,(short)(y2-(y2-y1)/2)-4);
> -    glVertex2f((short)x2-10,(short)(y2-(y2-y1)/2)+4);
> +    glVertex2f((short)x2 - 5, temp);
> +    glVertex2f((short)x2 - 10, temp - 4);
> +    glVertex2f((short)x2 - 10, temp + 4);
>     glEnd();
>        glDisable( GL_BLEND );
>
>
> _______________________________________________
> Bf-committers mailing list
> Bf-committers@blender.org
> http://www.blender.org/mailman/listinfo/bf-committers