Skip to content

Commit 411f896

Browse files
committed
LibGC: Implement incremental garbage collection
Convert collector to run incrementally using tricolor marking and a hybrid of Yuasa (SATB, deletion) and Dijkstra (insertion) write barriers that shade both old and new targets gray. Marking and sweeping budgets specified in cells, and blocks respectively. Budgets are currently arbitrary but can be tuned with an ini file. This code is more than proof-of-concept but unrefined.
1 parent 8c7c46f commit 411f896

32 files changed

+1134
-113
lines changed

Libraries/LibGC/Cell.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public: \
3838
} \
3939
friend class GC::Heap;
4040

41+
enum class Color : u8 {
42+
White, // Not yet reached — will be reclaimed if still white at sweep time.
43+
Gray, // Reached but outgoing edges not yet traced (on marking worklist).
44+
Black, // Reached and fully traced.
45+
};
46+
4147
class GC_API Cell {
4248
AK_MAKE_NONCOPYABLE(Cell);
4349
AK_MAKE_NONMOVABLE(Cell);
@@ -48,8 +54,11 @@ class GC_API Cell {
4854

4955
virtual ~Cell() = default;
5056

51-
bool is_marked() const { return m_mark; }
52-
void set_marked(bool b) { m_mark = b; }
57+
bool is_marked() const { return m_color != Color::White; }
58+
void set_marked(bool b) { m_color = b ? Color::Black : Color::White; }
59+
60+
Color gc_color() const { return m_color; }
61+
void set_gc_color(Color color) { m_color = color; }
5362

5463
enum class State : bool {
5564
Live,
@@ -139,6 +148,13 @@ class GC_API Cell {
139148
visit_impl(ReadonlySpan<NanBoxedValue>(vector.span().data(), vector.size()));
140149
}
141150

151+
template<typename T>
152+
void visit(ValueVector<T> const& vector)
153+
requires(IsBaseOf<NanBoxedValue, T>)
154+
{
155+
visit_impl(ReadonlySpan<NanBoxedValue>(vector.span().data(), vector.size()));
156+
}
157+
142158
template<typename T>
143159
void visit(HashTable<T> const& table)
144160
{
@@ -215,7 +231,7 @@ class GC_API Cell {
215231
Cell() = default;
216232

217233
private:
218-
bool m_mark { false };
234+
Color m_color { Color::White };
219235
State m_state { State::Live };
220236
};
221237

Libraries/LibGC/DeferGC.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
namespace GC {
1212

13+
// Defers all garbage collection activity (both starting new cycles and
14+
// running incremental marking steps) for the lifetime of this object.
15+
// Write barriers remain active during deferral so the tri-color invariant
16+
// is preserved. When the last DeferGC is destroyed, a deferred collection
17+
// runs if one was requested.
1318
class GC_API DeferGC {
1419
public:
1520
explicit DeferGC(Heap& heap)

Libraries/LibGC/Forward.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class Function;
2727
template<typename T>
2828
class HeapHashTable;
2929

30+
template<class T>
31+
class RawPtr;
32+
33+
template<class T>
34+
class RawRef;
35+
3036
template<class T>
3137
class Root;
3238

@@ -36,6 +42,9 @@ class ConservativeVector;
3642
template<class T>
3743
class HeapVector;
3844

45+
template<class T>
46+
class ValueVector;
47+
3948
template<class T, size_t inline_capacity = 0>
4049
class RootVector;
4150

0 commit comments

Comments
 (0)