ladybird/Libraries/LibJS/Runtime/IndexedProperties.cpp
Andreas Kling 614713ed08 LibJS: Replace IndexedProperties with inline Packed/Holey/Dictionary
Replace the OwnPtr<IndexedPropertyStorage> indirection with inline
indexed element storage directly on Object. This eliminates virtual
dispatch and reduces indirection for indexed property access.

The new system uses three storage kinds tracked by IndexedStorageKind:

- Packed: Dense array, no holes. Elements stored in a malloced Value*
  array with capacity header (same layout as named properties).
- Holey: Dense array with possible holes marked by empty sentinel.
  Same physical layout as Packed.
- Dictionary: Sparse storage using GenericIndexedPropertyStorage,
  type-punned into the m_indexed_elements pointer.

Transitions: None->Packed->Holey->Dictionary (mostly monotonic).
Dictionary mode triggers on non-default attributes or sparse arrays.

Object keeps the same 48-byte size since m_indexed_elements (8 bytes)
replaces IndexedProperties (8 bytes), and the storage kind + array
size fit in existing padding alongside m_flags.

The asm interpreter benefits from one fewer indirection: it now reads
the element pointer and array size directly from Object fields instead
of chasing through OwnPtr -> IndexedPropertyStorage -> Vector.

Removes: IndexedProperties, SimpleIndexedPropertyStorage,
IndexedPropertyStorage, IndexedPropertyIterator.
Keeps: GenericIndexedPropertyStorage (for Dictionary mode).
2026-03-17 22:28:35 -05:00

98 lines
2.3 KiB
C++

/*
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/QuickSort.h>
#include <LibJS/Runtime/Accessor.h>
#include <LibJS/Runtime/IndexedProperties.h>
namespace JS {
bool GenericIndexedPropertyStorage::has_index(u32 index) const
{
return m_sparse_elements.contains(index);
}
Optional<ValueAndAttributes> GenericIndexedPropertyStorage::get(u32 index) const
{
if (index >= m_array_size)
return {};
return m_sparse_elements.get(index).copy();
}
void GenericIndexedPropertyStorage::put(u32 index, Value value, PropertyAttributes attributes)
{
if (index >= m_array_size)
m_array_size = index + 1;
m_sparse_elements.set(index, { value, attributes });
}
void GenericIndexedPropertyStorage::remove(u32 index)
{
VERIFY(index < m_array_size);
m_sparse_elements.remove(index);
}
ValueAndAttributes GenericIndexedPropertyStorage::take_first()
{
VERIFY(m_array_size > 0);
m_array_size--;
auto indices = m_sparse_elements.keys();
quick_sort(indices);
auto it = m_sparse_elements.find(indices.first());
auto first_element = it->value;
m_sparse_elements.remove(it);
return first_element;
}
ValueAndAttributes GenericIndexedPropertyStorage::take_last()
{
VERIFY(m_array_size > 0);
m_array_size--;
auto result = m_sparse_elements.get(m_array_size);
if (!result.has_value())
return {};
m_sparse_elements.remove(m_array_size);
return result.value();
}
bool GenericIndexedPropertyStorage::set_array_like_size(size_t new_size)
{
if (new_size == m_array_size)
return true;
if (new_size >= m_array_size) {
m_array_size = new_size;
return true;
}
bool any_failed = false;
size_t highest_index = 0;
HashMap<u32, ValueAndAttributes> new_sparse_elements;
for (auto& entry : m_sparse_elements) {
if (entry.key >= new_size) {
if (entry.value.attributes.is_configurable())
continue;
else
any_failed = true;
}
new_sparse_elements.set(entry.key, entry.value);
highest_index = max(highest_index, entry.key);
}
if (any_failed)
m_array_size = highest_index + 1;
else
m_array_size = new_size;
m_sparse_elements = move(new_sparse_elements);
return !any_failed;
}
}