Commit c132ad9c authored by Stefan Behnel's avatar Stefan Behnel

Avoid dict resizing while creating fixed-size dicts.

parent 3fdd71a9
...@@ -8471,8 +8471,9 @@ class DictNode(ExprNode): ...@@ -8471,8 +8471,9 @@ class DictNode(ExprNode):
if is_dict: if is_dict:
self.release_errors() self.release_errors()
code.putln( code.putln(
"%s = PyDict_New(); %s" % ( "%s = __Pyx_PyDict_NewPresized(%d); %s" % (
self.result(), self.result(),
len(self.key_value_pairs),
code.error_goto_if_null(self.result(), self.pos))) code.error_goto_if_null(self.result(), self.pos)))
code.put_gotref(self.py_result()) code.put_gotref(self.py_result())
......
...@@ -3167,7 +3167,8 @@ class ToPyStructUtilityCode(object): ...@@ -3167,7 +3167,8 @@ class ToPyStructUtilityCode(object):
code.putln("%s {" % self.header) code.putln("%s {" % self.header)
code.putln("PyObject* res;") code.putln("PyObject* res;")
code.putln("PyObject* member;") code.putln("PyObject* member;")
code.putln("res = PyDict_New(); if (unlikely(!res)) return NULL;") code.putln("res = __Pyx_PyDict_NewPresized(%d); if (unlikely(!res)) return NULL;" %
len(self.type.scope.var_entries))
for member in self.type.scope.var_entries: for member in self.type.scope.var_entries:
nameconst_cname = code.get_py_string_const(member.name, identifier=True) nameconst_cname = code.get_py_string_const(member.name, identifier=True)
code.putln("%s; if (unlikely(!member)) goto bad;" % ( code.putln("%s; if (unlikely(!member)) goto bad;" % (
......
...@@ -1742,7 +1742,7 @@ static PyObject* __Pyx_Coroutine_patch_module(PyObject* module, const char* py_c ...@@ -1742,7 +1742,7 @@ static PyObject* __Pyx_Coroutine_patch_module(PyObject* module, const char* py_c
#if defined(__Pyx_Generator_USED) || defined(__Pyx_Coroutine_USED) #if defined(__Pyx_Generator_USED) || defined(__Pyx_Coroutine_USED)
int result; int result;
PyObject *globals, *result_obj; PyObject *globals, *result_obj;
globals = PyDict_New(); if (unlikely(!globals)) goto ignore; globals = __Pyx_PyDict_NewPresized(4); if (unlikely(!globals)) goto ignore;
result = PyDict_SetItemString(globals, "_cython_coroutine_type", result = PyDict_SetItemString(globals, "_cython_coroutine_type",
#ifdef __Pyx_Coroutine_USED #ifdef __Pyx_Coroutine_USED
(PyObject*)__pyx_CoroutineType); (PyObject*)__pyx_CoroutineType);
......
...@@ -255,6 +255,12 @@ ...@@ -255,6 +255,12 @@
#define __Pyx_PyThreadState_Current _PyThreadState_Current #define __Pyx_PyThreadState_Current _PyThreadState_Current
#endif #endif
#if CYTHON_COMPILING_IN_CPYTHON || defined(_PyDict_NewPresized)
#define __Pyx_PyDict_NewPresized(n) _PyDict_NewPresized(n)
#else
#define __Pyx_PyDict_NewPresized(n) PyDict_New()
#endif
#if PY_MAJOR_VERSION >= 3 || CYTHON_FUTURE_DIVISION #if PY_MAJOR_VERSION >= 3 || CYTHON_FUTURE_DIVISION
#define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y) #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)
#define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y) #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)
......
...@@ -83,7 +83,7 @@ static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) { ...@@ -83,7 +83,7 @@ static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) {
return -1; return -1;
} }
if (stream) { if (stream) {
kwargs = PyDict_New(); kwargs = __Pyx_PyDict_NewPresized(2);
if (unlikely(!kwargs)) if (unlikely(!kwargs))
return -1; return -1;
if (unlikely(PyDict_SetItem(kwargs, PYIDENT("file"), stream) < 0)) if (unlikely(PyDict_SetItem(kwargs, PYIDENT("file"), stream) < 0))
...@@ -100,7 +100,7 @@ static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) { ...@@ -100,7 +100,7 @@ static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) {
} }
} else if (!newline) { } else if (!newline) {
if (unlikely(!$print_function_kwargs)) { if (unlikely(!$print_function_kwargs)) {
$print_function_kwargs = PyDict_New(); $print_function_kwargs = __Pyx_PyDict_NewPresized(1);
if (unlikely(!$print_function_kwargs)) if (unlikely(!$print_function_kwargs))
return -1; return -1;
end_string = PyUnicode_FromStringAndSize(" ", 1); end_string = PyUnicode_FromStringAndSize(" ", 1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment