New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-35780: Fix errors in lru_cache() C code #11623
Merged
Merged
Conversation
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
…k->next references. This saves an unnecessary duplicate lookup. Clang assembly before: movq 16(%rax), %rcx # link->prev movq 24(%rax), %rdx # link->next movq %rdx, 24(%rcx) # link->prev->next = link->next; movq 24(%rax), %rdx # duplicate fetch of link->next movq %rcx, 16(%rdx) # link->next->prev = link->prev; Clang assembly after: movq 16(%rax), %rcx movq 24(%rax), %rdx movq %rdx, 24(%rcx) movq %rcx, 16(%rdx)
was incorrectly moved to the newest position as if the user had made a recent call with this key. The fix is to restore it the oldest position, keeping the LRU invariant where keys are tracked by recency of access.
Formerly, the code allowed cache to fall into an inconsistent state. Now, there are no code paths that have a full cache but no links.
Also move decrefs to the end of end path to make it easier to verify that there are no reentrant calls before the cache invariants have been restored.
Save hit update for last (as the pure python version does).
Backporting to 3.6 would require approval from @ned-deily |
Limit to just common scalar types to make the space saving technique easier to reason about (easier to show correctness).
Since the final setitem is potentially reentrant, we have to reset the full status prior to the setitem call (since we've already removed a link and its associated cache dict entry). After the setitem call, we cannot know whether some other thread has reset the status, so we cannot just restore it without checking to make sure the number of dict entries is at maxsize.
Negative maxsize was being treated as a cache size of 1 giving an almost 100% miss rate while still incurring the overhead of cache checking and eviction. The negative maxsize also showed-up in CacheInfo even though it was non-sensical to have a negative maxsize. The negative maxsize also made it into the struct for the C version. This caused erroneous results for the calculation of the "full" flag.
It is cheaper and more reliable to make on-demand checks for whether the cache is full than it is to recompute and occasionally toggle a persistent state variable.
…ad of else clauses
Thanks @rhettinger for the PR |
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Jan 26, 2019
(cherry picked from commit d8080c0) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
GH-11682 is a backport of this pull request to the 3.7 branch. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
https://bugs.python.org/issue35780