From 0ad4be3d78f100be10518e786fd4be78440a7f90 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 29 Dec 2023 16:17:51 +0100 Subject: [PATCH] LibJS: Skip redundant UTF-8 validation in rope string resolution When resolving a rope, we've already taken care to resolve it to a UTF-8 byte stream. There's no need to do a separate pass just for validating the data again. This was noticeable in some profiles. I made a simple microbenchmark that gets a 30% speed-up: ("x" + "y".repeat(100_000_000)).trimStart() --- Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp b/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp index 4095daab76..29eb4bd62d 100644 --- a/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp +++ b/Userland/Libraries/LibJS/Runtime/PrimitiveString.cpp @@ -348,7 +348,8 @@ void PrimitiveString::resolve_rope_if_needed(EncodingPreference preference) cons previous = current; } - m_utf8_string = MUST(builder.to_string()); + // NOTE: We've already produced valid UTF-8 above, so there's no need for additional validation. + m_utf8_string = builder.to_string_without_validation(); m_is_rope = false; m_lhs = nullptr; m_rhs = nullptr;