From b8d7e564edeecfdc5a31a84dc040e45237383ede Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 14 May 2022 16:39:05 +0900 Subject: [PATCH 1/3] Memory optimization for set.issubset Co-authored-by: Jack Nguyen --- Objects/setobject.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Objects/setobject.c b/Objects/setobject.c index 4b6a8b8dfb679d..373385e3a93063 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1735,12 +1735,19 @@ set_issubset(PySetObject *so, PyObject *other) int rv; if (!PyAnySet_Check(other)) { - PyObject *tmp, *result; - tmp = make_new_set(&PySet_Type, other); - if (tmp == NULL) + PyObject *tmp = set_intersection(so, other); + if (tmp == NULL) { return NULL; - result = set_issubset(so, tmp); + } + PyObject *result = NULL; + if (PySet_GET_SIZE(tmp) == PySet_GET_SIZE(so)) { + result = Py_True; + } + else { + result = Py_False; + } Py_DECREF(tmp); + Py_INCREF(result); return result; } if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other)) From cb0bb0ae23455781a6f7fc91c1f45694b9446afb Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 14 May 2022 17:19:04 +0900 Subject: [PATCH 2/3] Address code review --- Objects/setobject.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Objects/setobject.c b/Objects/setobject.c index 373385e3a93063..42a6fcc9035692 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1739,16 +1739,9 @@ set_issubset(PySetObject *so, PyObject *other) if (tmp == NULL) { return NULL; } - PyObject *result = NULL; - if (PySet_GET_SIZE(tmp) == PySet_GET_SIZE(so)) { - result = Py_True; - } - else { - result = Py_False; - } + int result = PySet_GET_SIZE(tmp) == PySet_GET_SIZE(so); Py_DECREF(tmp); - Py_INCREF(result); - return result; + return PyBool_FromLong(result); } if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other)) Py_RETURN_FALSE; From 8e15f125f128aed7b4423a62030836310598fc5f Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sat, 14 May 2022 17:20:30 +0900 Subject: [PATCH 3/3] nit --- Objects/setobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/setobject.c b/Objects/setobject.c index 42a6fcc9035692..dd55a943010ab8 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1739,7 +1739,7 @@ set_issubset(PySetObject *so, PyObject *other) if (tmp == NULL) { return NULL; } - int result = PySet_GET_SIZE(tmp) == PySet_GET_SIZE(so); + int result = (PySet_GET_SIZE(tmp) == PySet_GET_SIZE(so)); Py_DECREF(tmp); return PyBool_FromLong(result); }