summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-10-02 17:37:15 +0200
committerAndreas Kling <kling@serenityos.org>2021-10-02 17:39:38 +0200
commit024367d82e59e66390e8a8fc0f7dd866242438f9 (patch)
tree027c34c8ba5410f795cd45d82668f2e9e696a7b0 /AK
parentae0bdda86e9946e8fc09db0c4dc044b2d975d7fa (diff)
downloadserenity-024367d82e59e66390e8a8fc0f7dd866242438f9.zip
LibJS+AK: Use Vector<u16, 1> for UTF-16 string storage
It's very common to encounter single-character strings in JavaScript on the web. We can make such strings significantly lighter by having a 1-character inline capacity on the Vectors.
Diffstat (limited to 'AK')
-rw-r--r--AK/Utf16View.cpp13
-rw-r--r--AK/Utf16View.h8
2 files changed, 11 insertions, 10 deletions
diff --git a/AK/Utf16View.cpp b/AK/Utf16View.cpp
index d49ba18692..fcac008c01 100644
--- a/AK/Utf16View.cpp
+++ b/AK/Utf16View.cpp
@@ -21,9 +21,10 @@ static constexpr u32 replacement_code_point = 0xfffd;
static constexpr u32 first_supplementary_plane_code_point = 0x10000;
template<typename UtfViewType>
-static Vector<u16> to_utf16_impl(UtfViewType const& view) requires(IsSame<UtfViewType, Utf8View> || IsSame<UtfViewType, Utf32View>)
+static Vector<u16, 1> to_utf16_impl(UtfViewType const& view) requires(IsSame<UtfViewType, Utf8View> || IsSame<UtfViewType, Utf32View>)
{
- Vector<u16> utf16_data;
+ Vector<u16, 1> utf16_data;
+ utf16_data.ensure_capacity(view.length());
for (auto code_point : view)
code_point_to_utf16(utf16_data, code_point);
@@ -31,22 +32,22 @@ static Vector<u16> to_utf16_impl(UtfViewType const& view) requires(IsSame<UtfVie
return utf16_data;
}
-Vector<u16> utf8_to_utf16(StringView const& utf8_view)
+Vector<u16, 1> utf8_to_utf16(StringView const& utf8_view)
{
return to_utf16_impl(Utf8View { utf8_view });
}
-Vector<u16> utf8_to_utf16(Utf8View const& utf8_view)
+Vector<u16, 1> utf8_to_utf16(Utf8View const& utf8_view)
{
return to_utf16_impl(utf8_view);
}
-Vector<u16> utf32_to_utf16(Utf32View const& utf32_view)
+Vector<u16, 1> utf32_to_utf16(Utf32View const& utf32_view)
{
return to_utf16_impl(utf32_view);
}
-void code_point_to_utf16(Vector<u16>& string, u32 code_point)
+void code_point_to_utf16(Vector<u16, 1>& string, u32 code_point)
{
VERIFY(is_unicode(code_point));
diff --git a/AK/Utf16View.h b/AK/Utf16View.h
index a332ed6a23..805ab08000 100644
--- a/AK/Utf16View.h
+++ b/AK/Utf16View.h
@@ -16,10 +16,10 @@
namespace AK {
-Vector<u16> utf8_to_utf16(StringView const&);
-Vector<u16> utf8_to_utf16(Utf8View const&);
-Vector<u16> utf32_to_utf16(Utf32View const&);
-void code_point_to_utf16(Vector<u16>&, u32);
+Vector<u16, 1> utf8_to_utf16(StringView const&);
+Vector<u16, 1> utf8_to_utf16(Utf8View const&);
+Vector<u16, 1> utf32_to_utf16(Utf32View const&);
+void code_point_to_utf16(Vector<u16, 1>&, u32);
class Utf16View;