munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.99k stars 1.05k forks source link

Changing the GC to a reference counter #954

Open mcfriend99 opened 3 years ago

mcfriend99 commented 3 years ago

Hi,

I was looking at the possibility of swapping the GC out for a reference counter. Whilst implementing the reference counter itself is not an issue, I am stuck as to where to increment and decrement reference counts in the system.

@munificent and hopefully others, I believe this will increase performance a lot and is ripe for discussion here.

Anybody who has successfully done it or know exactly where I need to do this should please help out.

Thanks all in advance.

nocturn9x commented 3 years ago

While a refc GC would technically be faster, it would require another GC to detect cyclic references (which a refc GC doesn't detect), like for example Python does (using a refc GC and a generational GC for cyclic references)

mcfriend99 commented 3 years ago

I am fully aware of the need to handle cyclic references... What I need to know is where to plugin reference counting into the current codebase and use it as the default GC. That way, I can make the mark-sweep GC run less frequently (like very much less frequently).

munificent commented 3 years ago

What I need to know is where to plugin reference counting into the current codebase and use it as the default GC.

I don't know if there's a short answer to that. Basically, any place a reference to an object is written potentially needs to hook into ref counting. Off the top of my head, that's:

There are probably others I'm missing. :)