1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-08 13:37:10 +09:00

LibJS: Don't incrementally delete PropertyNameIterator while iterating

We were spending a lot of time removing each property name from the
iterator's underlying HashMap while iterating over it. This wasn't
actually necessary, so let's stop doing it and instead just iterate
over the property names with a stored HashTable iterator.

1.10x speedup on MicroBench/for-in-indexed-properties.js
This commit is contained in:
Andreas Kling 2025-05-03 21:12:00 +02:00 committed by Andreas Kling
parent c8edd37741
commit edb5547e37
Notes: github-actions[bot] 2025-05-03 20:01:45 +00:00

View file

@ -1728,20 +1728,20 @@ public:
ThrowCompletionOr<void> next(VM&, bool& done, Value& value) override
{
while (true) {
if (m_properties.is_empty()) {
if (m_iterator == m_properties.end()) {
done = true;
return {};
}
auto it = *m_properties.begin();
ScopeGuard remove_first = [&] { m_properties.take_first(); };
auto const& entry = *m_iterator;
ScopeGuard remove_first = [&] { ++m_iterator; };
// If the property is deleted, don't include it (invariant no. 2)
if (!TRY(m_object->has_property(it.key.key)))
if (!TRY(m_object->has_property(entry.key.key)))
continue;
done = false;
value = it.value;
value = entry.value;
return {};
}
}
@ -1751,6 +1751,7 @@ private:
: Object(realm, nullptr)
, m_object(object)
, m_properties(move(properties))
, m_iterator(m_properties.begin())
{
}
@ -1763,6 +1764,7 @@ private:
GC::Ref<Object> m_object;
OrderedHashMap<PropertyKeyAndEnumerableFlag, Value> m_properties;
decltype(m_properties.begin()) m_iterator;
};
GC_DEFINE_ALLOCATOR(PropertyNameIterator);