copy-on-write for dict objects?

Andrew Dalke adalke at mindspring.com
Mon Jan 13 03:54:02 EST 2003


Matthias Oberlaender wrote:
> 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!)
> 
> 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 can't think of any.  However, you should be able to build on the
following, which should get you pretty good performance.  There
are some caveats, like don't use a bound method from before and after
a copy-on-write.  (Most code doesn't use this.)


 >>> class MyDict:
...     def __init__(self, d):
...             self.data = d
...             self.__getitem__ = d.__getitem__
...             self.get = d.get
...     def __setitem__(self, k, v):
...             self._on_write()
...             self.data[k] = v
...     def _on_write(self):
...             self.data = self.data.copy()
...             self.__setitem__ = self.data.__setitem__
...             self.__getitem__ = self.data.__getitem__
...             self.get = self.data.get
...     def copy(self):
...             return self.data.copy()
...
 >>> d = {1:1, 2:4, 3:9}
 >>> md = MyDict(d)
 >>> md[3]
9
 >>> d[3] = 27
 >>> md[3]
27
 >>> d[2], md[2]
(4, 4)
 >>> md[2] = 8
 >>> d[2], md[2]
(4, 8)
 >>> d.clear()
 >>> md.copy()
{1: 1, 2: 8, 3: 27}
 >>>

Personally, I can't recall ever needing copy-on-write for dicts,
since a normal copy is fast enough.

					Andrew
					dalke at dalkescientific.com





More information about the Python-list mailing list