1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:37:37 +00:00

LibJS: Eliminate some (unnecessary) Vector copies

This commit is contained in:
AnotherTest 2020-09-08 13:17:37 +04:30 committed by Andreas Kling
parent 8d9c5a8e70
commit 699e1fdc07
4 changed files with 11 additions and 11 deletions

View file

@ -123,7 +123,7 @@ bool Console::counter_reset(String label)
Vector<String> ConsoleClient::get_trace() const Vector<String> ConsoleClient::get_trace() const
{ {
Vector<String> trace; Vector<String> trace;
auto call_stack = m_console.interpreter().call_stack(); auto& call_stack = m_console.interpreter().call_stack();
// -2 to skip the console.trace() call frame // -2 to skip the console.trace() call frame
for (ssize_t i = call_stack.size() - 2; i >= 0; --i) for (ssize_t i = call_stack.size() - 2; i >= 0; --i)
trace.append(call_stack[i].function_name); trace.append(call_stack[i].function_name);

View file

@ -32,7 +32,7 @@ namespace JS {
Exception::Exception(Value value) Exception::Exception(Value value)
: m_value(value) : m_value(value)
{ {
auto call_stack = interpreter().call_stack(); auto& call_stack = interpreter().call_stack();
for (ssize_t i = call_stack.size() - 1; i >= 0; --i) { for (ssize_t i = call_stack.size() - 1; i >= 0; --i) {
auto function_name = call_stack[i].function_name; auto function_name = call_stack[i].function_name;
if (function_name.is_empty()) if (function_name.is_empty())

View file

@ -273,7 +273,7 @@ Optional<ValueAndAttributes> IndexedProperties::get(Object* this_object, u32 ind
return result; return result;
if (!result.has_value()) if (!result.has_value())
return {}; return {};
auto value = result.value(); auto& value = result.value();
if (value.value.is_accessor()) { if (value.value.is_accessor()) {
ASSERT(this_object); ASSERT(this_object);
auto& accessor = value.value.as_accessor(); auto& accessor = value.value.as_accessor();
@ -315,7 +315,7 @@ void IndexedProperties::insert(u32 index, Value value, PropertyAttributes attrib
{ {
if (m_storage->is_simple_storage() && (index >= SPARSE_ARRAY_THRESHOLD || attributes != default_attributes || array_like_size() == SPARSE_ARRAY_THRESHOLD)) if (m_storage->is_simple_storage() && (index >= SPARSE_ARRAY_THRESHOLD || attributes != default_attributes || array_like_size() == SPARSE_ARRAY_THRESHOLD))
switch_to_generic_storage(); switch_to_generic_storage();
m_storage->insert(index, value, attributes); m_storage->insert(index, move(value), attributes);
} }
ValueAndAttributes IndexedProperties::take_first(Object* this_object) ValueAndAttributes IndexedProperties::take_first(Object* this_object)
@ -340,7 +340,7 @@ void IndexedProperties::append_all(Object* this_object, const IndexedProperties&
switch_to_generic_storage(); switch_to_generic_storage();
for (auto it = properties.begin(false); it != properties.end(); ++it) { for (auto it = properties.begin(false); it != properties.end(); ++it) {
auto element = it.value_and_attributes(this_object, evaluate_accessors); const auto& element = it.value_and_attributes(this_object, evaluate_accessors);
if (this_object && this_object->interpreter().exception()) if (this_object && this_object->interpreter().exception())
return; return;
m_storage->put(m_storage->array_like_size(), element.value, element.attributes); m_storage->put(m_storage->array_like_size(), element.value, element.attributes);
@ -357,14 +357,14 @@ void IndexedProperties::set_array_like_size(size_t new_size)
Vector<ValueAndAttributes> IndexedProperties::values_unordered() const Vector<ValueAndAttributes> IndexedProperties::values_unordered() const
{ {
if (m_storage->is_simple_storage()) { if (m_storage->is_simple_storage()) {
auto elements = static_cast<const SimpleIndexedPropertyStorage&>(*m_storage).elements(); const auto& elements = static_cast<const SimpleIndexedPropertyStorage&>(*m_storage).elements();
Vector<ValueAndAttributes> with_attributes; Vector<ValueAndAttributes> with_attributes;
for (auto& value : elements) for (auto& value : elements)
with_attributes.append({ value, default_attributes }); with_attributes.append({ value, default_attributes });
return with_attributes; return with_attributes;
} }
auto storage = static_cast<const GenericIndexedPropertyStorage&>(*m_storage); auto& storage = static_cast<const GenericIndexedPropertyStorage&>(*m_storage);
auto values = storage.packed_elements(); auto values = storage.packed_elements();
values.ensure_capacity(values.size() + storage.sparse_elements().size()); values.ensure_capacity(values.size() + storage.sparse_elements().size());
for (auto& entry : storage.sparse_elements()) for (auto& entry : storage.sparse_elements())
@ -374,7 +374,7 @@ Vector<ValueAndAttributes> IndexedProperties::values_unordered() const
void IndexedProperties::switch_to_generic_storage() void IndexedProperties::switch_to_generic_storage()
{ {
auto storage = static_cast<const SimpleIndexedPropertyStorage&>(*m_storage); auto& storage = static_cast<SimpleIndexedPropertyStorage&>(*m_storage);
m_storage = make<GenericIndexedPropertyStorage>(move(storage)); m_storage = make<GenericIndexedPropertyStorage>(move(storage));
} }

View file

@ -83,7 +83,7 @@ public:
virtual void set_array_like_size(size_t new_size) override; virtual void set_array_like_size(size_t new_size) override;
virtual bool is_simple_storage() const override { return true; } virtual bool is_simple_storage() const override { return true; }
Vector<Value> elements() const { return m_packed_elements; } const Vector<Value>& elements() const { return m_packed_elements; }
private: private:
friend GenericIndexedPropertyStorage; friend GenericIndexedPropertyStorage;
@ -109,8 +109,8 @@ public:
virtual size_t array_like_size() const override { return m_array_size; } virtual size_t array_like_size() const override { return m_array_size; }
virtual void set_array_like_size(size_t new_size) override; virtual void set_array_like_size(size_t new_size) override;
Vector<ValueAndAttributes> packed_elements() const { return m_packed_elements; } const Vector<ValueAndAttributes>& packed_elements() const { return m_packed_elements; }
HashMap<u32, ValueAndAttributes> sparse_elements() const { return m_sparse_elements; } const HashMap<u32, ValueAndAttributes>& sparse_elements() const { return m_sparse_elements; }
private: private:
size_t m_array_size { 0 }; size_t m_array_size { 0 };