Skip to content
Open
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 @@
Optimize pattern matching of mappings when the mapping is a builtin dictionary.
55 changes: 55 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,58 @@ static const binaryfunc binary_ops[] = {
// PEP 634: Structural Pattern Matching


// Faster version of match_keys (below) that works on a dict instead of
// a generic mapping
static PyObject*
match_keys_exactdict(PyThreadState *tstate, PyObject *map, PyObject *keys, Py_ssize_t nkeys)
{
PyObject *seen = NULL;
PyObject *values = NULL;
seen = PySet_New(NULL);
if (seen == NULL) {
goto fail;
}
values = PyTuple_New(nkeys);
if (values == NULL) {
goto fail;
}
for (Py_ssize_t i = 0; i < nkeys; i++) {
PyObject *key = PyTuple_GET_ITEM(keys, i);
if (PySet_Contains(seen, key) || PySet_Add(seen, key)) {
if (!_PyErr_Occurred(tstate)) {
// Seen it before!
_PyErr_Format(tstate, PyExc_ValueError,
"mapping pattern checks duplicate key (%R)", key);
}
goto fail;
}
PyObject *value = PyDict_GetItemWithError(map, key);
if (value == NULL) {
if (_PyErr_Occurred(tstate)) {
goto fail;
} else {
// key not in map!
Py_DECREF(values);
// Return None:
Py_INCREF(Py_None);
values = Py_None;
goto done;
}
}
Py_INCREF(value);
PyTuple_SET_ITEM(values, i, value);
}
// Success:
done:
Py_DECREF(seen);
return values;
fail:
Py_XDECREF(seen);
Py_XDECREF(values);
return NULL;
}


// Return a tuple of values corresponding to keys, with error checks for
// duplicate/missing keys.
static PyObject*
Expand All @@ -917,6 +969,9 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
// No keys means no items.
return PyTuple_New(0);
}
if (PyDict_CheckExact(map)) {
return match_keys_exactdict(tstate, map, keys, nkeys);
}
PyObject *seen = NULL;
PyObject *dummy = NULL;
PyObject *values = NULL;
Expand Down