'prune unreferenced, non-dirty states.'

def prune(self):

    ref_count = len(self)
    dirty = [s.obj() for s in self.all_states() if s.modified]
    keepers = weakref.WeakValueDictionary()
    keepers.update(self)
    dict.clear(self)
    dict.update(self, keepers)
    self.modified = bool(dirty)
    return (ref_count - len(self))
