summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Hutchinson <jlhutch@gmail.com>2022-03-01 14:44:32 -0600
committerJay Hutchinson <jlhutch@gmail.com>2022-03-06 10:27:10 -0600
commitf3b10e11d8253adb2a91318230952354dd77829e (patch)
treeeb27b06692ad2b176edfd67619bee6970cb12609
parent968a41a4094c20bb24fb1a604a44dd9bbc5aed33 (diff)
downloadpylru-f3b10e11d8253adb2a91318230952354dd77829e.tar.gz
Small optimization to popitem().
-rw-r--r--pylru.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/pylru.py b/pylru.py
index 842c7bf..6e031a9 100644
--- a/pylru.py
+++ b/pylru.py
@@ -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