From 74b730b474a24cd56e912b4e23edbc7f79cb5e59 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:52:54 -0600 Subject: [PATCH] perf(ota): bulk-read the running image during block search The find scan issued one esp_partition_read per 4-byte step, costing ~60 ms/KB on ESP32 and pushing a 128 KB query past the client's 10 s timeout. Read the window in 1 KB chunks into a small read-ahead buffer while keeping the rolling window circular in RAM. Measured 128 KB scans dropping from 8.2 s to 0.82 s, 256 KB from 15.8 s to 1.03 s. Add a regression test that spans 64 read chunks. --- examples/simple_repeater/OtaUpdate.cpp | 40 ++++++++++++++++++++------ test/test_ota/test_ota.cpp | 36 +++++++++++++++++++++++ 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/examples/simple_repeater/OtaUpdate.cpp b/examples/simple_repeater/OtaUpdate.cpp index 0654761f..d5863d1d 100644 --- a/examples/simple_repeater/OtaUpdate.cpp +++ b/examples/simple_repeater/OtaUpdate.cpp @@ -547,9 +547,18 @@ size_t OtaUpdate::buildFindReport(const uint8_t* body, uint16_t body_len, uint8_ // and compare every fingerprint at each offset; this keeps a 12-block query // to one flash scan. static const uint64_t kPrime = 0x00000100000001B3ull; + // One esp_partition_read per 4-byte step costs ~60 ms/KB on ESP32 and made + // a 128 KB query miss the client's 10 s timeout. Refill a read-ahead buffer + // in bulk; the rolling window itself stays circular in RAM. + constexpr uint32_t kReadChunk = 1024; // multiple of OTA_HASH_ALIGN uint8_t* buf = (uint8_t*)malloc(len); - if (buf == nullptr) return 0; - if (!slotRead(_io, false, start, buf, len)) { free(buf); return 0; } + uint8_t* ahead = (uint8_t*)malloc(kReadChunk); + if (buf == nullptr || ahead == nullptr) { + free(buf); free(ahead); return 0; + } + if (!slotRead(_io, false, start, buf, len)) { + free(buf); free(ahead); return 0; + } uint64_t h = 0; for (uint32_t i = 0; i < len; i++) h = h * kPrime + buf[i]; uint64_t pow = 1, base = kPrime; @@ -559,8 +568,11 @@ size_t OtaUpdate::buildFindReport(const uint8_t* body, uint16_t body_len, uint8_ } // Circular window: each step overwrites the four oldest bytes, so sliding - // costs four writes instead of shifting the whole block. + // costs four writes instead of shifting the whole block. Incoming bytes are + // served from the read-ahead buffer, refilled a chunk at a time. uint32_t head = 0; + uint32_t aheadPos = 0, aheadLen = 0; + uint32_t nextRead = start + len; for (uint32_t off = start; off + len <= end && remaining > 0; off += OTA_HASH_ALIGN) { for (uint8_t i = 0; i < count; i++) { @@ -570,15 +582,27 @@ size_t OtaUpdate::buildFindReport(const uint8_t* body, uint16_t body_len, uint8_ if (++rec[0] == OTA_FIND_HITS_PER) remaining--; } if (remaining == 0 || off + OTA_HASH_ALIGN + len > end) break; - uint8_t in[OTA_HASH_ALIGN]; - if (!slotRead(_io, false, off + len, in, sizeof(in))) break; + bool refilled = true; for (uint8_t k = 0; k < OTA_HASH_ALIGN; k++) { - h = (h - (uint64_t)buf[head] * pow) * kPrime + in[k]; - buf[head] = in[k]; + if (aheadPos == aheadLen) { + uint32_t avail = end - nextRead; + if (avail < OTA_HASH_ALIGN) { refilled = false; break; } + uint32_t want = avail > kReadChunk ? kReadChunk : avail; + if (!slotRead(_io, false, nextRead, ahead, want)) { + refilled = false; break; + } + aheadLen = want; + aheadPos = 0; + nextRead += want; + } + uint8_t b = ahead[aheadPos++]; + h = (h - (uint64_t)buf[head] * pow) * kPrime + b; + buf[head] = b; if (++head == len) head = 0; } + if (!refilled) break; } - free(buf); + free(buf); free(ahead); uint8_t* w = out + 4; for (uint8_t i = 0; i < count; i++) { diff --git a/test/test_ota/test_ota.cpp b/test/test_ota/test_ota.cpp index 2cd69e91..62ccde50 100644 --- a/test/test_ota/test_ota.cpp +++ b/test/test_ota/test_ota.cpp @@ -894,6 +894,42 @@ TEST_F(OtaTest, BlockFindRejectsBadRangesWithoutDisturbingStaging) { EXPECT_EQ(ota.state(), OtaUpdate::State::Staging); } +TEST_F(OtaTest, BlockFindSpanningManyReadChunksFindsEveryFingerprint) { + // The bulk read-ahead refills every 1 KB; make the scan cross dozens of + // chunks so an off-by-one in the refill path cannot hide in a small image. + const uint32_t seg_len = 64 * 1024; + std::vector img(24 + 8 + seg_len, 0); + img[0] = 0xE9; + img[1] = 1; + img[0x0c] = 9; + img[0x17] = 1; // hash_appended + img[24 + 4] = (uint8_t)(seg_len & 0xFF); + img[24 + 5] = (uint8_t)((seg_len >> 8) & 0xFF); + img[24 + 6] = (uint8_t)((seg_len >> 16) & 0xFF); + uint32_t rng = 0x12345678; + for (uint32_t k = 0; k < seg_len; k++) { + rng ^= rng << 13; rng ^= rng >> 17; rng ^= rng << 5; + img[24 + 8 + k] = (uint8_t)rng; + } + img.resize((img.size() + 1 + 15) & ~(size_t)15); + uint8_t digest[32]; + mockSha256(img, digest); + img.insert(img.end(), digest, digest + 32); + std::copy(img.begin(), img.end(), slot.source.begin()); + + const uint32_t offsets[] = {0, 4096, 12288, 32768, 57344}; + std::vector wants; + for (auto o : offsets) wants.push_back(rollingTest(img, o, 32)); + auto report = feed(findFrame(32, 0, (uint32_t)img.size(), wants)); + ASSERT_FALSE(report.empty()); + ASSERT_EQ(report[2], OTA_OP_BLOCK_FIND_REPORT); + for (size_t i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) { + auto hits = findHits(report, i); + EXPECT_TRUE(std::find(hits.begin(), hits.end(), (int)offsets[i]) != hits.end()) + << "fingerprint at " << offsets[i] << " not found"; + } +} + TEST_F(OtaTest, BlockHashWithoutAnImageReportsZeroSize) { // FakeSlotIo starts as erase fill, which is not an ESP app image. auto report = feed(hashFrame({}));