Commit e0a0d6af authored by Stefan Behnel's avatar Stefan Behnel

optimise string concatenation for 2-item f-strings

parent 3937bfcc
...@@ -3023,6 +3023,10 @@ class FormattedValueNode(ExprNode): ...@@ -3023,6 +3023,10 @@ class FormattedValueNode(ExprNode):
'a': 'PyObject_ASCII', # NOTE: Py3-only! 'a': 'PyObject_ASCII', # NOTE: Py3-only!
}.get }.get
def may_be_none(self):
# PyObject_Format() always returns a string (str in Py3, str/unicode in Py2) or raises an exception
return False
def analyse_types(self, env): def analyse_types(self, env):
self.value = self.value.analyse_types(env).coerce_to_pyobject(env) self.value = self.value.analyse_types(env).coerce_to_pyobject(env)
if self.format_spec: if self.format_spec:
...@@ -10592,9 +10596,15 @@ class AddNode(NumBinopNode): ...@@ -10592,9 +10596,15 @@ class AddNode(NumBinopNode):
self, type1, type2) self, type1, type2)
def py_operation_function(self, code): def py_operation_function(self, code):
is_unicode_concat = False
if isinstance(self.operand1, FormattedValueNode) or isinstance(self.operand2, FormattedValueNode):
is_unicode_concat = True
else:
type1, type2 = self.operand1.type, self.operand2.type type1, type2 = self.operand1.type, self.operand2.type
if type1 is unicode_type or type2 is unicode_type: if type1 is unicode_type or type2 is unicode_type:
if type1.is_builtin_type and type2.is_builtin_type: is_unicode_concat = type1.is_builtin_type and type2.is_builtin_type
if is_unicode_concat:
if self.operand1.may_be_none() or self.operand2.may_be_none(): if self.operand1.may_be_none() or self.operand2.may_be_none():
return '__Pyx_PyUnicode_ConcatSafe' return '__Pyx_PyUnicode_ConcatSafe'
else: else:
......
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