LibWeb: Avoid expensive Vector::insert() in innerText & outerText
Some checks failed
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, Clang (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Build Dev Container Image / build (push) Has been cancelled

When expanding "required line breaks", we now write directly into the
final StringBuilder instead of doing a pass of Vector remove/insert
operations first.

This removes a hot profile item on https://serpapi.com/
This commit is contained in:
Andreas Kling 2025-06-04 13:02:37 +02:00 committed by Jelle Raaijmakers
parent 326a8399a4
commit 63d862219e
Notes: github-actions[bot] 2025-06-19 15:32:27 +00:00

View file

@ -422,28 +422,28 @@ String HTMLElement::get_the_text_steps()
// 6. Replace each remaining run of consecutive required line break count items
// with a string consisting of as many U+000A LF code points as the maximum of the values
// in the required line break count items.
StringBuilder builder;
for (size_t i = 0; i < results.size(); ++i) {
if (!results[i].has<RequiredLineBreakCount>())
continue;
results[i].visit(
[&](String const& string) {
builder.append(string);
},
[&](RequiredLineBreakCount const& line_break_count) {
int max_line_breaks = line_break_count.count;
size_t j = i + 1;
while (j < results.size() && results[j].has<RequiredLineBreakCount>()) {
max_line_breaks = max(max_line_breaks, results[j].get<RequiredLineBreakCount>().count);
++j;
}
int max_line_breaks = results[i].get<RequiredLineBreakCount>().count;
size_t j = i + 1;
while (j < results.size() && results[j].has<RequiredLineBreakCount>()) {
max_line_breaks = max(max_line_breaks, results[j].get<RequiredLineBreakCount>().count);
++j;
}
// Skip over the run of required line break counts.
i = j - 1;
results.remove(i, j - i);
results.insert(i, MUST(String::repeated('\n', max_line_breaks)));
builder.append_repeated('\n', max_line_breaks);
});
}
// 7. Return the concatenation of the string items in results.
StringBuilder builder;
for (auto& item : results) {
item.visit(
[&](String const& string) { builder.append(string); },
[&](RequiredLineBreakCount const&) {});
}
return builder.to_string_without_validation();
}