Skip to content

Commit ffdf1c3

Browse files
authored
Consistently move the misses update to just before the user function call (GH-11715)
1 parent dcfcd14 commit ffdf1c3

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

Lib/functools.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,10 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
541541
if maxsize == 0:
542542

543543
def wrapper(*args, **kwds):
544-
# No caching -- just a statistics update after a successful call
544+
# No caching -- just a statistics update
545545
nonlocal misses
546-
result = user_function(*args, **kwds)
547546
misses += 1
547+
result = user_function(*args, **kwds)
548548
return result
549549

550550
elif maxsize is None:
@@ -557,9 +557,9 @@ def wrapper(*args, **kwds):
557557
if result is not sentinel:
558558
hits += 1
559559
return result
560+
misses += 1
560561
result = user_function(*args, **kwds)
561562
cache[key] = result
562-
misses += 1
563563
return result
564564

565565
else:

Modules/_functoolsmodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -796,10 +796,12 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
796796
static PyObject *
797797
uncached_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds)
798798
{
799-
PyObject *result = PyObject_Call(self->func, args, kwds);
799+
PyObject *result;
800+
801+
self->misses++;
802+
result = PyObject_Call(self->func, args, kwds);
800803
if (!result)
801804
return NULL;
802-
self->misses++;
803805
return result;
804806
}
805807

@@ -827,6 +829,7 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
827829
Py_DECREF(key);
828830
return NULL;
829831
}
832+
self->misses++;
830833
result = PyObject_Call(self->func, args, kwds);
831834
if (!result) {
832835
Py_DECREF(key);
@@ -838,7 +841,6 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
838841
return NULL;
839842
}
840843
Py_DECREF(key);
841-
self->misses++;
842844
return result;
843845
}
844846

0 commit comments

Comments
 (0)