[Zope-dev] SVN: zope.interface/branches/jinty-mem/src/zope/interface/interface.py Improve CPU performance of previous memory optimization
Laurence Rowe
l at lrowe.co.uk
Tue Nov 9 14:47:56 EST 2010
On 9 November 2010 18:35, Tres Seaver <tseaver at palladion.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 11/09/2010 08:26 AM, Wichert Akkerman wrote:
>> On 11/9/10 14:22 , Brian Sutherland wrote:
>>> Log message for revision 118295:
>>> Improve CPU performance of previous memory optimization
>>>
>>> Changed:
>>> U zope.interface/branches/jinty-mem/src/zope/interface/interface.py
>>>
>>> -=-
>>> Modified: zope.interface/branches/jinty-mem/src/zope/interface/interface.py
>>> ===================================================================
>>> --- zope.interface/branches/jinty-mem/src/zope/interface/interface.py 2010-11-09 08:31:37 UTC (rev 118294)
>>> +++ zope.interface/branches/jinty-mem/src/zope/interface/interface.py 2010-11-09 13:22:27 UTC (rev 118295)
>>> @@ -51,6 +51,7 @@
>>> # infrastructure in place.
>>> #
>>> #implements(IElement)
>>> + __tagged_values = None
>>>
>>> def __init__(self, __name__, __doc__=''):
>>> """Create an 'attribute' description
>>> @@ -72,22 +73,27 @@
>>>
>>> def getTaggedValue(self, tag):
>>> """ Returns the value associated with 'tag'. """
>>> - return getattr(self, '_Element__tagged_values', {})[tag]
>>> + if self.__tagged_values is None:
>>> + return default
>>> + return self.__tagged_values[tag]
>>
>> You can even optimise this further:
>>
>> tv = self.__tagged_values
>> if tv is None:
>> return default
>> return tv[tv]
>>
>> that avoids a second attribute lookup. You may also want to benchmark
>> that versus using a __tagged_values={} on the class and doing a simple
>> return self.__tagged_values.get(tag, default_
>
> - -1: mutable class defaults are a bug magnet.
None is immutable so I don't think that is a problem in this case.
I think the is a possible threading issue with Element.setTaggedValue
and Specification.subscribe - if two threads called the method
concurrently, then one of the values might be lost. I think the
correct way to do it would be:
tv = self.__tagged_values
if tv is None:
tv = self.__dict__.setdefault('_Element__tagged_values', {})
tv[tag] = value
This does bring the name mangling back though.
Laurence
More information about the Zope-Dev
mailing list