From 1b00ddf07e97df3f3075c900ceafe25cd5dcd3bc Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Sun, 10 Oct 2021 00:00:15 -0700 Subject: [PATCH] LibJS: Optimize PropertyName and StringPrototype for size We can reduce the amount of padding the compiler adds in order to ensure data alignment of member variables by ordering the types in a struct by size in decending order. Found By PVS-Studio: https://pvs-studio.com/en/docs/warnings/v802/ --- Userland/Libraries/LibJS/Runtime/PropertyName.h | 8 ++++---- Userland/Libraries/LibJS/Runtime/StringPrototype.cpp | 8 ++++---- Userland/Libraries/LibJS/Runtime/StringPrototype.h | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/PropertyName.h b/Userland/Libraries/LibJS/Runtime/PropertyName.h index 0c8c79f37d..c777fc3a3d 100644 --- a/Userland/Libraries/LibJS/Runtime/PropertyName.h +++ b/Userland/Libraries/LibJS/Runtime/PropertyName.h @@ -74,8 +74,8 @@ public: } PropertyName(FlyString string, StringMayBeNumber string_may_be_number = StringMayBeNumber::Yes) - : m_type(Type::String) - , m_string_may_be_number(string_may_be_number == StringMayBeNumber::Yes) + : m_string_may_be_number(string_may_be_number == StringMayBeNumber::Yes) + , m_type(Type::String) , m_string(move(string)) { VERIFY(!m_string.is_null()); @@ -184,11 +184,11 @@ public: } private: - Type m_type { Type::Invalid }; bool m_string_may_be_number { true }; + Type m_type { Type::Invalid }; + u32 m_number { 0 }; FlyString m_string; Symbol* m_symbol { nullptr }; - u32 m_number { 0 }; }; struct PropertyNameTraits : public Traits { diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index 9054837e47..96fb18ce5d 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -63,18 +63,18 @@ CodePoint code_point_at(Utf16View const& string, size_t position) auto code_point = static_cast(first); if (!Utf16View::is_high_surrogate(first) && !Utf16View::is_low_surrogate(first)) - return { code_point, 1, false }; + return { false, code_point, 1 }; if (Utf16View::is_low_surrogate(first) || (position + 1 == string.length_in_code_units())) - return { code_point, 1, true }; + return { true, code_point, 1 }; auto second = string.code_unit_at(position + 1); if (!Utf16View::is_low_surrogate(second)) - return { code_point, 1, true }; + return { true, code_point, 1 }; code_point = Utf16View::decode_surrogate_pair(first, second); - return { code_point, 2, false }; + return { false, code_point, 2 }; } // 6.1.4.1 StringIndexOf ( string, searchValue, fromIndex ), https://tc39.es/ecma262/#sec-stringindexof diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.h b/Userland/Libraries/LibJS/Runtime/StringPrototype.h index 2282b08531..50f17bb74a 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.h @@ -11,9 +11,9 @@ namespace JS { struct CodePoint { + bool is_unpaired_surrogate { false }; u32 code_point { 0 }; size_t code_unit_count { 0 }; - bool is_unpaired_surrogate { false }; }; CodePoint code_point_at(Utf16View const& string, size_t position);