fix: prevent O(N²) XObject reloading in do_form() causing OOM on complex PDFs#279
Open
seongmincho315 wants to merge 1 commit into
Open
fix: prevent O(N²) XObject reloading in do_form() causing OOM on complex PDFs#279seongmincho315 wants to merge 1 commit into
seongmincho315 wants to merge 1 commit into
Conversation
Contributor
|
✅ DCO Check Passed Thanks @seongmincho315, all your commits are properly signed off. 🎉 |
Contributor
Merge ProtectionsYour pull request matches the following merge protections and will not be merged until they are valid. 🟢 Enforce conventional commitWonderful, this rule succeeded.Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
…lex PDFs When a PDF page contains many Form XObjects sharing the same resource dictionary (e.g. 2296 scatter plot data points each referencing 2301 XObjects), do_form() was calling set() on fonts, graphics states, and XObjects on every invocation — reloading all N resources N times, resulting in O(N²) memory and CPU usage that caused OOM kills. Fix: before calling set(), check whether all keys in the resource dictionary are already present in the parent chain. If so, skip the set() call entirely. This reduces the redundant reloading to O(1) per do_form() call when resources are already inherited. Fixes: docling-project/docling#2109 Signed-off-by: Seongmin Cho <seongmin.cho@genon.ai>
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
PDFs containing many Form XObjects that share a large resource dictionary (e.g. a scatter plot with 2,296 data points, each referencing 2,301 sibling XObjects) caused an OOM kill during parsing.
Root cause: In
do_form()(src/parse/pdf_decoders/stream.h),set()was unconditionally called for fonts, graphics states, and XObjects on every invocation. Whendo_form()is called N times and each call reloads all N resources, the result is O(N²) memory allocations — 2,296 × 2,301 ≈ 5.3 million redundant reloads in the reported case.Fix: Before calling
set(), check whether all keys in the child resource dictionary are already present in the parent chain. If so, skip theset()call. Resources inherited from a parent Form XObject are already available, so re-registering them on everydo_form()call is unnecessary.Test case
Related issue: docling-project/docling#2109
Checklist
src/parse/pdf_decoders/stream.h)