perf: cache PDFium document across page operations#89
Open
AdemBoukhris457 wants to merge 5 commits intorun-llama:mainfrom
Open
perf: cache PDFium document across page operations#89AdemBoukhris457 wants to merge 5 commits intorun-llama:mainfrom
AdemBoukhris457 wants to merge 5 commits intorun-llama:mainfrom
Conversation
Add loadDocument()/closeDocument() to PdfiumRenderer so the PDF is loaded once and reused for all page renders and image-bound queries, instead of re-reading and re-parsing on every call. Wire up caching in PdfJsEngine and LiteParse.screenshot(). Methods fall back to per-call loading when no document is pre-loaded.
Add loadDocument()/closeDocument() to PdfiumRenderer so the PDF is loaded once and reused for all page renders and image-bound queries, instead of re-reading and re-parsing on every call. Wire up caching in PdfJsEngine and LiteParse.screenshot(). Methods fall back to per-call loading when no document is pre-loaded.
…to perf/pdfium-document-caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
loadDocument()/closeDocument()lifecycle toPdfiumRendererso the PDF is loaded once and reused across all per-page calls instead of re-parsing on every invocation.PdfJsEngine(extractPage + renderPageImage) andLiteParse.screenshot().Problem
Both
renderPageToBuffer()andextractImageBounds()inPdfiumRendererperformfs.readFile()+loadDocument()+destroy()on every single call. For a 100-page document this means 100-200 full PDF load/parse/destroy cycles with redundant disk I/O, CPU usage, and memory churn.Changes
src/engines/pdf/pdfium-renderer.ts: AddcachedDocumentfield,loadDocument(),closeDocument(), and privategetOrLoadDocument()helper. RefactorrenderPageToBuffer()andextractImageBounds()to reuse cached document. Updateclose()to clean up cached document.src/engines/pdf/pdfjs.ts: CallloadDocument()on the PdfiumRenderer after creation inextractPage()andrenderPageImage().src/core/parser.ts: Callrenderer.loadDocument()inscreenshot()before the page loop.src/engines/pdf/pdfium-renderer.test.ts: Rewrite mocks for repeated calls, add 4 tests for caching behavior.src/engines/pdf/pdfjs.test.ts,src/core/parser.test.ts: AddloadDocument/closeDocumentto PdfiumRenderer mocks.Closes #88