[fpc-devel] Improving Ref Counting

Jamie McCracken jamie-junk at blueyonder.co.uk
Sun Feb 27 12:35:22 CET 2005


Hi,

Rather than continuing the GC stuff which seems fruitless I thought it 
might be better to improve what we have with ref counting (whilst taking 
a leaf out of the GC book as well).

I note that ref counting is very fast without all the implicit 
try..finally stuff so I was wondering if it was possible to implement it 
without them (whilst still being able to free them even if an exception 
occurs).

My proposal is to do the following (its a hybrid ref count/GC):

1) Remove all implicit try..finally in ref counting.
2) Have a single linked list that contains references to all managed 
objects (ansi strings, variants etc)
3) Each managed object also has a single linked list that contains 
addresses of all pointers that reference it.
4) inc ref count means adding the pointer's address to that list
5) dec ref count means removing the pointer's address from that list. 
When that list is empty the object is freed. (a check for eliminating 
circular refs should also be done if applicable)
6) Whenever an exception is thrown, wait until its either handled or 
fully propagated and then perform some garbage collection. (traverse the 
single linked list of all managed objects and for each object check 
whether anything that references it is still valid and delete if 
appropriate).


Im not sure if there are any corner cases where the above would not work 
but it does have the advantage of being very fast and very efficient 
memory wise. It also avoids the problem of extended lifetimes that you 
get with GCs. The only performance hit you will get is when an exception 
occurs but considering they only occur in exceptional circumstances and 
that exceptions are slow anyhow I dont think thats any loss. If no 
exceptions are thrown then no GC needs to be performed ever. Can we get 
away with doing this?

jamie.




More information about the fpc-devel mailing list