doing tricks with refcounts

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Sep 26 05:07:27 EDT 2001


"Terry Reedy" <tjreedy at home.com> wrote in 
news:st4s7.51289$5A3.17400301 at news1.rdc2.pa.home.com:
> "Richard B. Kreckel" <Richard.Kreckel at GiNaC.DE> wrote in message
> news:9onqf2$d6h$1 at bambi.zdv.Uni-Mainz.DE...
>> If none of b1's and b2's reference-count is equal to 1, we can imagine
>> a more general scheme where the lower reference-count is further
>> decreased in favor of the higher reference-count.
>>
>> It is obvious, that this will save some memory.  It may also save
>> computing time.  The reason why it may save time is this: If f(a1,a2) 
>> is a comparison function, a repeated call may not have to do a deep
>> compare.  Instead, it just has to compare pointers.  How much is
>> actually saved is hard to determine.  It depends on how frequent
>> equivalent Bs are and how often f(a1,a2) is called.  Both parameters
>> might be quite hard to determine. 
> 
> All of which is why Python does not multiply objects except as
> requested.

The question that the original poster has to answer is what the probability 
is that two complex objects, created independently end up with identical 
state. If the objects aren't complex (or large) then you haven't gained 
much, if they aren't created independently then they should already refer 
to the same object. I would think that in most situations you would get 
almost all of the benefit by simply applying Python's rule of requiring 
copy to be explicit.

Another assumption that is being made here is that the b objects are 
effectively immutable (if either can change in the future then you cannot 
do this optimisation).

If I had a situation where all of these conditions appeared to hold, then I 
would probably try to ensure that I either avoided creating the duplicate 
objects in the first place, or as soon as they were created collapsed them 
together. In the Python universe I would implement this with a dictionary 
using the objects or other suitable value as keys, so that whenever a new 
object is to be created it is easily possible to check whether a matching 
one already exists.

Recent versions of Python support weak references, so it is easy to have a 
dictionary that holds all of the 'b' objects, but doesn't influence their 
reference counts (and hence lifetime).

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list