Optimize RAM usage: reduce parser buffers and use heap allocation

This commit is contained in:
Uri Tauber 2026-01-25 19:09:23 +02:00
parent 1374bc33ae
commit 71138a158d
2 changed files with 13 additions and 13 deletions

View File

@ -245,11 +245,11 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
viewportHeight, hyphenationEnabled); viewportHeight, hyphenationEnabled);
std::vector<uint32_t> lut = {}; std::vector<uint32_t> lut = {};
ChapterHtmlSlimParser visitor( std::unique_ptr<ChapterHtmlSlimParser> visitor(new ChapterHtmlSlimParser(
fileToParse, renderer, fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth, fileToParse, renderer, fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
viewportHeight, hyphenationEnabled, viewportHeight, hyphenationEnabled,
[this, &lut](std::unique_ptr<Page> page) { lut.emplace_back(this->onPageComplete(std::move(page))); }, [this, &lut](std::unique_ptr<Page> page) { lut.emplace_back(this->onPageComplete(std::move(page))); },
progressFn); progressFn));
Hyphenator::setPreferredLanguage(epub->getLanguage()); Hyphenator::setPreferredLanguage(epub->getLanguage());
@ -257,7 +257,7 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
std::set<std::string> rewrittenInlineIds; std::set<std::string> rewrittenInlineIds;
int noterefCount = 0; int noterefCount = 0;
visitor.setNoterefCallback([this, &noterefCount, &rewrittenInlineIds](Noteref& noteref) { visitor->setNoterefCallback([this, &noterefCount, &rewrittenInlineIds](Noteref& noteref) {
// Extract the ID from the href for tracking // Extract the ID from the href for tracking
std::string href(noteref.href); std::string href(noteref.href);
@ -277,7 +277,7 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
noterefCount++; noterefCount++;
}); });
success = visitor.parseAndBuildPages(); success = visitor->parseAndBuildPages();
if (!isVirtual) { if (!isVirtual) {
SdMan.remove(tmpHtmlPath.c_str()); SdMan.remove(tmpHtmlPath.c_str());
@ -293,9 +293,9 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
// --- Footnote Generation Logic (Merged from HEAD) --- // --- Footnote Generation Logic (Merged from HEAD) ---
// Inline footnotes // Inline footnotes
for (int i = 0; i < visitor.inlineFootnoteCount; i++) { for (int i = 0; i < visitor->inlineFootnoteCount; i++) {
const char* inlineId = visitor.inlineFootnotes[i].id; const char* inlineId = visitor->inlineFootnotes[i].id;
const char* inlineText = visitor.inlineFootnotes[i].text; const char* inlineText = visitor->inlineFootnotes[i].text;
if (rewrittenInlineIds.find(std::string(inlineId)) == rewrittenInlineIds.end()) continue; if (rewrittenInlineIds.find(std::string(inlineId)) == rewrittenInlineIds.end()) continue;
if (!inlineText || strlen(inlineText) == 0) continue; if (!inlineText || strlen(inlineText) == 0) continue;
@ -326,9 +326,9 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
} }
// Paragraph notes // Paragraph notes
for (int i = 0; i < visitor.paragraphNoteCount; i++) { for (int i = 0; i < visitor->paragraphNoteCount; i++) {
const char* pnoteId = visitor.paragraphNotes[i].id; const char* pnoteId = visitor->paragraphNotes[i].id;
const char* pnoteText = visitor.paragraphNotes[i].text; const char* pnoteText = visitor->paragraphNotes[i].text;
if (!pnoteText || strlen(pnoteText) == 0) continue; if (!pnoteText || strlen(pnoteText) == 0) continue;
if (rewrittenInlineIds.find(std::string(pnoteId)) == rewrittenInlineIds.end()) continue; if (rewrittenInlineIds.find(std::string(pnoteId)) == rewrittenInlineIds.end()) continue;

View File

@ -90,12 +90,12 @@ class ChapterHtmlSlimParser {
bool insideParagraphNote = false; bool insideParagraphNote = false;
int paragraphNoteDepth = 0; int paragraphNoteDepth = 0;
char currentParagraphNoteId[16] = {0}; char currentParagraphNoteId[16] = {0};
static constexpr int MAX_PNOTE_BUFFER = 512; static constexpr int MAX_PNOTE_BUFFER = 256;
char currentParagraphNoteText[MAX_PNOTE_BUFFER] = {0}; char currentParagraphNoteText[MAX_PNOTE_BUFFER] = {0};
int currentParagraphNoteTextLen = 0; int currentParagraphNoteTextLen = 0;
// Temporary buffer for accumulation, will be copied to dynamic allocation // Temporary buffer for accumulation, will be copied to dynamic allocation
static constexpr int MAX_ASIDE_BUFFER = 2048; static constexpr int MAX_ASIDE_BUFFER = 1024;
char currentAsideText[MAX_ASIDE_BUFFER] = {0}; char currentAsideText[MAX_ASIDE_BUFFER] = {0};
int currentAsideTextLen = 0; int currentAsideTextLen = 0;
@ -116,7 +116,7 @@ class ChapterHtmlSlimParser {
InlineFootnote inlineFootnotes[16]; InlineFootnote inlineFootnotes[16];
int inlineFootnoteCount = 0; int inlineFootnoteCount = 0;
// paragraph notes // paragraph notes
ParagraphNote paragraphNotes[32]; ParagraphNote paragraphNotes[16];
int paragraphNoteCount = 0; int paragraphNoteCount = 0;
explicit ChapterHtmlSlimParser(const std::string& filepath, GfxRenderer& renderer, const int fontId, explicit ChapterHtmlSlimParser(const std::string& filepath, GfxRenderer& renderer, const int fontId,