mozilla/pdf.js — pull request #4444 by nnethercote (2014-03-12 23:41:09+00:00)
Title: Estimate the size of decoded streams in advance.

When decoding a stream, the decode buffer is often grown multiple times, its
byte size increasing like so: 512, 1024, 2048, etc. This patch estimates the
minimum size in advance (using the length of the encoded stream), often
allowing the smaller sizes to be skipped. It also renames numerous |length|
variables as |maybeLength| to make it clear that they can be |null|.

I measured this change on eight documents. This change reduces the cumulative
size of decode buffer allocations by 0--32%, with 10--20% being typical. This
reduces peak RSS by 10 or 20 MiB for several of them.

BTW, I think Ascii85 and AsciiHex streams are the only ones for which the decoded data is regularly smaller than the encoded data, and I've accounted for that. Please let me know if any of the other stream kinds have the same behaviour.