[PEAK] Re: Trellis-fork

Sergey Schetinin maluke at gmail.com
Thu Jul 9 07:22:45 EDT 2009

The problem with the snapshoting implementation I described before is
the performance of WeakKeyDictionary -- there are a lot of lookups and
WeakKeyDictionary itself is implemented as a UserDict subclass, so
it's quite a bit slower than regular dict.

The snapshot dicts have a number of properties that make them a
candidate for a special implementation (they are read-only, lookups
need to be by identity, no need for key equality checking during
lookup etc), but a less involved solution would be to use a dedicated
key object that would make things work pretty much the same way as if
snapshots were WeakKeyDicts but using regular dict.

Here's what I have in mind:

import gc

class Cell(object):
    def __init__(self):
        self.key = object()

    def __del__(self):
        for ref in gc.get_referrers(self.key):
            if isinstance(ref, dict):
                ref.pop(self.key, None)

state = {}

c = Cell()
state[c.key] = 1
assert state
del c
assert not state, state

Best Regards,
Sergey Schetinin

http://s3bk.com/ -- S3 Backup
http://word-to-html.com/ -- Word to HTML Converter

More information about the PEAK mailing list