瀏覽代碼

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/
Brian Gianforcaro 3 年之前
父節點
當前提交
1b00ddf07e

+ 4 - 4
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<PropertyName> {

+ 4 - 4
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<u32>(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

+ 1 - 1
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);