Branch: refs/heads/main
Home: https://github.com/WebKit/WebKit
Commit: 2d30c8e50cab5765cebc41a7de0d179a28fbf827
https://github.com/WebKit/WebKit/commit/2d30c8e50cab5765cebc41a7de0d179a28fbf827
Author: Ahmad Saleem <[email protected]>
Date: 2026-09-21 (Mon, 21 Sep 2026)
Changed paths:
M Source/WTF/wtf/text/StringImpl.cpp
M Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp
Log Message:
-----------
Avoid allocation in StringImpl::simplifyWhiteSpace() when the string is
already simplified
https://bugs.webkit.org/show_bug.cgi?id=324694
rdar://187966162
Reviewed by Yusuke Suzuki.
simplifyMatchedCharactersToSpace() unconditionally allocated a
StringBuffer of the full length and copied every character, only to
return *this and discard the buffer when the string turned out to need
no simplification -- the common case for prose and attribute values.
Add a cheap pre-scan that returns *this without allocating unless the
string actually needs work: a leading or trailing matched character, a
run of two or more matched characters, or a matched character that is
not already a space. The scan uses the same runtime predicate, so every
caller benefits regardless of which whitespace definition it passes.
Measured ~2x on already-simplified strings and neutral on strings that
do need collapsing. The pre-scan verdict was verified against the
rewrite exhaustively for all strings of length <= 7 over {a, b, space,
newline, tab} and 500000 random strings, with no divergence.
Test: Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp
* Source/WTF/wtf/text/StringImpl.cpp:
(WTF::StringImpl::simplifyMatchedCharactersToSpace):
* Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp:
(TestWebKitAPI::TEST(WTF, StringSimplifyWhiteSpace)):
Canonical link: https://commits.webkit.org/321536@main
To unsubscribe from these emails, change your notification settings at
https://github.com/WebKit/WebKit/settings/notifications