Commit 45da07ea authored by Robert Bradshaw's avatar Robert Bradshaw

Optimize printing of one object.

parent eaae26b8
...@@ -3200,14 +3200,24 @@ class PrintStatNode(StatNode): ...@@ -3200,14 +3200,24 @@ class PrintStatNode(StatNode):
gil_message = "Python print statement" gil_message = "Python print statement"
def generate_execution_code(self, code): def generate_execution_code(self, code):
self.arg_tuple.generate_evaluation_code(code) if len(self.arg_tuple.args) == 1 and self.append_newline:
code.putln( arg = self.arg_tuple.args[0]
"if (__Pyx_Print(%s, %d) < 0) %s" % ( arg.generate_evaluation_code(code)
self.arg_tuple.py_result(), code.putln(
self.append_newline, "if (__Pyx_PrintOne(%s) < 0) %s" % (
code.error_goto(self.pos))) arg.py_result(),
self.arg_tuple.generate_disposal_code(code) code.error_goto(self.pos)))
self.arg_tuple.free_temps(code) arg.generate_disposal_code(code)
arg.free_temps(code)
else:
self.arg_tuple.generate_evaluation_code(code)
code.putln(
"if (__Pyx_Print(%s, %d) < 0) %s" % (
self.arg_tuple.py_result(),
self.append_newline,
code.error_goto(self.pos)))
self.arg_tuple.generate_disposal_code(code)
self.arg_tuple.free_temps(code)
def annotate(self, code): def annotate(self, code):
self.arg_tuple.annotate(code) self.arg_tuple.annotate(code)
...@@ -4746,6 +4756,7 @@ else: ...@@ -4746,6 +4756,7 @@ else:
printing_utility_code = UtilityCode( printing_utility_code = UtilityCode(
proto = """ proto = """
static int __Pyx_Print(PyObject *, int); /*proto*/ static int __Pyx_Print(PyObject *, int); /*proto*/
static int __Pyx_PrintOne(PyObject *o); /*proto*/
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3
static PyObject* %s = 0; static PyObject* %s = 0;
static PyObject* %s = 0; static PyObject* %s = 0;
...@@ -4793,7 +4804,23 @@ static int __Pyx_Print(PyObject *arg_tuple, int newline) { ...@@ -4793,7 +4804,23 @@ static int __Pyx_Print(PyObject *arg_tuple, int newline) {
return 0; return 0;
} }
static int __Pyx_PrintOne(PyObject *o) {
PyObject *f;
if (!(f = __Pyx_GetStdout()))
return -1;
if (PyFile_SoftSpace(f, 0)) {
if (PyFile_WriteString(" ", f) < 0)
return -1;
}
if (PyFile_WriteObject(o, f, Py_PRINT_RAW) < 0)
return -1;
if (PyFile_WriteString("\n", f) < 0)
return -1;
return 0;
}
#else /* Python 3 has a print function */ #else /* Python 3 has a print function */
static int __Pyx_Print(PyObject *arg_tuple, int newline) { static int __Pyx_Print(PyObject *arg_tuple, int newline) {
PyObject* kwargs = 0; PyObject* kwargs = 0;
PyObject* result = 0; PyObject* result = 0;
...@@ -4825,6 +4852,19 @@ static int __Pyx_Print(PyObject *arg_tuple, int newline) { ...@@ -4825,6 +4852,19 @@ static int __Pyx_Print(PyObject *arg_tuple, int newline) {
Py_DECREF(result); Py_DECREF(result);
return 0; return 0;
} }
static int __Pyx_PrintOne(PyObject *o) {
int res;
PyObject* arg_tuple = PyTuple_New(1);
if (unlikely(!arg_tuple))
return -1;
Py_INCREF(o);
PyTuple_SET_ITEM(arg_tuple, 0, o);
res = __Pyx_Print(arg_tuple, 1);
Py_DECREF(arg_tuple);
return res;
}
#endif #endif
""" % {'BUILTINS' : Naming.builtins_cname, """ % {'BUILTINS' : Naming.builtins_cname,
'PRINT_FUNCTION' : Naming.print_function, 'PRINT_FUNCTION' : Naming.print_function,
......
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