ladybird/Userland/Libraries/LibJS/Runtime/WeakMap.cpp
Andreas Kling 83bd675477 LibJS: Make WeakContainer pruning do less work
Instead of iterating *all* swept cells when pruning weak containers,
only iterate the cells actually *in* the container.

Also, instead of compiling a list of all swept cells, we can simply
check the Cell::state() flag to know if something should be pruned.
2021-10-05 18:52:00 +02:00

45 lines
913 B
C++

/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/WeakMap.h>
namespace JS {
WeakMap* WeakMap::create(GlobalObject& global_object)
{
return global_object.heap().allocate<WeakMap>(global_object, *global_object.weak_map_prototype());
}
WeakMap::WeakMap(Object& prototype)
: Object(prototype)
, WeakContainer(heap())
{
}
WeakMap::~WeakMap()
{
}
void WeakMap::remove_dead_cells(Badge<Heap>)
{
// FIXME: Do this in a single pass.
Vector<Cell*> to_remove;
for (auto& it : m_values) {
if (it.key->state() != Cell::State::Live)
to_remove.append(it.key);
}
for (auto* cell : to_remove)
m_values.remove(cell);
}
void WeakMap::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
for (auto& entry : m_values)
visitor.visit(entry.value);
}
}