From 51a4faddd4b2c7a625998ab913a3b803bcd4d13d Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 20 Jan 2026 20:41:27 -0800 Subject: [PATCH] fix: remove OOM-causing hrefToSpineIndex map from TOC pass The unordered_map with 2768 string keys (~100KB+) was causing OOM crashes at beginTocPass() on ESP32-C3's limited ~380KB RAM. Reverted createTocEntry() to use original O(n) spine file scan instead. Kept the safe spineToTocIndex vector in buildBookBin() (only ~5.5KB). --- lib/Epub/Epub/BookMetadataCache.cpp | 24 +++++++++--------------- lib/Epub/Epub/BookMetadataCache.h | 3 --- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/Epub/Epub/BookMetadataCache.cpp b/lib/Epub/Epub/BookMetadataCache.cpp index 517d59d..98f86c1 100644 --- a/lib/Epub/Epub/BookMetadataCache.cpp +++ b/lib/Epub/Epub/BookMetadataCache.cpp @@ -49,23 +49,12 @@ bool BookMetadataCache::beginTocPass() { return false; } - // Build href->spineIndex lookup map for O(1) access during TOC creation - hrefToSpineIndex.clear(); - hrefToSpineIndex.reserve(spineCount); - spineFile.seek(0); - for (int i = 0; i < spineCount; i++) { - auto entry = readSpineEntry(spineFile); - hrefToSpineIndex[entry.href] = static_cast(i); - } - spineFile.seek(0); - return true; } bool BookMetadataCache::endTocPass() { tocFile.close(); spineFile.close(); - hrefToSpineIndex.clear(); return true; } @@ -271,10 +260,15 @@ void BookMetadataCache::createTocEntry(const std::string& title, const std::stri } int16_t spineIndex = -1; - auto it = hrefToSpineIndex.find(href); - if (it != hrefToSpineIndex.end()) { - spineIndex = it->second; - } else { + spineFile.seek(0); + for (int i = 0; i < spineCount; i++) { + auto spineEntry = readSpineEntry(spineFile); + if (spineEntry.href == href) { + spineIndex = static_cast(i); + break; + } + } + if (spineIndex == -1) { Serial.printf("[%lu] [BMC] addTocEntry: Could not find spine item for TOC href %s\n", millis(), href.c_str()); } diff --git a/lib/Epub/Epub/BookMetadataCache.h b/lib/Epub/Epub/BookMetadataCache.h index e0efc36..c7e9590 100644 --- a/lib/Epub/Epub/BookMetadataCache.h +++ b/lib/Epub/Epub/BookMetadataCache.h @@ -3,7 +3,6 @@ #include #include -#include #include class BookMetadataCache { @@ -55,8 +54,6 @@ class BookMetadataCache { // Temp file handles during build FsFile spineFile; FsFile tocFile; - // Lookup cache for O(1) href->spineIndex during TOC pass - std::unordered_map hrefToSpineIndex; uint32_t writeSpineEntry(FsFile& file, const SpineEntry& entry) const; uint32_t writeTocEntry(FsFile& file, const TocEntry& entry) const;