This is an automated archive made by the Lemmit Bot.

The original was posted on /r/programminglanguages by /u/Western-Cod-3486 on 2024-11-08 18:57:53+00:00.


Basically title, but I am over hyped of the progress I’ve made during the last few days, despite missing my mark.

I was considering to not build a GC for my scripting language, but during compilation to add free equivalent code where appropriate in order to ensure objects, arrays, strings, etc get dropped when not needed, but as good as that sounded (at least in my head) I realised that it will be quite a huge task, given that I will have to readjust all jumps (loops, statements, function offsets) and was postponing it until I have the mental capacity to figure it all out.

Yesterday, I thought “how hard could it be” to do a simple mark & sweep, besides I am using enums for my stack and the objects are just an integers which I have to lookup, so I just have to keep track of what is referenced with a counter (I was thinking I was doing a mark and sweep btw) drop them from the map… right?

I did a benchmark script with a simple class in my language, which gets constructed and a method is called in a loop. So I made a loop to build 100k objects to measure memory and time and went to town. Lo and behold no GC it was taking about 30ms to complete, but with “GC” ~50seconds … yes… about 100x the slowdown, I was defeated as I had to understand a bit of the shenanigans of Rc, Arc, Cell RefCell and Mutex but by the middle of the night, about 3am I was defeated, managed to fix some bugs, unnecessary abstractions, but only got to about 10x slowdown… I was… defeated…

BUT

Comes today, I am groggy and annoyed I screwed up this bad and decided to reconsider my approach and went with straight up reference counter, because using the underlying one from std did not do the job, because my objects lived as long as the program did and they were dropped but not when I wanted them, so I removed almost everything started again, fought with the borrow checker and my own stupidity at the same time and by the afternoon Ive had a working ref counter that actually slows my program just by 1x-2x and keeps the memory at bay,l. The above mentioned test initially was taking about 70k memory, but now just about 2k and is blazingly fast.

So yeah, I feel like I’ve gained at least a couple of levels in programming, at least 10 in rust and 1000 in happiness, so yeah I am bragging guys!!!