1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:27:34 +00:00

LibJS: Stop eagerly creating the backing store for IndexedProperties

The vast majority of objects do not, and are unlikely to ever need
indexed property storage. By delaying the creation of the backing
store of IndexedProperties we reduce the memory used by each object
and reduce allocation and deallocation by somewhere between 20 and
30%
This commit is contained in:
Anonymous 2022-02-11 22:46:17 -08:00 committed by Andreas Kling
parent d1cc67bbe1
commit 34a4ce7955
2 changed files with 32 additions and 4 deletions

View file

@ -72,6 +72,7 @@ private:
class GenericIndexedPropertyStorage final : public IndexedPropertyStorage {
public:
explicit GenericIndexedPropertyStorage(SimpleIndexedPropertyStorage&&);
explicit GenericIndexedPropertyStorage();
virtual bool has_index(u32 index) const override;
virtual Optional<ValueAndAttributes> get(u32 index) const override;
@ -116,11 +117,12 @@ public:
IndexedProperties() = default;
explicit IndexedProperties(Vector<Value> values)
: m_storage(make<SimpleIndexedPropertyStorage>(move(values)))
{
if (!values.is_empty())
m_storage = make<SimpleIndexedPropertyStorage>(move(values));
}
bool has_index(u32 index) const { return m_storage->has_index(index); }
bool has_index(u32 index) const { return m_storage ? m_storage->has_index(index) : false; }
Optional<ValueAndAttributes> get(u32 index) const;
void put(u32 index, Value value, PropertyAttributes attributes = default_attributes);
void remove(u32 index);
@ -131,7 +133,7 @@ public:
IndexedPropertyIterator end() const { return IndexedPropertyIterator(*this, array_like_size(), false); };
bool is_empty() const { return array_like_size() == 0; }
size_t array_like_size() const { return m_storage->array_like_size(); }
size_t array_like_size() const { return m_storage ? m_storage->array_like_size() : 0; }
bool set_array_like_size(size_t);
size_t real_size() const;
@ -141,6 +143,8 @@ public:
template<typename Callback>
void for_each_value(Callback callback)
{
if (!m_storage)
return;
if (m_storage->is_simple_storage()) {
for (auto& value : static_cast<SimpleIndexedPropertyStorage&>(*m_storage).elements())
callback(value);
@ -152,8 +156,9 @@ public:
private:
void switch_to_generic_storage();
void ensure_storage();
NonnullOwnPtr<IndexedPropertyStorage> m_storage { make<SimpleIndexedPropertyStorage>() };
OwnPtr<IndexedPropertyStorage> m_storage;
};
}