Commit f1744511 authored by Stefan Behnel's avatar Stefan Behnel

simplify dict.clear() optimisation

parent ee471d52
...@@ -457,6 +457,8 @@ builtin_types_table = [ ...@@ -457,6 +457,8 @@ builtin_types_table = [
("dict", "PyDict_Type", [BuiltinMethod("items", "T", "O", "PyDict_Items"), # FIXME: Py3 mode? ("dict", "PyDict_Type", [BuiltinMethod("items", "T", "O", "PyDict_Items"), # FIXME: Py3 mode?
BuiltinMethod("keys", "T", "O", "PyDict_Keys"), # FIXME: Py3 mode? BuiltinMethod("keys", "T", "O", "PyDict_Keys"), # FIXME: Py3 mode?
BuiltinMethod("values","T", "O", "PyDict_Values"), # FIXME: Py3 mode? BuiltinMethod("values","T", "O", "PyDict_Values"), # FIXME: Py3 mode?
BuiltinMethod("clear", "T", "r", "__Pyx_PyDict_Clear",
utility_code = UtilityCode.load_cached("py_dict_clear", "Optimize.c")),
BuiltinMethod("copy", "T", "T", "PyDict_Copy")]), BuiltinMethod("copy", "T", "T", "PyDict_Copy")]),
("slice", "PySlice_Type", [BuiltinAttribute('start'), ("slice", "PySlice_Type", [BuiltinAttribute('start'),
......
...@@ -2288,34 +2288,6 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform): ...@@ -2288,34 +2288,6 @@ class OptimizeBuiltinCalls(Visitor.EnvTransform):
### methods of builtin types ### methods of builtin types
PyDict_Clear_func_type = PyrexTypes.CFuncType(
PyrexTypes.c_void_type, [
PyrexTypes.CFuncTypeArg("dict", Builtin.dict_type, None)
])
PyDict_Clear_Retval_func_type = PyrexTypes.CFuncType(
PyrexTypes.py_object_type, [
PyrexTypes.CFuncTypeArg("dict", Builtin.dict_type, None)
])
def _handle_simple_method_dict_clear(self, node, args, is_unbound_method):
"""Optimise dict.clear() differently, depending on the use (or
non-use) of the return value.
"""
if len(args) != 1:
return node
if node.result_is_used:
return self._substitute_method_call(
node, "__Pyx_PyDict_Clear", self.PyDict_Clear_Retval_func_type,
'clear', is_unbound_method, args,
may_return_none=True, is_temp=True,
utility_code=load_c_utility('py_dict_clear')
).coerce_to(node.type, self.current_env)
else:
return self._substitute_method_call(
node, "PyDict_Clear", self.PyDict_Clear_func_type,
'clear', is_unbound_method, args, is_temp=False)
PyObject_Append_func_type = PyrexTypes.CFuncType( PyObject_Append_func_type = PyrexTypes.CFuncType(
PyrexTypes.py_object_type, [ PyrexTypes.py_object_type, [
PyrexTypes.CFuncTypeArg("list", PyrexTypes.py_object_type, None), PyrexTypes.CFuncTypeArg("list", PyrexTypes.py_object_type, None),
......
...@@ -309,11 +309,7 @@ static PyObject *__Pyx_PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *d ...@@ -309,11 +309,7 @@ static PyObject *__Pyx_PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *d
/////////////// py_dict_clear.proto /////////////// /////////////// py_dict_clear.proto ///////////////
static CYTHON_INLINE PyObject* __Pyx_PyDict_Clear(PyObject* d) { #define __Pyx_PyDict_Clear(d) (PyDict_Clear(d), 0)
PyDict_Clear(d);
Py_INCREF(Py_None);
return Py_None;
}
/////////////// dict_iter.proto /////////////// /////////////// dict_iter.proto ///////////////
......
...@@ -68,6 +68,9 @@ def set_clear(set s): ...@@ -68,6 +68,9 @@ def set_clear(set s):
""" """
>>> set_clear(_set([1,2,3])) is None >>> set_clear(_set([1,2,3])) is None
True True
>>> set_clear(None)
Traceback (most recent call last):
AttributeError: 'NoneType' object has no attribute 'clear'
""" """
s.clear() s.clear()
return s.clear() return s.clear()
...@@ -92,6 +95,9 @@ def dict_clear(dict d): ...@@ -92,6 +95,9 @@ def dict_clear(dict d):
""" """
>>> dict_clear({1:2,3:4}) is None >>> dict_clear({1:2,3:4}) is None
True True
>>> dict_clear(None)
Traceback (most recent call last):
AttributeError: 'NoneType' object has no attribute 'clear'
""" """
d.clear() d.clear()
return d.clear() return d.clear()
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