This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix LruCache. Make TreeCache track its own size. #538
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
50e1893
Reset size on clear
erikjohnston 766526e
Make TreeCache keep track of its own size.
erikjohnston a30364c
Correctly bookkeep the size of TreeCache
erikjohnston c046630
Remove spurious self.size
erikjohnston fb72998
Directly set self.value
erikjohnston 4fce59f
Add tests
erikjohnston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ class TreeCache(object): | |
Keys must be tuples. | ||
""" | ||
def __init__(self): | ||
self.size = 0 | ||
self.root = {} | ||
|
||
def __setitem__(self, key, value): | ||
|
@@ -20,17 +21,19 @@ def set(self, key, value): | |
node = self.root | ||
for k in key[:-1]: | ||
node = node.setdefault(k, {}) | ||
node[key[-1]] = value | ||
node[key[-1]] = _Entry(value) | ||
self.size += 1 | ||
|
||
def get(self, key, default=None): | ||
node = self.root | ||
for k in key[:-1]: | ||
node = node.get(k, None) | ||
if node is None: | ||
return default | ||
return node.get(key[-1], default) | ||
return node.get(key[-1], _Entry(default)).value | ||
|
||
def clear(self): | ||
self.size = 0 | ||
self.root = {} | ||
|
||
def pop(self, key, default=None): | ||
|
@@ -57,4 +60,33 @@ def pop(self, key, default=None): | |
break | ||
node_and_keys[i+1][0].pop(k) | ||
|
||
popped, cnt = _strip_and_count_entires(popped) | ||
self.size -= cnt | ||
return popped | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are going to enumerate the popped tree here then you might as well return a list of popped entries for the LruCache to remove from its linked list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd quite like to keep the |
||
|
||
def __len__(self): | ||
return self.size | ||
|
||
|
||
class _Entry(object): | ||
__slots__ = ["value"] | ||
|
||
def __init__(self, value): | ||
object.__setattr__(self, "value", value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just |
||
|
||
|
||
def _strip_and_count_entires(d): | ||
"""Takes an _Entry or dict with leaves of _Entry's, and either returns the | ||
value or a dictionary with _Entry's replaced by their values. | ||
|
||
Also returns the count of _Entry's | ||
""" | ||
if isinstance(d, dict): | ||
cnt = 0 | ||
for key, value in d.items(): | ||
v, n = _strip_and_count_entires(value) | ||
d[key] = v | ||
cnt += n | ||
return d, cnt | ||
else: | ||
return d.value, 1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this still here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also why didn't this fix the problem OOI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dislike the
LruCache
trying to keep track of the size of the underlying cache. I much prefer that caches/collections keep track of their own size.