Commit 4293dbdc authored by Stefan Behnel's avatar Stefan Behnel

Avoid useless unicode string formatting of known unicode strings.

Pass "never None" property of decoded byte strings through to the code that uses them.
parent a7bb88a5
......@@ -2337,6 +2337,18 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
utility_code=utility_code,
py_name="unicode")
def visit_FormattedValueNode(self, node):
"""Simplify or avoid plain string formatting of a unicode value.
This seems misplaced here, but plain unicode formatting is essentially
a call to the unicode() builtin, which is optimised right above.
"""
self.visitchildren(node)
if node.value.type is Builtin.unicode_type and not node.c_format_spec and not node.format_spec:
if not node.conversion_char or node.conversion_char == 's':
# value is definitely a unicode string and we don't format it any special
return self._handle_simple_function_unicode(node, None, [node.value])
return node
PyDict_Copy_func_type = PyrexTypes.CFuncType(
Builtin.dict_type, [
PyrexTypes.CFuncTypeArg("dict", Builtin.dict_type, None)
......@@ -4584,6 +4596,7 @@ class FinalOptimizePhase(Visitor.CythonTransform, Visitor.NodeRefCleanupMixin):
- eliminate None assignment and refcounting for first assignment.
- isinstance -> typecheck for cdef types
- eliminate checks for None and/or types that became redundant after tree changes
- eliminate useless string formatting steps
- replace Python function calls that look like method calls by a faster PyMethodCallNode
"""
def visit_SingleAssignmentNode(self, node):
......@@ -4664,6 +4677,7 @@ class FinalOptimizePhase(Visitor.CythonTransform, Visitor.NodeRefCleanupMixin):
return node.arg
return node
class ConsolidateOverflowCheck(Visitor.CythonTransform):
"""
This class facilitates the sharing of overflow checking among all nodes
......
......@@ -267,6 +267,9 @@ class EvalWithTempExprNode(ExprNodes.ExprNode, LetNodeMixin):
def infer_type(self, env):
return self.subexpression.infer_type(env)
def may_be_none(self):
return self.subexpression.may_be_none()
def result(self):
return self.subexpression.result()
......
......@@ -431,3 +431,16 @@ def format_str(value):
b = f'x{value!s:6}x'
assert isinstance(b, unicode), type(b)
return a, b
@cython.test_fail_if_path_exists(
"//FormattedValueNode", # bytes.decode() returns unicode => formatting is useless
"//JoinedStrNode", # replaced by call to PyUnicode_Concat()
"//PythonCapiCallNode//PythonCapiCallNode",
)
def format_decoded_bytes(bytes value):
"""
>>> print(format_decoded_bytes(b'xyz'))
U-xyz
"""
return f"U-{value.decode('utf-8')}"
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