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).
This commit is contained in:
Daniel 2026-01-20 20:41:27 -08:00 committed by cottongin
parent a91bb0b1b8
commit 51a4faddd4
No known key found for this signature in database
GPG Key ID: 0ECC91FE4655C262
2 changed files with 9 additions and 18 deletions

View File

@ -49,23 +49,12 @@ bool BookMetadataCache::beginTocPass() {
return false; 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<int16_t>(i);
}
spineFile.seek(0);
return true; return true;
} }
bool BookMetadataCache::endTocPass() { bool BookMetadataCache::endTocPass() {
tocFile.close(); tocFile.close();
spineFile.close(); spineFile.close();
hrefToSpineIndex.clear();
return true; return true;
} }
@ -271,10 +260,15 @@ void BookMetadataCache::createTocEntry(const std::string& title, const std::stri
} }
int16_t spineIndex = -1; int16_t spineIndex = -1;
auto it = hrefToSpineIndex.find(href); spineFile.seek(0);
if (it != hrefToSpineIndex.end()) { for (int i = 0; i < spineCount; i++) {
spineIndex = it->second; auto spineEntry = readSpineEntry(spineFile);
} else { if (spineEntry.href == href) {
spineIndex = static_cast<int16_t>(i);
break;
}
}
if (spineIndex == -1) {
Serial.printf("[%lu] [BMC] addTocEntry: Could not find spine item for TOC href %s\n", millis(), href.c_str()); Serial.printf("[%lu] [BMC] addTocEntry: Could not find spine item for TOC href %s\n", millis(), href.c_str());
} }

View File

@ -3,7 +3,6 @@
#include <SDCardManager.h> #include <SDCardManager.h>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
class BookMetadataCache { class BookMetadataCache {
@ -55,8 +54,6 @@ class BookMetadataCache {
// Temp file handles during build // Temp file handles during build
FsFile spineFile; FsFile spineFile;
FsFile tocFile; FsFile tocFile;
// Lookup cache for O(1) href->spineIndex during TOC pass
std::unordered_map<std::string, int16_t> hrefToSpineIndex;
uint32_t writeSpineEntry(FsFile& file, const SpineEntry& entry) const; uint32_t writeSpineEntry(FsFile& file, const SpineEntry& entry) const;
uint32_t writeTocEntry(FsFile& file, const TocEntry& entry) const; uint32_t writeTocEntry(FsFile& file, const TocEntry& entry) const;