From 93b3f126807eeee5dc8ea165cac40dce44dca918 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 14 Jul 2023 09:06:01 -0400 Subject: [PATCH] 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 --- Userland/Libraries/LibPDF/ObjectDerivatives.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibPDF/ObjectDerivatives.cpp b/Userland/Libraries/LibPDF/ObjectDerivatives.cpp index b2ef5b5a74..3b9f1b66e4 100644 --- a/Userland/Libraries/LibPDF/ObjectDerivatives.cpp +++ b/Userland/Libraries/LibPDF/ObjectDerivatives.cpp @@ -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);