Skip to content

Commit

Permalink
LibJS: Eliminate some (unnecessary) Vector copies
Browse files Browse the repository at this point in the history
  • Loading branch information
alimpfard authored and awesomekling committed Sep 8, 2020
1 parent 8d9c5a8 commit 699e1fd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Libraries/LibJS/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ bool Console::counter_reset(String label)
Vector<String> ConsoleClient::get_trace() const
{
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
for (ssize_t i = call_stack.size() - 2; i >= 0; --i)
trace.append(call_stack[i].function_name);
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibJS/Runtime/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace JS {
Exception::Exception(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) {
auto function_name = call_stack[i].function_name;
if (function_name.is_empty())
Expand Down
12 changes: 6 additions & 6 deletions Libraries/LibJS/Runtime/IndexedProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Optional<ValueAndAttributes> IndexedProperties::get(Object* this_object, u32 ind
return result;
if (!result.has_value())
return {};
auto value = result.value();
auto& value = result.value();
if (value.value.is_accessor()) {
ASSERT(this_object);
auto& accessor = value.value.as_accessor();
Expand Down Expand Up @@ -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))
switch_to_generic_storage();
m_storage->insert(index, value, attributes);
m_storage->insert(index, move(value), attributes);
}

ValueAndAttributes IndexedProperties::take_first(Object* this_object)
Expand All @@ -340,7 +340,7 @@ void IndexedProperties::append_all(Object* this_object, const IndexedProperties&
switch_to_generic_storage();

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())
return;
m_storage->put(m_storage->array_like_size(), element.value, element.attributes);
Expand All @@ -357,14 +357,14 @@ void IndexedProperties::set_array_like_size(size_t new_size)
Vector<ValueAndAttributes> IndexedProperties::values_unordered() const
{
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;
for (auto& value : elements)
with_attributes.append({ value, default_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();
values.ensure_capacity(values.size() + storage.sparse_elements().size());
for (auto& entry : storage.sparse_elements())
Expand All @@ -374,7 +374,7 @@ Vector<ValueAndAttributes> IndexedProperties::values_unordered() const

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));
}

Expand Down
6 changes: 3 additions & 3 deletions Libraries/LibJS/Runtime/IndexedProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SimpleIndexedPropertyStorage final : public IndexedPropertyStorage {
virtual void set_array_like_size(size_t new_size) override;

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:
friend GenericIndexedPropertyStorage;
Expand All @@ -109,8 +109,8 @@ class GenericIndexedPropertyStorage final : public IndexedPropertyStorage {
virtual size_t array_like_size() const override { return m_array_size; }
virtual void set_array_like_size(size_t new_size) override;

Vector<ValueAndAttributes> packed_elements() const { return m_packed_elements; }
HashMap<u32, ValueAndAttributes> sparse_elements() const { return m_sparse_elements; }
const Vector<ValueAndAttributes>& packed_elements() const { return m_packed_elements; }
const HashMap<u32, ValueAndAttributes>& sparse_elements() const { return m_sparse_elements; }

private:
size_t m_array_size { 0 };
Expand Down

0 comments on commit 699e1fd

Please sign in to comment.