Skip to content

bpo-44310: Note that lru_cache keep references to both arguments and results #26715

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

Merged
merged 26 commits into from
Jun 14, 2021
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bbd2da9
Merge pull request #1 from python/master
rhettinger Mar 16, 2021
74bdf1b
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
6c53f1a
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
a487c4f
.
rhettinger Mar 24, 2021
eb56423
.
rhettinger Mar 25, 2021
cc7ba06
.
rhettinger Mar 26, 2021
d024dd0
.
rhettinger Apr 22, 2021
b10f912
merge
rhettinger May 5, 2021
0958bf0
merge
rhettinger May 6, 2021
399afee
Merge branch 'main' of github.com:python/cpython
rhettinger May 8, 2021
daf9f13
Merge branch 'main' of github.com:python/cpython
rhettinger May 13, 2021
423c26e
Merge branch 'main' of github.com:python/cpython
rhettinger May 14, 2021
919d54d
Merge branch 'main' of github.com:python/cpython
rhettinger May 15, 2021
6666c42
Merge branch 'main' of github.com:python/cpython
rhettinger May 16, 2021
9fc71d6
Merge branch 'main' of github.com:python/cpython
rhettinger May 18, 2021
5f4f498
Merge branch 'main' of github.com:python/cpython
rhettinger May 21, 2021
a5aa352
Merge branch 'main' of github.com:python/cpython
rhettinger May 25, 2021
d1fceb2
Merge branch 'main' of github.com:python/cpython
rhettinger May 25, 2021
dd123c6
Merge branch 'main' of github.com:python/cpython
rhettinger May 26, 2021
9dc931c
Merge branch 'main' of github.com:python/cpython
rhettinger Jun 4, 2021
0492914
Merge branch 'main' of github.com:python/cpython
rhettinger Jun 5, 2021
fd83668
Simplify the count_vowels example
rhettinger Jun 5, 2021
b4ef895
Hits and misses are fetched while a lock is held
rhettinger Jun 5, 2021
61b527f
Add note that references are kept for arguments and return values
rhettinger Jun 5, 2021
77c826a
Fix typo
rhettinger Jun 5, 2021
094c41a
Clarify behavior when *typed* is false.
rhettinger Jun 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Doc/library/functools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,16 @@ The :mod:`functools` module defines the following functions:

@lru_cache
def count_vowels(sentence):
sentence = sentence.casefold()
return sum(sentence.count(vowel) for vowel in 'aeiou')
return sum(sentence.count(vowel) for vowel in 'AEIOUaeiou')

If *maxsize* is set to ``None``, the LRU feature is disabled and the cache can
grow without bound.

If *typed* is set to true, function arguments of different types will be
cached separately. For example, ``f(3)`` and ``f(3.0)`` will be treated
as distinct calls with distinct results.
cached separately. For example, ``f(3)`` and ``f(3.0)`` will always be
treated as distinct calls with distinct results. If *typed* is false,
the implementation will usually but not always regard them as equivalent
calls and only cache a single result.

The wrapped function is instrumented with a :func:`cache_parameters`
function that returns a new :class:`dict` showing the values for *maxsize*
Expand All @@ -172,8 +173,7 @@ The :mod:`functools` module defines the following functions:
To help measure the effectiveness of the cache and tune the *maxsize*
parameter, the wrapped function is instrumented with a :func:`cache_info`
function that returns a :term:`named tuple` showing *hits*, *misses*,
*maxsize* and *currsize*. In a multi-threaded environment, the hits
and misses are approximate.
*maxsize* and *currsize*.

The decorator also provides a :func:`cache_clear` function for clearing or
invalidating the cache.
Expand All @@ -182,6 +182,9 @@ The :mod:`functools` module defines the following functions:
:attr:`__wrapped__` attribute. This is useful for introspection, for
bypassing the cache, or for rewrapping the function with a different cache.

The cache keeps references to the arguments and return values until they age
out of the cache or until the cache is cleared.

An `LRU (least recently used) cache
<https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)>`_
works best when the most recent calls are the best predictors of upcoming
Expand Down