C API: Change immutable objects? :-o

Chris Liechti cliechti at gmx.net
Fri Aug 2 13:10:21 EDT 2002

Frank Sonnenburg <frank.sonnenburg at biosolveit.de> wrote in
news:aiecue$suu$06$1 at news.t-online.com: 

>>> I'm trying to speed up updating my PyObjects, e.g. PyFloat.
>>> So instead of deleting the old object and creating a new one with my
>>> new double value, i over-write the old value in the
>>> PyFloatObject-struct: 
>> You are a brave, brave man.
>> I can't think of a reason why this wouldn't work (assuming you're not
>> dumb enough to be keeping multiple references to the object and
>> expecting old ones to retain their values, their hashes, or other
>> such features). But that doesn't mean there isn't such a reason.
>> But I ought to ask... the main thing you're saving is the time to
>> allocate and deallocate the memory for the objects. Are you running a
>> version of Python compiled with pymalloc? Because I believe that
>> fairly recent changes have wrought significant gains in performance
>> when working with large numbers of small objects.
>> Anyway... good luck brave soul!
>> -- Michael Chermside
> Ok, i understand this as "keep your hands off!"
> Anyway, i'm still experimenting. At first i will use "old-fashioned"
> style. ;-)

small int numbers are cached. this means that some int objects (1..100 or 
so?) are laying around all the time. you must not alter one of those or 
you'll get troubles. i don't know if such a chache exist for floats too.
but if not, maybe you could use this technique in you extension.

in any case i would only alter immutable objects when their refcount is 1. 
otherwise problems are likely...


Chris <cliechti at gmx.net>

More information about the Python-list mailing list