From b729181a504b6aff2461dcbc07c33e902ee90cb0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 6 Dec 2016 18:45:50 +0100 Subject: Use _PyObject_CallNoArg() Replace: PyObject_CallObject(callable, NULL) with: _PyObject_CallNoArg(callable) --- Modules/_functoolsmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Modules/_functoolsmodule.c') diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index 2269d05da8..19ca65becc 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -1246,7 +1246,7 @@ PyInit__functools(void) if (m == NULL) return NULL; - kwd_mark = PyObject_CallObject((PyObject *)&PyBaseObject_Type, NULL); + kwd_mark = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); if (!kwd_mark) { Py_DECREF(m); return NULL; -- cgit v1.2.1 From f22b56b777f7134b5c4e6c9caa9249e92c4f4fd9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 16 Dec 2016 16:18:57 +0200 Subject: Issue #28959: Added private macro PyDict_GET_SIZE for retrieving the size of dict. --- Modules/_functoolsmodule.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Modules/_functoolsmodule.c') diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index 19ca65becc..6b5c008f74 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -84,7 +84,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) } Py_DECREF(nargs); - if (pkw == NULL || PyDict_Size(pkw) == 0) { + if (pkw == NULL || PyDict_GET_SIZE(pkw) == 0) { if (kw == NULL) { pto->kw = PyDict_New(); } @@ -155,7 +155,7 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kw) assert(PyTuple_Check(argappl)); } - if (PyDict_Size(pto->kw) == 0) { + if (PyDict_GET_SIZE(pto->kw) == 0) { kwappl = kw; Py_XINCREF(kwappl); } @@ -713,7 +713,7 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed) return args; } - if (kwds && PyDict_Size(kwds) > 0) { + if (kwds && PyDict_GET_SIZE(kwds) > 0) { sorted_items = PyDict_Items(kwds); if (!sorted_items) return NULL; @@ -933,7 +933,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds } lru_cache_append_link(self, link); Py_INCREF(result); /* for return */ - self->full = (PyDict_Size(self->cache) >= self->maxsize); + self->full = (PyDict_GET_SIZE(self->cache) >= self->maxsize); } self->misses++; return result; @@ -1062,7 +1062,7 @@ lru_cache_cache_info(lru_cache_object *self, PyObject *unused) { return PyObject_CallFunction(self->cache_info_type, "nnOn", self->hits, self->misses, self->maxsize_O, - PyDict_Size(self->cache)); + PyDict_GET_SIZE(self->cache)); } static PyObject * -- cgit v1.2.1 From 1708f4b554b63e5a25691f316db8d392c0692392 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 8 Jan 2017 18:04:30 -0800 Subject: Complete the merge for issue #29203 --- Modules/_functoolsmodule.c | 49 +++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) (limited to 'Modules/_functoolsmodule.c') diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index 6b5c008f74..62b45e98cb 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -704,8 +704,8 @@ static PyTypeObject lru_cache_type; static PyObject * lru_cache_make_key(PyObject *args, PyObject *kwds, int typed) { - PyObject *key, *sorted_items; - Py_ssize_t key_size, pos, key_pos; + PyObject *key, *keyword, *value; + Py_ssize_t key_size, pos, key_pos, kwds_size; /* short path, key will match args anyway, which is a tuple */ if (!typed && !kwds) { @@ -713,28 +713,18 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed) return args; } - if (kwds && PyDict_GET_SIZE(kwds) > 0) { - sorted_items = PyDict_Items(kwds); - if (!sorted_items) - return NULL; - if (PyList_Sort(sorted_items) < 0) { - Py_DECREF(sorted_items); - return NULL; - } - } else - sorted_items = NULL; + kwds_size = kwds ? PyDict_GET_SIZE(kwds) : 0; + assert(kwds_size >= 0); key_size = PyTuple_GET_SIZE(args); - if (sorted_items) - key_size += PyList_GET_SIZE(sorted_items); + if (kwds_size) + key_size += kwds_size * 2 + 1; if (typed) - key_size *= 2; - if (sorted_items) - key_size++; + key_size += PyTuple_GET_SIZE(args) + kwds_size; key = PyTuple_New(key_size); if (key == NULL) - goto done; + return NULL; key_pos = 0; for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) { @@ -742,14 +732,16 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed) Py_INCREF(item); PyTuple_SET_ITEM(key, key_pos++, item); } - if (sorted_items) { + if (kwds_size) { Py_INCREF(kwd_mark); PyTuple_SET_ITEM(key, key_pos++, kwd_mark); - for (pos = 0; pos < PyList_GET_SIZE(sorted_items); ++pos) { - PyObject *item = PyList_GET_ITEM(sorted_items, pos); - Py_INCREF(item); - PyTuple_SET_ITEM(key, key_pos++, item); + for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) { + Py_INCREF(keyword); + PyTuple_SET_ITEM(key, key_pos++, keyword); + Py_INCREF(value); + PyTuple_SET_ITEM(key, key_pos++, value); } + assert(key_pos == PyTuple_GET_SIZE(args) + kwds_size * 2 + 1); } if (typed) { for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) { @@ -757,20 +749,15 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed) Py_INCREF(item); PyTuple_SET_ITEM(key, key_pos++, item); } - if (sorted_items) { - for (pos = 0; pos < PyList_GET_SIZE(sorted_items); ++pos) { - PyObject *tp_items = PyList_GET_ITEM(sorted_items, pos); - PyObject *item = (PyObject *)Py_TYPE(PyTuple_GET_ITEM(tp_items, 1)); + if (kwds_size) { + for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) { + PyObject *item = (PyObject *)Py_TYPE(value); Py_INCREF(item); PyTuple_SET_ITEM(key, key_pos++, item); } } } assert(key_pos == key_size); - -done: - if (sorted_items) - Py_DECREF(sorted_items); return key; } -- cgit v1.2.1 From 0e8e0bbd7ca35e98c840be65bfb6776ad900ac7c Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 8 Jan 2017 19:34:28 -0800 Subject: Sync-up lru_cache() C code with space saving feature in the Python version. --- Modules/_functoolsmodule.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'Modules/_functoolsmodule.c') diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index 62b45e98cb..bc34c215b4 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -709,6 +709,12 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed) /* short path, key will match args anyway, which is a tuple */ if (!typed && !kwds) { + if (PyTuple_GET_SIZE(args) == 1) { + /* Save space and improve speed by unwrapping 1-tuples */ + key = PyTuple_GET_ITEM(args, 0); + Py_INCREF(key); + return key; + } Py_INCREF(args); return args; } -- cgit v1.2.1 From 2b7cb3682436fbb82d27307cedaa7a547d5a1079 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 9 Jan 2017 07:39:46 -0800 Subject: Backed out changeset ea064ff3c10f --- Modules/_functoolsmodule.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'Modules/_functoolsmodule.c') diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c index bc34c215b4..62b45e98cb 100644 --- a/Modules/_functoolsmodule.c +++ b/Modules/_functoolsmodule.c @@ -709,12 +709,6 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed) /* short path, key will match args anyway, which is a tuple */ if (!typed && !kwds) { - if (PyTuple_GET_SIZE(args) == 1) { - /* Save space and improve speed by unwrapping 1-tuples */ - key = PyTuple_GET_ITEM(args, 0); - Py_INCREF(key); - return key; - } Py_INCREF(args); return args; } -- cgit v1.2.1