[Spice-devel] [PATCH 3/3] optimize end of row check for images

Frediano Ziglio fziglio at redhat.com
Fri Aug 21 02:57:50 PDT 2015


Doing profiles reveals that the check for "is this pixel at end of row?"

  if ((cur_pix + 1 - lines) % width == 0)

leads to 2 divisions, one to compute (cur_pix + 1 - lines) (divided by
sizeof(*cur_pix) which could be 3) and one for (x % width).
Although the substituted code looks more long is faster as it does not
involve divisions.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/red_bitmap_utils.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/server/red_bitmap_utils.h b/server/red_bitmap_utils.h
index bf82e79..1849588 100644
--- a/server/red_bitmap_utils.h
+++ b/server/red_bitmap_utils.h
@@ -117,7 +117,8 @@ static void FNAME(compute_lines_gradual_score)(PIXEL *lines, int width, int num_
                                                double *o_samples_sum_score, int *o_num_samples)
 {
     int jump = (SAMPLE_JUMP % width) ? SAMPLE_JUMP : SAMPLE_JUMP - 1;
-    PIXEL *cur_pix = lines + width / 2;
+    int cur_x = width / 2;
+    PIXEL *cur_pix = lines + cur_x;
     PIXEL *bottom_pix;
     PIXEL *last_line = lines + (num_lines - 1) * width;
     int num_samples;
@@ -133,13 +134,17 @@ static void FNAME(compute_lines_gradual_score)(PIXEL *lines, int width, int num_
     num_samples = 0;
 
     while (cur_pix < last_line) {
-        if ((cur_pix + 1 - lines) % width == 0) { // last pixel in the row
+        if (cur_x + 1 == width) { // last pixel in the row
             cur_pix--; // jump is bigger than 1 so we will not enter endless loop
+            cur_x--;
         }
         bottom_pix = cur_pix + width;
         samples_sum_score += FNAME(pixels_square_score)(cur_pix, bottom_pix);
         num_samples++;
         cur_pix += jump;
+        cur_x += jump;
+        while (cur_x >= width)
+            cur_x -= width;
     }
 
     (*o_samples_sum_score) = samples_sum_score;
-- 
2.4.3



More information about the Spice-devel mailing list