cython-devel

changeset 1785:1d797805d2b9

Optimize printing of one object.
author Robert Bradshaw <robertwb@math.washington.edu>
date Thu Feb 26 15:55:04 2009 -0800 (2 years ago)
parents 0ffeb158969e
children f6132dddabe1fe2e968224a3
files Cython/Compiler/Nodes.py
line diff
1.1 --- a/Cython/Compiler/Nodes.py Thu Feb 26 15:08:44 2009 -0800 1.2 +++ b/Cython/Compiler/Nodes.py Thu Feb 26 15:55:04 2009 -0800 1.3 @@ -3200,14 +3200,24 @@ 1.4 gil_message = "Python print statement" 1.5 1.6 def generate_execution_code(self, code): 1.7 - self.arg_tuple.generate_evaluation_code(code) 1.8 - code.putln( 1.9 - "if (__Pyx_Print(%s, %d) < 0) %s" % ( 1.10 - self.arg_tuple.py_result(), 1.11 - self.append_newline, 1.12 - code.error_goto(self.pos))) 1.13 - self.arg_tuple.generate_disposal_code(code) 1.14 - self.arg_tuple.free_temps(code) 1.15 + if len(self.arg_tuple.args) == 1 and self.append_newline: 1.16 + arg = self.arg_tuple.args[0] 1.17 + arg.generate_evaluation_code(code) 1.18 + code.putln( 1.19 + "if (__Pyx_PrintOne(%s) < 0) %s" % ( 1.20 + arg.py_result(), 1.21 + code.error_goto(self.pos))) 1.22 + arg.generate_disposal_code(code) 1.23 + arg.free_temps(code) 1.24 + else: 1.25 + self.arg_tuple.generate_evaluation_code(code) 1.26 + code.putln( 1.27 + "if (__Pyx_Print(%s, %d) < 0) %s" % ( 1.28 + self.arg_tuple.py_result(), 1.29 + self.append_newline, 1.30 + code.error_goto(self.pos))) 1.31 + self.arg_tuple.generate_disposal_code(code) 1.32 + self.arg_tuple.free_temps(code) 1.33 1.34 def annotate(self, code): 1.35 self.arg_tuple.annotate(code) 1.36 @@ -4746,6 +4756,7 @@ 1.37 printing_utility_code = UtilityCode( 1.38 proto = """ 1.39 static int __Pyx_Print(PyObject *, int); /*proto*/ 1.40 +static int __Pyx_PrintOne(PyObject *o); /*proto*/ 1.41 #if PY_MAJOR_VERSION >= 3 1.42 static PyObject* %s = 0; 1.43 static PyObject* %s = 0; 1.44 @@ -4793,7 +4804,23 @@ 1.45 return 0; 1.46 } 1.47 1.48 +static int __Pyx_PrintOne(PyObject *o) { 1.49 + PyObject *f; 1.50 + if (!(f = __Pyx_GetStdout())) 1.51 + return -1; 1.52 + if (PyFile_SoftSpace(f, 0)) { 1.53 + if (PyFile_WriteString(" ", f) < 0) 1.54 + return -1; 1.55 + } 1.56 + if (PyFile_WriteObject(o, f, Py_PRINT_RAW) < 0) 1.57 + return -1; 1.58 + if (PyFile_WriteString("\n", f) < 0) 1.59 + return -1; 1.60 + return 0; 1.61 +} 1.62 + 1.63 #else /* Python 3 has a print function */ 1.64 + 1.65 static int __Pyx_Print(PyObject *arg_tuple, int newline) { 1.66 PyObject* kwargs = 0; 1.67 PyObject* result = 0; 1.68 @@ -4825,6 +4852,19 @@ 1.69 Py_DECREF(result); 1.70 return 0; 1.71 } 1.72 + 1.73 +static int __Pyx_PrintOne(PyObject *o) { 1.74 + int res; 1.75 + PyObject* arg_tuple = PyTuple_New(1); 1.76 + if (unlikely(!arg_tuple)) 1.77 + return -1; 1.78 + Py_INCREF(o); 1.79 + PyTuple_SET_ITEM(arg_tuple, 0, o); 1.80 + res = __Pyx_Print(arg_tuple, 1); 1.81 + Py_DECREF(arg_tuple); 1.82 + return res; 1.83 +} 1.84 + 1.85 #endif 1.86 """ % {'BUILTINS' : Naming.builtins_cname, 1.87 'PRINT_FUNCTION' : Naming.print_function,