copy-on-write for dict objects?
Just
just at xs4all.nl
Wed Jan 15 15:34:52 EST 2003
In article <7h3n0m2p77m.fsf at pc150.maths.bris.ac.uk>,
Michael Hudson <mwh at python.net> wrote:
> Matthias Oberlaender <matthias.oberlaender at REMOVE.daimlerchrysler.com> writes:
>
> > Is there an easy way in Python 2.2.1 to add a copy-on-write mechanism to
> > subclasses of dict, i.e. without extending/modifying the C-code
> > implementation? (I guess there is none!)
>
> Not sure what you mean. But I doubt it. You need a level of
> indirection in there, so I don't see how you could do it directly even
> hacking C.
>
> > I don't want to resort to UserDict again, since, at least for my
> > purposes, it's much slower than direct subclasses of dict (around
> > 10x)
>
> I know that feeling.
This may help somewhat, although it will still be slower than a true
dict (and is fairly inefficient for small dicts):
class CopyOnWriteDict:
def __init__(self, d):
self.__d = d
self.__shared = 1
def __getattr__(self, name):
# delegate attr lookups to the underlying dict, cache results
# so __getattr__ won't get called again for this attr name
attr = getattr(self.__d, name)
setattr(self, name, attr)
return attr
def __copy(self):
d = self.__d.copy()
self.__dict__.clear()
self.__d = d
self.__shared = 0
def __setitem__(self, key, value):
if self.__shared:
self.__copy()
self.__d.__setitem__(key, value)
def __delitem__(self, key):
if self.__shared:
self.__copy()
self.__d.__delitem__(key)
def update(self, d):
if self.__shared:
self.__copy()
self.__d.update(d)
# other "writing" methods are left as an exercise..
This approach might actually yield performance close to a real dict when
implemented in C, as it could use the fast tp_as_mapping methods.
Just
More information about the Python-list
mailing list