Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2dda72a

Browse files
rhettingermiss-islington
authored andcommitted
lru_cache: Add more comments. Fix comment typos. Clarify a comment. (GH-11795)
1 parent 7ab3d15 commit 2dda72a

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

Modules/_functoolsmodule.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,26 @@ sequence is empty.");
661661

662662
/* lru_cache object **********************************************************/
663663

664+
/* There are four principal algorithmic differences from the pure python version:
665+
666+
1). The C version relies on the GIL instead of having its own reentrant lock.
667+
668+
2). The prev/next link fields use borrowed references.
669+
670+
3). For a full cache, the pure python version rotates the location of the
671+
root entry so that it never has to move individual links and it can
672+
limit updates to just the key and result fields. However, in the C
673+
version, links are temporarily removed while the cache dict updates are
674+
occurring. Afterwards, they are appended or prepended back into the
675+
doubly-linked lists.
676+
677+
4) In the Python version, the _HashSeq class is used to prevent __hash__
678+
from being called more than once. In the C version, the "known hash"
679+
variants of dictionary calls as used to the same effect.
680+
681+
*/
682+
683+
664684
/* this object is used delimit args and keywords in the cache keys */
665685
static PyObject *kwd_mark = NULL;
666686

@@ -1009,14 +1029,15 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
10091029
link = self->root.next;
10101030
lru_cache_extract_link(link);
10111031
/* Remove it from the cache.
1012-
The cache dict holds one reference to the link,
1013-
and the linked list holds yet one reference to it. */
1032+
The cache dict holds one reference to the link.
1033+
We created one other reference when the link was created.
1034+
The linked list only has borrowed references. */
10141035
popresult = _PyDict_Pop_KnownHash(self->cache, link->key,
10151036
link->hash, Py_None);
10161037
if (popresult == Py_None) {
10171038
/* Getting here means that the user function call or another
10181039
thread has already removed the old key from the dictionary.
1019-
This link is now an orpan. Since we don't want to leave the
1040+
This link is now an orphan. Since we don't want to leave the
10201041
cache in an inconsistent state, we don't restore the link. */
10211042
Py_DECREF(popresult);
10221043
Py_DECREF(link);
@@ -1048,7 +1069,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
10481069
prev and next fields set to valid values. We have to wait
10491070
for successful insertion in the cache dict before adding the
10501071
link to the linked list. Otherwise, the potentially reentrant
1051-
__eq__ call could cause the then ophan link to be visited. */
1072+
__eq__ call could cause the then orphan link to be visited. */
10521073
if (_PyDict_SetItem_KnownHash(self->cache, key, (PyObject *)link,
10531074
hash) < 0) {
10541075
/* Somehow the cache dict update failed. We no longer can

0 commit comments

Comments
 (0)