Unverified Kaydet (Commit) ffdf1c30 authored tarafından Raymond Hettinger's avatar Raymond Hettinger Kaydeden (comit) GitHub

Consistently move the misses update to just before the user function call (GH-11715)

üst dcfcd146
...@@ -541,10 +541,10 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo): ...@@ -541,10 +541,10 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
if maxsize == 0: if maxsize == 0:
def wrapper(*args, **kwds): def wrapper(*args, **kwds):
# No caching -- just a statistics update after a successful call # No caching -- just a statistics update
nonlocal misses nonlocal misses
result = user_function(*args, **kwds)
misses += 1 misses += 1
result = user_function(*args, **kwds)
return result return result
elif maxsize is None: elif maxsize is None:
...@@ -557,9 +557,9 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo): ...@@ -557,9 +557,9 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
if result is not sentinel: if result is not sentinel:
hits += 1 hits += 1
return result return result
misses += 1
result = user_function(*args, **kwds) result = user_function(*args, **kwds)
cache[key] = result cache[key] = result
misses += 1
return result return result
else: else:
......
...@@ -796,10 +796,12 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed) ...@@ -796,10 +796,12 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
static PyObject * static PyObject *
uncached_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds) uncached_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds)
{ {
PyObject *result = PyObject_Call(self->func, args, kwds); PyObject *result;
self->misses++;
result = PyObject_Call(self->func, args, kwds);
if (!result) if (!result)
return NULL; return NULL;
self->misses++;
return result; return result;
} }
...@@ -827,6 +829,7 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd ...@@ -827,6 +829,7 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
Py_DECREF(key); Py_DECREF(key);
return NULL; return NULL;
} }
self->misses++;
result = PyObject_Call(self->func, args, kwds); result = PyObject_Call(self->func, args, kwds);
if (!result) { if (!result) {
Py_DECREF(key); Py_DECREF(key);
...@@ -838,7 +841,6 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd ...@@ -838,7 +841,6 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
return NULL; return NULL;
} }
Py_DECREF(key); Py_DECREF(key);
self->misses++;
return result; return result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment