LibPDF: Fix quadratic runtime in stream dumping

DeprecatedString::substring() makes a copy of the substring.
Instead, use a StringView, which can make substring views in constant
time.

Reduces time for `pdf --dump-contents image-based-pdf-sample.pdf` to
2.2s (from not completing for 1+ minutes).

That file contains a 221 kB jpeg.

Find it on the internet here:
https://nlsblog.org/wp-content/uploads/2020/06/image-based-pdf-sample.pdf
This commit is contained in:
Nico Weber 2023-07-14 09:06:01 -04:00 committed by Tim Flynn
parent d18f01d7d7
commit 93b3f12680
Notes: sideshowbarker 2024-07-17 06:46:15 +09:00

View file

@ -145,13 +145,14 @@ DeprecatedString StreamObject::to_deprecated_string(int indent) const
}
} else {
auto string = encode_hex(bytes());
while (string.length() > 60) {
builder.appendff("{}\n", string.substring(0, 60));
StringView view { string };
while (view.length() > 60) {
builder.appendff("{}\n", view.substring_view(0, 60));
append_indent(builder, indent);
string = string.substring(60);
view = view.substring_view(60);
}
builder.appendff("{}\n", string);
builder.appendff("{}\n", view);
}
builder.append("endstream"sv);