Search code examples
arrayscperformancesdlsdl-2

Accessing array in if statement significantly decreases performance


So I am trying to implement a rendering function for my array of squares. Array represents output of raycasted hitbox of explosion:

Algorithm output

For drawing I am using SDL2 library, but drawing one rect at a time is too demanding. So I came across texture streaming. Now I have two for loops that are going through my array containing hitbox information. Array is on stack is type of char (ExplosionGrid) and represents rectangles on screen, if rectangle is hit by collision it stores 1 if not then it stores 0 and if there is an obstacle it stores 2.

I have two code examples.

  1. No performance impact:
SDL_LockTexture(ExplTextFast, NULL, &ExplSurfFast->pixels, &ExplSurfFast->pitch);
            int StartRendX = (int)((crosshair.x + 10) / 6);
            int StartRendY = (int)((crosshair.y + 10) / 6);
            for (int i = 0; i < 65; i++)
            {
                for (int j = 0; j < 65; j++)
                {
                        if (1)
                        {
                            *(((int*)ExplSurfFast->pixels) + j + (i * 65)) = 0;
                        }
                        else
                        {
                            *(((int*)ExplSurfFast->pixels) + j + (i * 65)) = 0xFFFFFFFF;
                        }
                }
            }
            SDL_UnlockTexture(ExplTextFast);
            SDL_RenderCopy(renderer1, ExplTextFast, NULL, &DrawingRectExpl);

It just changes pixel data in surface.

  1. Huge performance drop:
SDL_LockTexture(ExplTextFast, NULL, &ExplSurfFast->pixels, &ExplSurfFast->pitch);
            int StartRendX = (int)((crosshair.x + 10) / 6);
            int StartRendY = (int)((crosshair.y + 10) / 6);
            for (int i = 0; i < 65; i++)
            {
                for (int j = 0; j < 65; j++)
                {
                        if (ExplosionGrid[i][j] == 1)
                        {
                            *(((int*)ExplSurfFast->pixels) + j + (i * 65)) = 0;
                        }
                        else
                        {
                            *(((int*)ExplSurfFast->pixels) + j + (i * 65)) = 0xFFFFFFFF;
                        }
                }
            }
            SDL_UnlockTexture(ExplTextFast);
            SDL_RenderCopy(renderer1, ExplTextFast, NULL, &DrawingRectExpl);

As you can see the number of iterations in very small. But at the same time it uses 20% of cpu. And the first example uses 0.0xx%. Those methods are called hundred times a second. But still the number of iterations is very small.

If I use if (rand()%2) the performance is good. But it seems like accessing an array on the stack and using it in an if statement causes performance problems.

I tried profiling my code and found out that array inside of if statements was crushing my performance, but i can not wrap my head around it why.


Solution

  • After more profiling i have found that problem is not in rendering. But in algorithm computing ExplosionGrid array. But without rendering it was not used anywhere so compiler did not iclude it. But with just one access to that array compiler compiled that algorithm and it ruined performance.