Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Optimized some set operations (e.g. ``|``, ``^``, and ``-``) of
``dict_keys``. ``d.keys() | other`` was slower than ``set(d) | other`` but
they are almost same performance for now.
54 changes: 33 additions & 21 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4162,17 +4162,34 @@ static PySequenceMethods dictkeys_as_sequence = {
(objobjproc)dictkeys_contains, /* sq_contains */
};

// Create an set object from dictviews object.
// Returns a new reference.
// This utility function is used by set operations.
static PyObject*
dictviews_sub(PyObject* self, PyObject *other)
dictviews_to_set(PyObject *self)
{
PyObject *result = PySet_New(self);
PyObject *tmp;
_Py_IDENTIFIER(difference_update);
PyObject *left = self;
if (PyDictKeys_Check(self)) {
Comment thread
methane marked this conversation as resolved.
// PySet_New() has fast path for the dict object.
PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
if (PyDict_CheckExact(dict)) {
left = dict;
}
}
return PySet_New(left);
}

if (result == NULL)
static PyObject*
dictviews_sub(PyObject *self, PyObject *other)
{
PyObject *result = dictviews_to_set(self);
if (result == NULL) {
return NULL;
}

tmp = _PyObject_CallMethodIdOneArg(result, &PyId_difference_update, other);
_Py_IDENTIFIER(difference_update);
PyObject *tmp = _PyObject_CallMethodIdOneArg(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether calling set.difference_update(result, other) using fast call can be faster than calling result.difference_update(other). But this is for other issue.

result, &PyId_difference_update, other);
if (tmp == NULL) {
Py_DECREF(result);
return NULL;
Expand Down Expand Up @@ -4273,34 +4290,29 @@ _PyDictView_Intersect(PyObject* self, PyObject *other)
static PyObject*
dictviews_or(PyObject* self, PyObject *other)
{
PyObject *result = PySet_New(self);
PyObject *tmp;
_Py_IDENTIFIER(update);

if (result == NULL)
PyObject *result = dictviews_to_set(self);
if (result == NULL) {
return NULL;
}

tmp = _PyObject_CallMethodIdOneArg(result, &PyId_update, other);
if (tmp == NULL) {
if (_PySet_Update(result, other) < 0) {
Py_DECREF(result);
return NULL;
}

Py_DECREF(tmp);
return result;
}

static PyObject*
dictviews_xor(PyObject* self, PyObject *other)
{
PyObject *result = PySet_New(self);
PyObject *tmp;
_Py_IDENTIFIER(symmetric_difference_update);

if (result == NULL)
PyObject *result = dictviews_to_set(self);
if (result == NULL) {
return NULL;
}

tmp = _PyObject_CallMethodIdOneArg(result, &PyId_symmetric_difference_update, other);
_Py_IDENTIFIER(symmetric_difference_update);
PyObject *tmp = _PyObject_CallMethodIdOneArg(
result, &PyId_symmetric_difference_update, other);
if (tmp == NULL) {
Py_DECREF(result);
return NULL;
Expand Down