samiam@moorecad.com wrote:
Hence the reference count stuffs. I did a paper analysis once, I decided that:
- To do true automated storage recycling, you have to track each
pointer leaving scope, which means that you have to know where every pointer is.
- Be ready to reference count each pointer, that is, each copy
operation increments the reference count, and each dynamic entry gets a reference counter that is initially one for the pointer that is returned.
Decrement the reference counter each time any pointer leaves scope.
produce an error if the programmer attempts to free a non-zero
referred pointer.
- The whole thing generates headaches.
Seriously, it is quite doable with Pascal, but generates a lot of overhead. Each data structure needs a template for the contained pointers, and every data structure leaving scope or freed must be analysed for contained pointers, and those pointers decrementing reference counts.
This seems correct, and in principle doable. (One additional well-known problem with reference counting are cyclic data structures, so you need additional logic to detect and dispose of them when unused.)
But it's all a lot of work, and some runtime overhead, also memory overhead, and incompatible with low-level stuff (including manual memory allocation which might be brushed off as "dirty, don't do it", but also interfacing with foreign-language libraries which is quite a common thing).
Wirth also described this scheme, although I don't think he ever tried it. I have thought about implementing it primarily to shut people up, i.e., lack of automated storage collection "is a problem with old languages like Pascal". It isn't, the price paid for reclaimation overhead is the same as Java or other languages,
I'm not sure it's actually the same. I think there are scenarios where reference counting is more efficient, and others where garbage collection (i.e., regular cleanup, by starting from known live structures) is more efficient. Of course, the latter can also be done in Pascal, but it also a lot of work. It's partly the same as for reference counting (in both cases, you need to find pointers in structures), but partly quite different (finding live bindings, and the actual garbage collection).
we are primarily burdened by users who expect true compiled performance code.
For some of my programs (not most, though), I'm one of those burdensome users. :-) So if such a thing is ever implemented (note that I have no plans to do so), I think it should be optional.
Frank