~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lru_cache.py

  • Committer: John Arbash Meinel
  • Date: 2009-05-01 20:20:37 UTC
  • mto: This revision was merged to the branch mainline in revision 4323.
  • Revision ID: john@arbash-meinel.com-20090501202037-frhn2e9060142ldf
Small tweaks from Ian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
        if self.prev is None:
44
44
            prev_key = None
45
45
        else:
46
 
            prev_key = self.prev_key
 
46
            prev_key = self.prev.key
47
47
        return '%s(%r n:%r p:%r)' % (self.__class__.__name__, self.key,
48
48
                                     self.next_key, prev_key)
49
49
 
70
70
        # The "HEAD" of the lru linked list
71
71
        self._most_recently_used = None
72
72
        # The "TAIL" of the lru linked list
73
 
        self._last_recently_used = None
 
73
        self._least_recently_used = None
74
74
        self._update_max_cache(max_cache, after_cleanup_count)
75
75
 
76
76
    def __contains__(self, key):
91
91
        node_prev = node.prev
92
92
        next_key = node.next_key
93
93
        # benchmarking shows that the lookup of _null_key in globals is faster
94
 
        # than the attribute lookup for (node is self._last_recently_used)
 
94
        # than the attribute lookup for (node is self._least_recently_used)
95
95
        if next_key is _null_key:
96
 
            # 'node' is the _last_recently_used, because it doesn't have a
 
96
            # 'node' is the _least_recently_used, because it doesn't have a
97
97
            # 'next' item. So move the current lru to the previous node.
98
 
            self._last_recently_used = node_prev
 
98
            self._least_recently_used = node_prev
99
99
        else:
100
100
            node_next = cache[next_key]
101
101
            node_next.prev = node_prev
120
120
                                     ' %s' % (node,))
121
121
        while node is not None:
122
122
            if node.next_key is _null_key:
123
 
                if node is not self._last_recently_used:
 
123
                if node is not self._least_recently_used:
124
124
                    raise AssertionError('only the last node should have'
125
125
                                         ' no next value: %s' % (node,))
126
126
                node_next = None
213
213
        # Move 'node' to the front of the queue
214
214
        if self._most_recently_used is None:
215
215
            self._most_recently_used = node
216
 
            self._last_recently_used = node
 
216
            self._least_recently_used = node
217
217
            return
218
218
        elif node is self._most_recently_used:
219
219
            # Nothing to do, this node is already at the head of the queue
221
221
        # We've taken care of the tail pointer, remove the node, and insert it
222
222
        # at the front
223
223
        # REMOVE
224
 
        if node is self._last_recently_used:
225
 
            self._last_recently_used = node.prev
 
224
        if node is self._least_recently_used:
 
225
            self._least_recently_used = node.prev
226
226
        if node.prev is not None:
227
227
            node.prev.next_key = node.next_key
228
228
        if node.next_key is not _null_key:
235
235
        node.prev = None
236
236
 
237
237
    def _remove_node(self, node):
238
 
        if node is self._last_recently_used:
239
 
            self._last_recently_used = node.prev
 
238
        if node is self._least_recently_used:
 
239
            self._least_recently_used = node.prev
240
240
        self._cache.pop(node.key)
241
241
        # If we have removed all entries, remove the head pointer as well
242
 
        if self._last_recently_used is None:
 
242
        if self._least_recently_used is None:
243
243
            self._most_recently_used = None
244
244
        node.run_cleanup()
245
245
        # Now remove this node from the linked list
258
258
        If there are no more references to the lru, then this entry should be
259
259
        removed from the cache.
260
260
        """
261
 
        self._remove_node(self._last_recently_used)
 
261
        self._remove_node(self._least_recently_used)
262
262
 
263
263
    def clear(self):
264
264
        """Clear out all of the cache."""