Author rhettinger
Recipients jaraco, josh.r, rhettinger, serhiy.storchaka
Date 2019-04-20.00:55:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1555721712.44.0.719963913104.issue36650@roundup.psfhosted.org>
In-reply-to
Content
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)) {
History
Date User Action Args
2019-04-20 00:55:12rhettingersetrecipients: + rhettinger, jaraco, serhiy.storchaka, josh.r
2019-04-20 00:55:12rhettingersetmessageid: <1555721712.44.0.719963913104.issue36650@roundup.psfhosted.org>
2019-04-20 00:55:12rhettingerlinkissue36650 messages
2019-04-20 00:55:12rhettingercreate