mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:37:35 +00:00
LibJS: Delete fully-empty HeapBlocks after garbage collection
We now deallocate GC blocks when they are found to have no live cells inside them.
This commit is contained in:
parent
bc002f807a
commit
2106dafd62
2 changed files with 23 additions and 0 deletions
8
Base/home/anon/js/gc-strings.js
Normal file
8
Base/home/anon/js/gc-strings.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
function foo() {
|
||||||
|
var a = [];
|
||||||
|
for (var i = 0; i < 4000; ++i) {
|
||||||
|
a.push("string" + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foo();
|
|
@ -185,7 +185,10 @@ void Heap::sweep_dead_cells()
|
||||||
#ifdef HEAP_DEBUG
|
#ifdef HEAP_DEBUG
|
||||||
dbg() << "sweep_dead_cells:";
|
dbg() << "sweep_dead_cells:";
|
||||||
#endif
|
#endif
|
||||||
|
Vector<HeapBlock*, 32> empty_blocks;
|
||||||
|
|
||||||
for (auto& block : m_blocks) {
|
for (auto& block : m_blocks) {
|
||||||
|
bool block_has_live_cells = false;
|
||||||
block->for_each_cell([&](Cell* cell) {
|
block->for_each_cell([&](Cell* cell) {
|
||||||
if (cell->is_live()) {
|
if (cell->is_live()) {
|
||||||
if (!cell->is_marked()) {
|
if (!cell->is_marked()) {
|
||||||
|
@ -195,9 +198,21 @@ void Heap::sweep_dead_cells()
|
||||||
block->deallocate(cell);
|
block->deallocate(cell);
|
||||||
} else {
|
} else {
|
||||||
cell->set_marked(false);
|
cell->set_marked(false);
|
||||||
|
block_has_live_cells = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (!block_has_live_cells)
|
||||||
|
empty_blocks.append(block);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto* block : empty_blocks) {
|
||||||
|
dbg() << " - Reclaim HeapBlock @ " << block << ": cell_size=" << block->cell_size();
|
||||||
|
m_blocks.remove_first_matching([block](auto& entry) { return entry == block; });
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& block : m_blocks) {
|
||||||
|
dbg() << " > Live HeapBlock @ " << block << ": cell_size=" << block->cell_size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue