diff options
author | Jay Hutchinson <jlhutch@gmail.com> | 2022-03-01 14:44:32 -0600 |
---|---|---|
committer | Jay Hutchinson <jlhutch@gmail.com> | 2022-03-06 10:27:10 -0600 |
commit | f3b10e11d8253adb2a91318230952354dd77829e (patch) | |
tree | eb27b06692ad2b176edfd67619bee6970cb12609 | |
parent | 968a41a4094c20bb24fb1a604a44dd9bbc5aed33 (diff) | |
download | pylru-f3b10e11d8253adb2a91318230952354dd77829e.tar.gz |
Small optimization to popitem().
-rw-r--r-- | pylru.py | 24 |
1 files changed, 21 insertions, 3 deletions
@@ -205,9 +205,27 @@ class lrucache(object): if len(self) < 1: raise KeyError - key = self.head.key - value = self.head.value - del self[key] + # Grab the head node + node = self.head + + # Save the key and value so that we can return them. + key = node.key + value = node.value + + # Remove the key from the hash table and mark the node as empty. + del self.table[key] + node.empty = True + + # Not strictly necessary. + node.key = None + node.value = None + + # Because this node is now empty we want to reuse it before any + # non-empty node. To do that we want to move it to the tail of the + # list. This node is the head node. Due to the list being circular, + # the ordering is already correct, we just need to adjust the 'head' + # variable. + self.head = node.next return key, value |