Message340560
For the record, here's what is going on. The method_cache() code uses a slightly different invocation for the first call than for subsequent calls. In particular, the wrapper() uses **kwargs with an empty dict whereas the first call didn't use keyword arguments at all. The C version of the lru_cache() is treating that first call as distinct from the second call, resulting in a cache miss for the both the first and second invocation but not in subsequent invocations.
The pure python lru_cache() has a memory saving fast path taken when kwds is an empty dict. The C version is out-of-sync with because it runs the that path only when kwds==NULL and it doesn't check for the case where kwds is an empty dict. Here's a minimal reproducer:
@lru_cache()
def f(x):
pass
f(0)
f(0, **{})
assert f.cache_info().hits == 1
Here's a possible fix:
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 3f1c01651d..f118119479 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -751,7 +751,7 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
Py_ssize_t key_size, pos, key_pos, kwds_size;
/* short path, key will match args anyway, which is a tuple */
- if (!typed && !kwds) {
+ if (!typed && (!kwds || PyDict_GET_SIZE(kwds) == 0)) {
if (PyTuple_GET_SIZE(args) == 1) {
key = PyTuple_GET_ITEM(args, 0);
if (PyUnicode_CheckExact(key) || PyLong_CheckExact(key)) { |
|
Date |
User |
Action |
Args |
2019-04-20 00:55:12 | rhettinger | set | recipients:
+ rhettinger, jaraco, serhiy.storchaka, josh.r |
2019-04-20 00:55:12 | rhettinger | set | messageid: <1555721712.44.0.719963913104.issue36650@roundup.psfhosted.org> |
2019-04-20 00:55:12 | rhettinger | link | issue36650 messages |
2019-04-20 00:55:12 | rhettinger | create | |
|