From 7f3269fb025051c3eb2794b15e785b85b0ce37f3 Mon Sep 17 00:00:00 2001 From: Jonne Ransijn Date: Sun, 27 Oct 2024 14:03:16 +0100 Subject: [PATCH] AK: Add ASCII fast path for `Utf8View::contains` This also makes `Utf8View::trim` significantly faster, since most strings start and end with ASCII. --- AK/Utf8View.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/AK/Utf8View.cpp b/AK/Utf8View.cpp index 151b5918c46..edc50e62bae 100644 --- a/AK/Utf8View.cpp +++ b/AK/Utf8View.cpp @@ -104,10 +104,19 @@ bool Utf8View::starts_with(Utf8View const& start) const bool Utf8View::contains(u32 needle) const { - for (u32 code_point : *this) { - if (code_point == needle) - return true; + if (needle <= 0x7f) { + // OPTIMIZATION: Fast path for ASCII + for (u8 code_point : as_string()) { + if (code_point == needle) + return true; + } + } else { + for (u32 code_point : *this) { + if (code_point == needle) + return true; + } } + return false; }