Eliminate __dict__ from persistent btree, bnode,
dict, list and set
Patrick K. O'Brien
2006-07-06
David Binger wrote:
>
> On Jul 5, 2006, at 7:59 PM, Patrick K. O'Brien wrote:
>
>>
>> I have one idea. Instead of PersistentDict.data being a dict, it could
>> be a subclass of dict. Then __setstate__ could test the incoming state
>> to see if it is a regular dict or our special subclass. But that would
>> trade off physical disk savings against type inspection within
>> __setstate__, which gets called a lot. I'm not sure that would be
>> worthwhile. The alternative would be a one-time conversion routine.
>> Unless you think of something better.
>
> It does not help PersistentSet or PersistentList as your idea does,
> but it might be possible to make PersistentDict use the actual __dict__
> value as the dict instead of wrapping another dict instance.
>
> I think I remember trying to make PersistentDict be a dict as you
> suggest here, and I felt like it was getting difficult to understand.
I'm not sure we're talking about the exact same thing. My idea was
something like this:
class special_dict(dict):
pass
class PersistentDict(Persistent):
__slots__ = []
def __init__(self, *args, **kwargs):
self.data = special_dict(*args, **kwargs)
def __getstate__(self):
return self.data
def __setstate__(self, state):
if isinstance(state, special_dict):
self.data = state
else:
self.data = state['data']
However, now that I've written it out it occurs to me that the module
and class name of special_dict would get pickled, which would likely
negate any physical file space savings to be expected.
Now, you suggest that maybe the following might make sense:
class PersistentDict(dict):
I did something very similar for our odict (ordered dictionary)
implementation, which is used quite a bit in Schevo and has a single
slot defined on it. Take a look at the source code for that:
http://schevo.org/trac/browser/trunk/Schevo/schevo/lib/odict.py
In the case of PersistentDict, I'm not sure there is any advantage to
inheriting from dict as opposed to the current implementation. In the
case of odict I wanted instances of it to return True for:
isinstance(some_odict, dict)
However, I'm not sure the same holds true for PersistentDict.
--
Patrick K. O'Brien
Orbtech http://www.orbtech.com
Schevo http://www.schevo.org
Louie http://www.pylouie.org