Skip to content

Commit 9b108fb

Browse files
committed
gh-150942: Speed up Py_BuildValue dict construction
1 parent 65e149a commit 9b108fb

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

Python/modsupport.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "Python.h"
55
#include "pycore_abstract.h" // _PyIndex_Check()
6+
#include "pycore_dict.h" // _PyDict_SetItem_Take2()
67
#include "pycore_object.h" // _PyType_IsReady()
78
#include "pycore_unicodeobject.h" // _PyUnicodeWriter_FormatV()
89

@@ -174,15 +175,17 @@ do_mkdict(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n)
174175
return NULL;
175176
}
176177
v = do_mkvalue(p_format, p_va);
177-
if (v == NULL || PyDict_SetItem(d, k, v) < 0) {
178+
if (v == NULL) {
178179
do_ignore(p_format, p_va, endchar, n - i - 2);
179180
Py_DECREF(k);
180-
Py_XDECREF(v);
181181
Py_DECREF(d);
182182
return NULL;
183183
}
184-
Py_DECREF(k);
185-
Py_DECREF(v);
184+
if (_PyDict_SetItem_Take2((PyDictObject *)d, k, v) < 0) {
185+
do_ignore(p_format, p_va, endchar, n - i - 2);
186+
Py_DECREF(d);
187+
return NULL;
188+
}
186189
}
187190
if (!check_end(p_format, endchar)) {
188191
Py_DECREF(d);

0 commit comments

Comments
 (0)