mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-02 22:30:31 +00:00
AK: Add ASCII fast path in StringBuilder::append(Utf16View)
And let's at least try to pre-allocate an appropriate amount of buffer space in the builder instead of appending and growing one code point at a time.
This commit is contained in:
parent
d16414b61e
commit
b6e28ff807
Notes:
github-actions[bot]
2024-10-25 13:11:04 +00:00
Author: https://github.com/awesomekling
Commit: b6e28ff807
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1961
1 changed files with 12 additions and 0 deletions
|
@ -224,7 +224,19 @@ void StringBuilder::append_code_point(u32 code_point)
|
||||||
|
|
||||||
ErrorOr<void> StringBuilder::try_append(Utf16View const& utf16_view)
|
ErrorOr<void> StringBuilder::try_append(Utf16View const& utf16_view)
|
||||||
{
|
{
|
||||||
|
// NOTE: This may under-allocate in the presence of surrogate pairs.
|
||||||
|
// That's okay, appending will still grow the buffer as needed.
|
||||||
|
TRY(will_append(utf16_view.length_in_code_units()));
|
||||||
|
|
||||||
for (size_t i = 0; i < utf16_view.length_in_code_units();) {
|
for (size_t i = 0; i < utf16_view.length_in_code_units();) {
|
||||||
|
// OPTIMIZATION: Fast path for ASCII characters.
|
||||||
|
auto code_unit = utf16_view.data()[i];
|
||||||
|
if (code_unit <= 0x7f) {
|
||||||
|
append(static_cast<char>(code_unit));
|
||||||
|
++i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
auto code_point = utf16_view.code_point_at(i);
|
auto code_point = utf16_view.code_point_at(i);
|
||||||
TRY(try_append_code_point(code_point));
|
TRY(try_append_code_point(code_point));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue