Fix #1268: cache parsed fonts during content stream processing#1595
Open
andreasrosdalw wants to merge 2 commits into
Open
Fix #1268: cache parsed fonts during content stream processing#1595andreasrosdalw wants to merge 2 commits into
andreasrosdalw wants to merge 2 commits into
Conversation
Every Tf operator in a content stream constructed a new CMapAwareDocumentFont, which parses the embedded font program and the ToUnicode CMap from scratch. A page that switches fonts hundreds of times (the PDF attached to the issue has 386 Tf operators on page 4) allocated gigabytes of short-lived objects during text extraction - the reporter measured ~17 GB; with a 1 GB heap the extraction dies with OutOfMemoryError inside the CMap parser. PdfContentStreamHandler now caches parsed fonts by the object number of their font dictionary, so each font is parsed once per handler instead of once per Tf operator. With the fix, extracting all 7 pages of the issue's PDF completes in ~0.5 s within a 256 MB heap. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 6 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
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
Fixes #1268.
Extracting text from the PDF attached to the issue used ~17 GB of memory on page 4. Reproduced locally: with a 1 GB heap,
PdfTextExtractor.getTextFromPage(4)dies withOutOfMemoryErrorinside the CMap parser; pages 1–3 use ~30 MB.Root cause
PdfContentStreamHandler$SetTextFont(theTfoperator) constructed a newCMapAwareDocumentFontfor every font-selection operator in the content stream:Constructing that font parses the embedded font program and the ToUnicode CMap from scratch. Page 4 of the issue's PDF contains 386
Tfoperators across 12 fonts (vs ≤73 on other pages), so the same dozen fonts were fully re-parsed hundreds of times, allocating gigabytes of short-lived objects.Fix
PdfContentStreamHandlernow caches parsed fonts by the object number of their font dictionary; each font is parsed once per handler instead of once perTfoperator. This mirrors the font caching iText later added to its content stream processor.Measured on the issue's PDF (all 7 pages, page-by-page extraction):
OutOfMemoryErrorat 1 GB heap (~17 GB reported)Extracted text is unchanged for the pages that completed before the fix.
Tests
New
PdfContentStreamHandlerFontCacheTestasserts that the handler returns the same parsed font instance for repeated lookups of the same font reference, and that text extraction output is unaffected. Fullopenpdf-coresuite passes.