Skip to content

Build PDF objects incrementally to lower peak memory - #827

Open
splitbrain wants to merge 1 commit into
smalot:masterfrom
cosmocode:mem/pipeline-objects
Open

Build PDF objects incrementally to lower peak memory#827
splitbrain wants to merge 1 commit into
smalot:masterfrom
cosmocode:mem/pipeline-objects

Conversation

@splitbrain

Copy link
Copy Markdown

Part 1 of #824

parseData() used to decode every indirect object into a fully built raw object graph - deeply nested arrays that inflate the source roughly 8-10x - and return the whole thing before Parser turned any of it into PDFObjects. At that moment the entire raw graph and the PDF string were held simultaneously, which is the peak for most documents.

Replace parseData() with parseHeaderAndXref() plus a getObjectsStream() generator. Parser now builds each PDFObject and releases the raw structure before pulling the next one, so the full raw graph is never materialized, and the PDF string is freed before text extraction.

Backward compatibility note: the public RawDataParser::parseData() method is removed (it had no remaining callers). Third party consumers (I don't think there are any) that called it directly should switch to parseHeaderAndXref() + getObjectsStream(), or iterate the latter into an array for the old [$xref, $objects] return shape.

Measured peak memory (getText() over the whole file), vs master:

samples/bugs/PullRequest457.pdf 147 MB -> 88 MB (-40%)
samples/DocumentWithLotsOfObjects.pdf 99 MB -> 62 MB (-38%)

Documents whose peak occurs during text extraction rather than parsing (e.g. Issue356.pdf, Issue391.pdf) are unchanged. Extracted text is identical in all cases.

parseData() used to decode every indirect object into a fully built raw
object graph - deeply nested arrays that inflate the source roughly
8-10x - and return the whole thing before Parser turned any of it into
PDFObjects. At that moment the entire raw graph and the PDF string were
held simultaneously, which is the peak for most documents.

Replace parseData() with parseHeaderAndXref() plus a getObjectsStream()
generator. Parser now builds each PDFObject and releases the raw
structure before pulling the next one, so the full raw graph is never
materialized, and the PDF string is freed before text extraction.

Backward compatibility note: the public RawDataParser::parseData()
method is removed (it had no remaining callers). Third party consumers
(I don't think there are any) that called it directly should switch
to parseHeaderAndXref() + getObjectsStream(), or iterate the latter into
an array for the old [$xref, $objects] return shape.

Measured peak memory (getText() over the whole file), vs master:

  samples/bugs/PullRequest457.pdf        147 MB -> 88 MB  (-40%)
  samples/DocumentWithLotsOfObjects.pdf   99 MB -> 62 MB  (-38%)

Documents whose peak occurs during text extraction rather than parsing
(e.g. Issue356.pdf, Issue391.pdf) are unchanged. Extracted text is
identical in all cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant