Skip to content

Use istr as keys in CIMultiDict #1097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 28 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
6 changes: 6 additions & 0 deletions CHANGES/1097.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Rewritten :class:`multidict.CIMultiDict` and it proxy to always return
:class:`multidict.istr` keys. ``istr`` is derived from :class:`str`,
thus the change is backward compatible.

The performance boost is about 15% for some operations for C Extension,
pure Python implementation have got a visible (15% - 230%) speedup as well.
88 changes: 12 additions & 76 deletions multidict/_multidict.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ _multidict_extend(MultiDictObject *self, PyObject *arg,
}


static inline int
static inline Py_ssize_t
_multidict_extend_parse_args(PyObject *args, PyObject *kwds,
const char *name, PyObject **parg)
{
Expand Down Expand Up @@ -344,85 +344,13 @@ multidict_reduce(MultiDictObject *self)
return result;
}

static inline PyObject *
_do_multidict_repr(MultiDictObject *md, PyObject *name,
bool show_keys, bool show_values)
{
PyObject *key = NULL,
*value = NULL;
bool comma = false;

PyUnicodeWriter *writer = PyUnicodeWriter_Create(1024);
if (writer == NULL)
return NULL;

if (PyUnicodeWriter_WriteChar(writer, '<') <0)
goto fail;
if (PyUnicodeWriter_WriteStr(writer, name) <0)
goto fail;
if (PyUnicodeWriter_WriteChar(writer, '(') <0)
goto fail;

pair_list_pos_t pos;
pair_list_init_pos(&md->pairs, &pos);

for (;;) {
int res = pair_list_next(&md->pairs, &pos, &key, &value);
if (res < 0) {
goto fail;
}
if (res == 0) {
break;
}

if (comma) {
if (PyUnicodeWriter_WriteChar(writer, ',') <0)
goto fail;
if (PyUnicodeWriter_WriteChar(writer, ' ') <0)
goto fail;
}
if (show_keys) {
if (PyUnicodeWriter_WriteChar(writer, '\'') <0)
goto fail;
if (PyUnicodeWriter_WriteStr(writer, key) <0)
goto fail;
if (PyUnicodeWriter_WriteChar(writer, '\'') <0)
goto fail;
}
if (show_keys && show_values) {
if (PyUnicodeWriter_WriteChar(writer, ':') <0)
goto fail;
if (PyUnicodeWriter_WriteChar(writer, ' ') <0)
goto fail;
}
if (show_values) {
if (PyUnicodeWriter_WriteRepr(writer, value) <0)
goto fail;
}

Py_CLEAR(key);
Py_CLEAR(value);
comma = true;
}

if (PyUnicodeWriter_WriteChar(writer, ')') <0)
goto fail;
if (PyUnicodeWriter_WriteChar(writer, '>') <0)
goto fail;
return PyUnicodeWriter_Finish(writer);
fail:
Py_CLEAR(key);
Py_CLEAR(value);
PyUnicodeWriter_Discard(writer);
}

static inline PyObject *
multidict_repr(MultiDictObject *self)
{
PyObject *name = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "__name__");
if (name == NULL)
return NULL;
PyObject *ret = _do_multidict_repr(self, name, true, true);
PyObject *ret = pair_list_repr(&self->pairs, name, true, true);
Py_CLEAR(name);
return ret;
}
Expand Down Expand Up @@ -604,9 +532,11 @@ static inline PyObject *
multidict_extend(MultiDictObject *self, PyObject *args, PyObject *kwds)
{
PyObject *arg = NULL;
if (_multidict_extend_parse_args(args, kwds, "extend", &arg) < 0) {
Py_ssize_t size = _multidict_extend_parse_args(args, kwds, "extend", &arg);
if (size < 0) {
return NULL;
}
pair_list_grow(&self->pairs, size);
if (_multidict_extend(self, arg, kwds, "extend", 1) < 0) {
return NULL;
}
Expand Down Expand Up @@ -1200,7 +1130,7 @@ multidict_proxy_repr(MultiDictProxyObject *self)
PyObject *name = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "__name__");
if (name == NULL)
return NULL;
PyObject *ret = _do_multidict_repr(self->md, name, true, true);
PyObject *ret = pair_list_repr(&self->md->pairs, name, true, true);
Py_CLEAR(name);
return ret;
}
Expand Down Expand Up @@ -1412,6 +1342,7 @@ static inline void
module_free(void *m)
{
Py_CLEAR(multidict_str_lower);
Py_CLEAR(multidict_str_canonical);
Py_CLEAR(viewbaseset_and_func);
Py_CLEAR(viewbaseset_or_func);
Py_CLEAR(viewbaseset_sub_func);
Expand All @@ -1438,6 +1369,10 @@ PyInit__multidict(void)
if (multidict_str_lower == NULL) {
goto fail;
}
multidict_str_canonical = PyUnicode_InternFromString("_canonical");
if (multidict_str_canonical == NULL) {
goto fail;
}

PyObject *module = NULL;

Expand Down Expand Up @@ -1531,6 +1466,7 @@ PyInit__multidict(void)

fail:
Py_XDECREF(multidict_str_lower);
Py_XDECREF(multidict_str_canonical);

return NULL;
}
Loading
Loading