Browse Source

LibJS: Devirtualize is_simple_storage()

iliadsh 1 năm trước cách đây
mục cha
commit
ee4948b9e7

+ 3 - 1
Userland/Libraries/LibJS/Runtime/IndexedProperties.cpp

@@ -14,7 +14,8 @@ constexpr const size_t SPARSE_ARRAY_HOLE_THRESHOLD = 200;
 constexpr const size_t LENGTH_SETTER_GENERIC_STORAGE_THRESHOLD = 4 * MiB;
 
 SimpleIndexedPropertyStorage::SimpleIndexedPropertyStorage(Vector<Value>&& initial_values)
-    : m_array_size(initial_values.size())
+    : IndexedPropertyStorage(IsSimpleStorage::Yes)
+    , m_array_size(initial_values.size())
     , m_packed_elements(move(initial_values))
 {
 }
@@ -83,6 +84,7 @@ bool SimpleIndexedPropertyStorage::set_array_like_size(size_t new_size)
 }
 
 GenericIndexedPropertyStorage::GenericIndexedPropertyStorage(SimpleIndexedPropertyStorage&& storage)
+    : IndexedPropertyStorage(IsSimpleStorage::No)
 {
     m_array_size = storage.array_like_size();
     for (size_t i = 0; i < storage.m_packed_elements.size(); ++i) {

+ 17 - 4
Userland/Libraries/LibJS/Runtime/IndexedProperties.h

@@ -27,6 +27,11 @@ class IndexedPropertyStorage {
 public:
     virtual ~IndexedPropertyStorage() = default;
 
+    enum class IsSimpleStorage {
+        No,
+        Yes,
+    };
+
     virtual bool has_index(u32 index) const = 0;
     virtual Optional<ValueAndAttributes> get(u32 index) const = 0;
     virtual void put(u32 index, Value value, PropertyAttributes attributes = default_attributes) = 0;
@@ -39,12 +44,20 @@ public:
     virtual size_t array_like_size() const = 0;
     virtual bool set_array_like_size(size_t new_size) = 0;
 
-    virtual bool is_simple_storage() const { return false; }
+    bool is_simple_storage() const { return m_is_simple_storage; }
+
+protected:
+    explicit IndexedPropertyStorage(IsSimpleStorage is_simple_storage)
+        : m_is_simple_storage(is_simple_storage == IsSimpleStorage::Yes) {};
+
+private:
+    bool m_is_simple_storage { false };
 };
 
 class SimpleIndexedPropertyStorage final : public IndexedPropertyStorage {
 public:
-    SimpleIndexedPropertyStorage() = default;
+    SimpleIndexedPropertyStorage()
+        : IndexedPropertyStorage(IsSimpleStorage::Yes) {};
     explicit SimpleIndexedPropertyStorage(Vector<Value>&& initial_values);
 
     virtual bool has_index(u32 index) const override;
@@ -59,7 +72,6 @@ public:
     virtual size_t array_like_size() const override { return m_array_size; }
     virtual bool set_array_like_size(size_t new_size) override;
 
-    virtual bool is_simple_storage() const override { return true; }
     Vector<Value> const& elements() const { return m_packed_elements; }
 
 private:
@@ -74,7 +86,8 @@ private:
 class GenericIndexedPropertyStorage final : public IndexedPropertyStorage {
 public:
     explicit GenericIndexedPropertyStorage(SimpleIndexedPropertyStorage&&);
-    explicit GenericIndexedPropertyStorage() = default;
+    explicit GenericIndexedPropertyStorage()
+        : IndexedPropertyStorage(IsSimpleStorage::No) {};
 
     virtual bool has_index(u32 index) const override;
     virtual Optional<ValueAndAttributes> get(u32 index) const override;