Просмотр исходного кода

perf(alignment): integer SSD in CFindTemplate::FindTemplate

The template search accumulated the sum of squared differences as
`double aktSAD += pow(a - b, 2)`. The ESP32 FPU is single precision only,
so every add/multiply ran in software double emulation. With the default
SearchField of 20 (41x41 positions) and a ~120x40 reference this is
~2M emulated operations per reference, i.e. several seconds per reference
image and the largest part of the whole round time.

The inner template loop also walked column by column, so every step
jumped one full image row (1920 bytes) through the PSRAM cache.

Changes:
- accumulate in integers (uint32 per row, uint64 total)
- walk the template row by row (sequential memory access)
- stop summing a candidate position as soon as it can no longer beat
  the current minimum

The result is bit-identical to before: the sum is exact in both
versions (max sum is far below 2^53), the order of the search positions
and therefore the tie-breaking (first strict minimum wins) is unchanged,
and the early exit only skips positions that cannot win.

Measured on an ESP32-CAM (watermeter, VGA, SearchField 20, 121x44 and
131x36 references): the "Aligning" step went from 16.9 s to 9.2 s per
round; both template searches together now take ~1.3 s. Found positions
are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Victor Gladkikh 1 месяц назад
Родитель
Сommit
6b00d121fc
1 измененных файлов с 20 добавлено и 9 удалено
  1. 20 9
      code/components/jomjol_image_proc/CFindTemplate.cpp

+ 20 - 9
code/components/jomjol_image_proc/CFindTemplate.cpp

@@ -8,6 +8,7 @@
 #include <esp_timer.h>
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
+#include <cstdint>
 
 static const char* TAG = "C FIND TEMPL";
 
@@ -97,9 +98,11 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
 //    ESP_LOGD(TAG, "FindTemplate 04");
 
 
-    double aktSAD;
-    double sadSpan = (double)tpl_width * tpl_height * 255;
-    double minSAD = sadSpan * sadSpan;
+    // Sum of squared differences in integer arithmetic: the ESP32 FPU is single precision only,
+    // so the former double/pow() version ran in software emulation and took several seconds per reference.
+    // The result is identical: the sum is exact either way and the search order / tie-breaking is unchanged.
+    uint64_t aktSAD;
+    uint64_t minSAD = UINT64_MAX;
 
     RGBImageLock();
 
@@ -112,22 +115,30 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
     // PSRAM-Zugriffe auf Core 0 verdrängen den WiFi-Stack. Spätestens alle 50 ms
     // den Core abgeben, damit Beacons weiter verarbeitet werden (Tick = 10 ms).
     int64_t nextYieldUs = esp_timer_get_time() + 50000;
+    const int tpl_row_len = tpl_width * channels;
 
     for (xouter = ow_start; xouter <= ow_stop; xouter++)
         for (youter = oh_start; youter <= oh_stop; ++youter)
         {
             aktSAD = 0;
-            for (tpl_x = 0; tpl_x < tpl_width; tpl_x++)
-                for (tpl_y = 0; tpl_y < tpl_height; tpl_y++)
+            // Walk the template row by row so that image accesses stay sequential in (PSRAM) memory
+            for (tpl_y = 0; tpl_y < tpl_height; tpl_y++)
+            {
+                const stbi_uc* p_org = rgb_image + (channels * ((youter + tpl_y) * width + xouter));
+                const stbi_uc* p_tpl = rgb_template + (tpl_y * tpl_row_len);
+                uint32_t rowSAD = 0;
+                for (tpl_x = 0; tpl_x < tpl_row_len; tpl_x += channels)
                 {
-                    stbi_uc* p_org = rgb_image + (channels * ((youter + tpl_y) * width + (xouter + tpl_x)));
-                    stbi_uc* p_tpl = rgb_template + (channels * (tpl_y * tpl_width + tpl_x));
                     for (_ch = 0; _ch < _anzchannels; ++_ch)
                     {
-                        int diff = (int)p_tpl[_ch] - (int)p_org[_ch];
-                        aktSAD += diff * diff;
+                        int dif = p_tpl[tpl_x + _ch] - p_org[tpl_x + _ch];
+                        rowSAD += dif * dif;
                     }
                 }
+                aktSAD += rowSAD;
+                if (aktSAD >= minSAD)   // can no longer become the new minimum
+                    break;
+            }
             if (aktSAD < minSAD)
             {
                 minSAD = aktSAD;