changeset 1870:e3108a01e841

Optimize the map matching function by removing unnecessary computations and checks from the inner loop.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 04 Nov 2017 16:37:33 +0200
parents 03b6f161ce90
children a6990341df22
files mapsearch.c
diffstat 1 files changed, 5 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mapsearch.c	Sat Nov 04 16:36:52 2017 +0200
+++ b/mapsearch.c	Sat Nov 04 16:37:33 2017 +0200
@@ -683,7 +683,7 @@
 }
 
 
-double mapMatchBlock(const MapBlock *map, const MapBlock *match, int ox, int oy, int fillCh, BOOL hardDrop)
+double mapMatchBlock(const MapBlock *map, const MapBlock *match, const int ox, const int oy, const BOOL hardDrop)
 {
     int n, k;
 
@@ -691,22 +691,15 @@
     for (int y = 0; y < match->height; y++)
     {
         unsigned char *dp = match->data + (y * match->scansize);
+        unsigned char *sp = map->data + ((y + oy) * map->scansize) + ox;
         for (int x = 0; x < match->width; x++)
         {
-            int c1, c2;
             const int
-                dx = ox + x,
-                dy = oy + y;
+                c2 = sp[x],
+                c1 = dp[x];
 
             k++;
 
-            if (dx >= 0 && dx < map->width && dy >= 0 && dy < map->height)
-                c2 = map->data[(dy * map->scansize) + dx];
-            else
-                c2 = fillCh;
-
-            c1 = dp[x];
-
             if (c1 == 0 || c1 == c2)
                 n++;
             else
@@ -894,7 +887,7 @@
         for (int ox = 0; ox < info->map->width - width - 1; ox++)
         {
             // Check for match
-            double accuracy = mapMatchBlock(info->map, pattern, ox, oy, 0, TRUE);
+            double accuracy = mapMatchBlock(info->map, pattern, ox, oy, TRUE);
             if (accuracy > 99.5f)
             {
                 // Okay, add the match to our list