mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:57:35 +00:00
LibJS: Notify WeakSets when heap cells are sweeped
This is an implementation of the following optional optimization: https://tc39.es/ecma262/#sec-weakref-execution
This commit is contained in:
parent
fb63aeae4d
commit
a00d154522
7 changed files with 62 additions and 3 deletions
|
@ -16,6 +16,7 @@
|
|||
#include <LibJS/Heap/HeapBlock.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
#include <LibJS/Runtime/WeakSet.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
namespace JS {
|
||||
|
@ -182,18 +183,22 @@ void Heap::sweep_dead_cells(bool print_report, const Core::ElapsedTimer& measure
|
|||
dbgln_if(HEAP_DEBUG, "sweep_dead_cells:");
|
||||
Vector<HeapBlock*, 32> empty_blocks;
|
||||
Vector<HeapBlock*, 32> full_blocks_that_became_usable;
|
||||
Vector<Cell*> sweeped_cells;
|
||||
|
||||
size_t collected_cells = 0;
|
||||
size_t live_cells = 0;
|
||||
size_t collected_cell_bytes = 0;
|
||||
size_t live_cell_bytes = 0;
|
||||
|
||||
auto should_store_sweeped_cells = !m_weak_sets.is_empty();
|
||||
for_each_block([&](auto& block) {
|
||||
bool block_has_live_cells = false;
|
||||
bool block_was_full = block.is_full();
|
||||
block.template for_each_cell_in_state<Cell::State::Live>([&](Cell* cell) {
|
||||
if (!cell->is_marked()) {
|
||||
dbgln_if(HEAP_DEBUG, " ~ {}", cell);
|
||||
if (should_store_sweeped_cells)
|
||||
sweeped_cells.append(cell);
|
||||
block.deallocate(cell);
|
||||
++collected_cells;
|
||||
collected_cell_bytes += block.cell_size();
|
||||
|
@ -221,6 +226,9 @@ void Heap::sweep_dead_cells(bool print_report, const Core::ElapsedTimer& measure
|
|||
allocator_for_size(block->cell_size()).block_did_become_usable({}, *block);
|
||||
}
|
||||
|
||||
for (auto* weak_set : m_weak_sets)
|
||||
weak_set->remove_sweeped_cells({}, sweeped_cells);
|
||||
|
||||
if constexpr (HEAP_DEBUG) {
|
||||
for_each_block([&](auto& block) {
|
||||
dbgln(" > Live HeapBlock @ {}: cell_size={}", &block, block.cell_size());
|
||||
|
@ -272,6 +280,18 @@ void Heap::did_destroy_marked_value_list(Badge<MarkedValueList>, MarkedValueList
|
|||
m_marked_value_lists.remove(&list);
|
||||
}
|
||||
|
||||
void Heap::did_create_weak_set(Badge<WeakSet>, WeakSet& set)
|
||||
{
|
||||
VERIFY(!m_weak_sets.contains(&set));
|
||||
m_weak_sets.set(&set);
|
||||
}
|
||||
|
||||
void Heap::did_destroy_weak_set(Badge<WeakSet>, WeakSet& set)
|
||||
{
|
||||
VERIFY(m_weak_sets.contains(&set));
|
||||
m_weak_sets.remove(&set);
|
||||
}
|
||||
|
||||
void Heap::defer_gc(Badge<DeferGC>)
|
||||
{
|
||||
++m_gc_deferrals;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue