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:
parent
a91bb0b1b8
commit
51a4faddd4
@ -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<int16_t>(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<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());
|
||||
}
|
||||
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
#include <SDCardManager.h>
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, int16_t> hrefToSpineIndex;
|
||||
|
||||
uint32_t writeSpineEntry(FsFile& file, const SpineEntry& entry) const;
|
||||
uint32_t writeTocEntry(FsFile& file, const TocEntry& entry) const;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user