Skip to content

Chunked getSectionsText() to lower peak memory#828

Open
splitbrain wants to merge 1 commit into
smalot:masterfrom
cosmocode:mem/stream-sections
Open

Chunked getSectionsText() to lower peak memory#828
splitbrain wants to merge 1 commit into
smalot:masterfrom
cosmocode:mem/stream-sections

Conversation

@splitbrain

Copy link
Copy Markdown

Part 2 of #824

getSectionsText() ran the formatted content stream through a single preg_split() into an array of every line, then kept only the handful needed for text positioning. A vector-graphics-heavy page can format to hundreds of thousands of lines while yielding almost no text, so that array was the peak of the whole text-extraction phase.

Split the formatted stream in, line-aligned 1 MB chunks instead. Each chunk is then preg_split(), filtered and then discarded. Where the source stream is smaller than 1 MB, there is no change in behaviour.

Output is byte-for-byte identical, and there is virtually no throughput cost.

Measured peak memory (parseFile + getText on the whole file) vs master, on a graphics heavy document:

samples/bugs/Issue356.pdf 79.4 MB -> 59.2 MB (-25%)

Other documents are unchanged (PullRequest457.pdf 146.9 MB, DocumentWithLotsOfObjects.pdf 99.1 MB, Issue391.pdf 10.4 MB).

getSectionsText() ran the formatted content stream through a single
preg_split() into an array of every line, then kept only the handful
needed for text positioning. A vector-graphics-heavy page can format
to hundreds of thousands of lines while yielding almost no text, so
that array was the peak of the whole text-extraction phase.

Split the formatted stream in, line-aligned 1 MB chunks instead. Each
chunk is then preg_split(), filtered and then discarded. Where the
source stream is smaller than 1 MB, there is no change in behaviour.

Output is byte-for-byte identical, and there is virtually no
throughput cost.

Measured peak memory (parseFile + getText on the whole file) vs
master, on a graphics heavy document:

  samples/bugs/Issue356.pdf   79.4 MB -> 59.2 MB  (-25%)

Other documents are unchanged (PullRequest457.pdf 146.9 MB,
DocumentWithLotsOfObjects.pdf 99.1 MB, Issue391.pdf 10.4 MB).
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