I'm not a Swift beginner, but this is my first time attempting to hack on the Swift compiler.
After reading a paper on improvements to reference counting (e.g. automatic cycle detection and coalescing of reference mutations) I became curious whether or not some of these improvements could be brought to Swift. The problem is, this paper requires a garbage collector.
My plan is to at least prototype a rudimentary mark-sweep garbage collector that runs along-side reference counting. The way I see it, I must implement the following:
- Find referenced classes.
- This means synthesizing a
reachablePointersmethod for all objects, which returns the set of pointers this object references.
- I don't expect this is too hard as its enumerating all class/struct properties and returning what they point to, but I'd be happy to know where to even start reading the compiler source.
- Add a Mark bit to the object header of all classes.
- I expect structs don't need a Mark bit.
- Somehow find the root set.
- This feels like a problem that I can solve by reading about SIL
- Implement Mark-Sweep.
- I think I could just do this in Swift, using
Unmanagedto free unmarked memory and
- Bonus that if I extend steps 1-3 to Objective-C, Swift could handle that as well.
I want to stress that this is not a Swift Evolution post or a serious improvement to Swift itself. I posted it in "Using Swift" to not clog the very important channels. This is a research project.
I would appreciate any feedback other than "don't do it". Even RTFM would be great if it links to someplace helpful. Thank you all in advance.