Incremental garbage collection

Elronnd elronnd at elronnd.net
Fri Jan 21 11:12:01 UTC 2022


On Friday, 21 January 2022 at 10:42:58 UTC, IGotD- wrote:
>> Again, this can be solved conservatively at the 
>> implementation-level.
> Basically no.
> Managed pointers enables you to swap out the entire default
> GC all together including GC usage in the standard library.

I guess I was insufficiently clear.  My bad.  I am talking about 
a mechanism that works without needing to modify any existing 
code.  Let's say we have this:

void f(int** x) {
	*x = new int;
}
void g() {
	int** x = new int*;
	f(x);
	int* y;
	f(&y);
}

and we want to add a generational gc, which barriers.  So naively 
we rewrite f as follows:

void f(int** x) {
	store_barrier(x, new int);
}

This will involve overhead because we don't know if x is a gc 
pointer or not.  So instead, generate multiple definitions and 
rewrite callers:

void f_gc(int** x) {
	store_barrier(x, new int);
}
void f_nogc(int** x) {
	*x = new int; //no barrier!
}
void g() {
	int** x = new int*;
	f_gc(x);
	int* y;
	f_nogc(&y); //no overhead from passing stack object!
}

Of course this transformation is conservative: you will sometimes 
call f_gc with a non-gc pointer.  But I bet it works 99% of the 
time such that the runtime overhead is negligible in practice.


More information about the Digitalmars-d mailing list