/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/lru_cache.py

  • Committer: John Arbash Meinel
  • Date: 2009-06-04 17:12:29 UTC
  • mto: This revision was merged to the branch mainline in revision 4410.
  • Revision ID: john@arbash-meinel.com-20090604171229-kbgfatt63y3u3uh1
Some small tweaks to decoding strings (avoid passing over the length 2x)

Down to 1.1s (from 1.4s) for decoding all of bzr.dev.
Also, favor decoding strings and then lists in _decode_object, since that is the
frequency we have those types inside Revisions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2006, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
48
48
                                     self.next_key, prev_key)
49
49
 
50
50
    def run_cleanup(self):
51
 
        try:
52
 
            if self.cleanup is not None:
53
 
                self.cleanup(self.key, self.value)
54
 
        finally:
55
 
            # cleanup might raise an exception, but we want to make sure
56
 
            # to break refcycles, etc
57
 
            self.cleanup = None
58
 
            self.value = None
 
51
        if self.cleanup is not None:
 
52
            self.cleanup(self.key, self.value)
 
53
        self.cleanup = None
 
54
        # Just make sure to break any refcycles, etc
 
55
        self.value = None
59
56
 
60
57
 
61
58
class LRUCache(object):
159
156
            raise ValueError('cannot use _null_key as a key')
160
157
        if key in self._cache:
161
158
            node = self._cache[key]
162
 
            try:
163
 
                node.run_cleanup()
164
 
            finally:
165
 
                # Maintain the LRU properties, even if cleanup raises an
166
 
                # exception
167
 
                node.value = value
168
 
                node.cleanup = cleanup
169
 
                self._record_access(node)
 
159
            node.run_cleanup()
 
160
            node.value = value
 
161
            node.cleanup = cleanup
170
162
        else:
171
163
            node = _LRUNode(key, value, cleanup=cleanup)
172
164
            self._cache[key] = node
173
 
            self._record_access(node)
 
165
        self._record_access(node)
174
166
 
175
167
        if len(self._cache) > self._max_cache:
176
168
            # Trigger the cleanup
249
241
        # If we have removed all entries, remove the head pointer as well
250
242
        if self._least_recently_used is None:
251
243
            self._most_recently_used = None
252
 
        try:
253
 
            node.run_cleanup()
254
 
        finally:
255
 
            # cleanup might raise an exception, but we want to make sure to
256
 
            # maintain the linked list
257
 
            if node.prev is not None:
258
 
                node.prev.next_key = node.next_key
259
 
            if node.next_key is not _null_key:
260
 
                node_next = self._cache[node.next_key]
261
 
                node_next.prev = node.prev
262
 
            # And remove this node's pointers
263
 
            node.prev = None
264
 
            node.next_key = _null_key
 
244
        node.run_cleanup()
 
245
        # Now remove this node from the linked list
 
246
        if node.prev is not None:
 
247
            node.prev.next_key = node.next_key
 
248
        if node.next_key is not _null_key:
 
249
            node_next = self._cache[node.next_key]
 
250
            node_next.prev = node.prev
 
251
        # And remove this node's pointers
 
252
        node.prev = None
 
253
        node.next_key = _null_key
265
254
 
266
255
    def _remove_lru(self):
267
256
        """Remove one entry from the lru, and handle consequences.