[lxml-dev] Weakref support?

Christian Zagrodnick cz at gocept.com
Mon Jul 20 08:02:47 CEST 2009


On 2009-07-19 15:25:52 +0200, Stefan Behnel <stefan_ml at behnel.de> said:

> 
> Christian Zagrodnick wrote:
>> would it be possible to add weakref support to lxml trees (objectify in
>> particular)?
> 
> Possible: yes. You can simply add a "__weakref__" attribute to the _Element
> class (or just the ObjectifiedElement class) and Cython will do the rest.
> 
> http://docs.cython.org/docs/extension_types.html#making-extension-types-weak-referenceable

However, 
> 
> making the class weak-referenceable will add another bit to the
> size of each Element instance, which you will notice when you do lots of
> tree work in Python space. I'd prefer avoiding that for a class as vital as
> _Element, that's why it isn't currently there. I also don't know if it has
> any performance implications if a class is weak-referenceable. Does anyone
> know if there is any impact on the garbage collector, for example?
> 
> At least the memory size isn't that a big issue. When you look at the
> class, you may notice that there already is some stuff in there that isn't
> 100% required (e.g. the cached tag name). Adding 4/8 bytes to it may not be
> that bad.

Of course there are a lot of elements. So being cautious is good.

> 
> Is there a specific use case you have in mind?

Actually all I'd need is a reference to the tree. I don't think 
weak-refs to all elements are necessary or useful. Well, maybe they 
are, but I don't need them :)

Anyway, if you think it is to risky, it won't kill me to go w/o weak refs. :)

Regards,
-- 
Christian Zagrodnick · cz at gocept.com
gocept gmbh & co. kg · forsterstraße 29 · 06112 halle (saale) · germany
http://gocept.com · tel +49 345 1229889 4 · fax +49 345 1229889 1
Zope and Plone consulting and development




More information about the lxml-dev mailing list