Message9164

Author stefan.richthofer
Recipients stefan.richthofer
Date 2014-10-23.16:31:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1414081913.19.0.137689915055.issue2224@psf.upfronthosting.co.za>
In-reply-to
Content
There are several scenarios in Jython where one needs to attach some attribute to a given arbitrary PyObject. Typically, only few PyObjects will get this attribute attached, so adding a new field to PyObject for this attribute would not be worth the increased memory footprint of all PyObjects.
What is frequently done in Jython in such cases, is to maintain a HashMap that maps PyObjects to this attribute. Of course this shall not prevent gc from collecting the mapped PyObjects, so this is usually implemented as a weak hashMap.
However, this hashMap breaks if a PyObject is resurrected in its finalizer (Please no "Resurrection is evil anyway"-comments, I did not invent that Python allows this, but we have to deal with it!).

Examples are
- ids as returned by Py.id()
- List of WeakReferences to a PyObject (in weakref module)
- handles to native objects in JyNI
(I'm sure there are more...)

This is an inherent problem, as weak hashMaps and resurrection simply don't go together. And there is no easy work-around.

So I propose to refine this common pattern and instead add a general purpose (linked) list of attributes to PyObject.
Supported attributes get reserved id-fields, ordering them by priority and allowing a fail-fast lookup. I propose a linked list for this, because there will be only few attributes per object and typically, they will be accessed seldomly. So a linked list is a sufficient data-structure with minimal overhead.

In addition to the above mentioned examples, also FinalizeTriggers can be stored in this list, amortizing the memory of the additional field for all PyInstance objects and their new style equivalents (as the  finalizeTrigger-field becomes obsolete). If memory matters, one can also include javaProxy as always-on-top element. Then there is not even need for an additional field in PyObject as the javaProxy field can be refined to be the top attribute, which would hardly increase lookup time for this field.

Benefits:
- solves the breaking attributes problem in resurrection case
- would make weak hash maps obsolete in several places
  - saves memory and lookup-time
  - no more threads like the refReaper thread in the weakref module
- simplifies implementation of Finalizable PyObjects
- simplifies implementation wherever one needs to attach attributes to PyObjects


Note: In the attached draft-implementation, I included javaProxy as possible attribute, but this is of course not strictly necessary - lets decide this together.

Note2: I selected urgent severity since it is a major refinement to the internal API, so I recommend this to be done before 2.7.0 is released.
History
Date User Action Args
2014-10-23 16:31:53stefan.richthofersetrecipients: + stefan.richthofer
2014-10-23 16:31:53stefan.richthofersetmessageid: <1414081913.19.0.137689915055.issue2224@psf.upfronthosting.co.za>
2014-10-23 16:31:53stefan.richthoferlinkissue2224 messages
2014-10-23 16:31:52stefan.richthofercreate