From 6c4b823cefba78443134e90a4fadfc02ae5c9142 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 8 Dec 2020 13:33:01 +0100 Subject: [PATCH] LibJS: Make marking object indexed properties less allocation-heavy We were building up a vector with all the values in an object's indexed property storage, and then iterating over the vector to mark values. Instead of this, simply iterate over the property storage directly. :^) --- Libraries/LibJS/Runtime/IndexedProperties.h | 14 ++++++++++++++ Libraries/LibJS/Runtime/Object.cpp | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Libraries/LibJS/Runtime/IndexedProperties.h b/Libraries/LibJS/Runtime/IndexedProperties.h index cba20878d5..3509e81689 100644 --- a/Libraries/LibJS/Runtime/IndexedProperties.h +++ b/Libraries/LibJS/Runtime/IndexedProperties.h @@ -169,6 +169,20 @@ public: Vector values_unordered() const; + template + void for_each_value(Callback callback) + { + if (m_storage->is_simple_storage()) { + for (auto& value : static_cast(*m_storage).elements()) + callback(value); + } else { + for (auto& element : static_cast(*m_storage).packed_elements()) + callback(element.value); + for (auto& element : static_cast(*m_storage).sparse_elements()) + callback(element.value.value); + } + } + private: void switch_to_generic_storage(); diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index 012ea4a0c2..d5e9976fb0 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -818,8 +818,9 @@ void Object::visit_edges(Cell::Visitor& visitor) for (auto& value : m_storage) visitor.visit(value); - for (auto& value : m_indexed_properties.values_unordered()) - visitor.visit(value.value); + m_indexed_properties.for_each_value([&visitor](auto& value) { + visitor.visit(value); + }); } bool Object::has_property(const PropertyName& property_name) const