Skip to content

Commit fafcfff

Browse files
authored
bpo-44310: Note that lru_cache keep references to both arguments and results (GH-26715)
* Simplify the count_vowels example * Hits and misses are fetched while a lock is held * Add note that references are kept for arguments and return values * Clarify behavior when *typed* is false.
1 parent bf52727 commit fafcfff

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Doc/library/functools.rst

+9-6
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,16 @@ The :mod:`functools` module defines the following functions:
154154

155155
@lru_cache
156156
def count_vowels(sentence):
157-
sentence = sentence.casefold()
158-
return sum(sentence.count(vowel) for vowel in 'aeiou')
157+
return sum(sentence.count(vowel) for vowel in 'AEIOUaeiou')
159158

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

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

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

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

185+
The cache keeps references to the arguments and return values until they age
186+
out of the cache or until the cache is cleared.
187+
185188
An `LRU (least recently used) cache
186189
<https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)>`_
187190
works best when the most recent calls are the best predictors of upcoming

0 commit comments

Comments
 (0)