1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 05:27:14 +09:00

LibJS: Maintain number of empty elements in SimpleIndexedPropertyStorage

This will be used in upcoming changes to do a fast path when array does
not have any holes.
This commit is contained in:
Aliaksandr Kalenik 2025-06-02 21:35:27 +02:00 committed by Alexander Kalenik
parent 4c54a28c45
commit 3f7c4dd5f6
Notes: github-actions[bot] 2025-06-03 15:17:13 +00:00
2 changed files with 32 additions and 0 deletions

View file

@ -50,18 +50,29 @@ void SimpleIndexedPropertyStorage::put(u32 index, Value value, PropertyAttribute
if (index >= m_array_size) {
m_array_size = index + 1;
grow_storage_if_needed();
} else {
if (m_packed_elements[index].is_special_empty_value()) {
--m_number_of_empty_elements;
}
}
m_packed_elements[index] = value;
if (value.is_special_empty_value()) {
++m_number_of_empty_elements;
}
}
void SimpleIndexedPropertyStorage::remove(u32 index)
{
VERIFY(index < m_array_size);
++m_number_of_empty_elements;
m_packed_elements[index] = js_special_empty_value();
}
ValueAndAttributes SimpleIndexedPropertyStorage::take_first()
{
if (m_packed_elements.first().is_special_empty_value()) {
--m_number_of_empty_elements;
}
m_array_size--;
return { m_packed_elements.take_first(), default_attributes };
}
@ -70,14 +81,32 @@ ValueAndAttributes SimpleIndexedPropertyStorage::take_last()
{
m_array_size--;
auto last_element = m_packed_elements[m_array_size];
if (last_element.is_special_empty_value()) {
--m_number_of_empty_elements;
}
m_packed_elements[m_array_size] = js_special_empty_value();
return { last_element, default_attributes };
}
bool SimpleIndexedPropertyStorage::set_array_like_size(size_t new_size)
{
if (new_size == m_array_size)
return true;
auto old_size = m_array_size;
m_array_size = new_size;
m_packed_elements.resize_with_default_value_and_keep_capacity(new_size, js_special_empty_value());
if (old_size <= m_array_size) {
m_number_of_empty_elements += m_array_size - old_size;
} else {
m_number_of_empty_elements = 0;
for (auto& value : m_packed_elements) {
if (value.is_special_empty_value())
++m_number_of_empty_elements;
}
}
return true;
}

View file

@ -90,12 +90,15 @@ public:
return ValueAndAttributes { m_packed_elements.data()[index], default_attributes };
}
bool has_empty_elements() const { return m_number_of_empty_elements.value() > 0; }
private:
friend GenericIndexedPropertyStorage;
void grow_storage_if_needed();
size_t m_array_size { 0 };
Checked<size_t> m_number_of_empty_elements { 0 };
Vector<Value> m_packed_elements;
};