[lxml-dev] Weakref support?
Stefan Behnel
stefan_ml at behnel.de
Sun Jul 19 15:25:52 CEST 2009
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.
Is there a specific use case you have in mind?
Stefan
More information about the lxml-dev
mailing list