Commit b36e1104 authored by Stefan Behnel's avatar Stefan Behnel

Avoid redundant type check after calling "ext_type.__new__()" (and in some other cases).

parent 5af807ff
...@@ -51,6 +51,8 @@ Features added ...@@ -51,6 +51,8 @@ Features added
* Single argument function calls can avoid the argument tuple creation in some cases. * Single argument function calls can avoid the argument tuple creation in some cases.
* Some redundant extension type checks are avoided.
* Formatting C enum values in f-strings is faster, as well as some other special cases. * Formatting C enum values in f-strings is faster, as well as some other special cases.
* Subscripting (item access) is faster in some cases. * Subscripting (item access) is faster in some cases.
......
...@@ -12894,6 +12894,15 @@ class PyTypeTestNode(CoercionNode): ...@@ -12894,6 +12894,15 @@ class PyTypeTestNode(CoercionNode):
def nonlocally_immutable(self): def nonlocally_immutable(self):
return self.arg.nonlocally_immutable() return self.arg.nonlocally_immutable()
def reanalyse(self):
if self.type != self.arg.type or not self.arg.is_temp:
return self
if not self.type.typeobj_is_available():
return self
if self.arg.may_be_none() and self.notnone:
return self.arg.as_none_safe_node("Cannot convert NoneType to %.200s" % self.type.name)
return self.arg
def calculate_constant_result(self): def calculate_constant_result(self):
# FIXME # FIXME
pass pass
......
...@@ -2024,16 +2024,11 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin, ...@@ -2024,16 +2024,11 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
""" """
### cleanup to avoid redundant coercions to/from Python types ### cleanup to avoid redundant coercions to/from Python types
def _visit_PyTypeTestNode(self, node): def visit_PyTypeTestNode(self, node):
# disabled - appears to break assignments in some cases, and
# also drops a None check, which might still be required
"""Flatten redundant type checks after tree changes. """Flatten redundant type checks after tree changes.
""" """
old_arg = node.arg
self.visitchildren(node) self.visitchildren(node)
if old_arg is node.arg or node.arg.type != node.type: return node.reanalyse()
return node
return node.arg
def _visit_TypecastNode(self, node): def _visit_TypecastNode(self, node):
# disabled - the user may have had a reason to put a type # disabled - the user may have had a reason to put a type
...@@ -2804,7 +2799,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin, ...@@ -2804,7 +2799,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
PyTypeObjectPtr = PyrexTypes.CPtrType( PyTypeObjectPtr = PyrexTypes.CPtrType(
cython_scope.lookup('PyTypeObject').type) cython_scope.lookup('PyTypeObject').type)
pyx_tp_new_kwargs_func_type = PyrexTypes.CFuncType( pyx_tp_new_kwargs_func_type = PyrexTypes.CFuncType(
PyrexTypes.py_object_type, [ ext_type, [
PyrexTypes.CFuncTypeArg("type", PyTypeObjectPtr, None), PyrexTypes.CFuncTypeArg("type", PyTypeObjectPtr, None),
PyrexTypes.CFuncTypeArg("args", PyrexTypes.py_object_type, None), PyrexTypes.CFuncTypeArg("args", PyrexTypes.py_object_type, None),
PyrexTypes.CFuncTypeArg("kwargs", PyrexTypes.py_object_type, None), PyrexTypes.CFuncTypeArg("kwargs", PyrexTypes.py_object_type, None),
...@@ -2817,6 +2812,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin, ...@@ -2817,6 +2812,7 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
node.pos, slot_func_cname, node.pos, slot_func_cname,
pyx_tp_new_kwargs_func_type, pyx_tp_new_kwargs_func_type,
args=[type_arg, args_tuple, kwargs], args=[type_arg, args_tuple, kwargs],
may_return_none=False,
is_temp=True) is_temp=True)
else: else:
# arbitrary variable, needs a None check for safety # arbitrary variable, needs a None check for safety
......
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