Commit 8d707e05 authored by Stefan Behnel's avatar Stefan Behnel

switch to using PySet_GET_SIZE() for set size and truth requests as it's fast...

switch to using PySet_GET_SIZE() for set size and truth requests as it's fast and also works as expected in PyPy
parent 35acc9d6
...@@ -11481,11 +11481,13 @@ class CoerceToBooleanNode(CoercionNode): ...@@ -11481,11 +11481,13 @@ class CoerceToBooleanNode(CoercionNode):
type = PyrexTypes.c_bint_type type = PyrexTypes.c_bint_type
_special_builtins = { _special_builtins = {
Builtin.list_type : 'PyList_GET_SIZE', Builtin.list_type: 'PyList_GET_SIZE',
Builtin.tuple_type : 'PyTuple_GET_SIZE', Builtin.tuple_type: 'PyTuple_GET_SIZE',
Builtin.bytes_type : 'PyBytes_GET_SIZE', Builtin.set_type: 'PySet_GET_SIZE',
Builtin.unicode_type : 'PyUnicode_GET_SIZE', Builtin.frozenset_type: 'PySet_GET_SIZE',
} Builtin.bytes_type: 'PyBytes_GET_SIZE',
Builtin.unicode_type: 'PyUnicode_GET_SIZE',
}
def __init__(self, arg, env): def __init__(self, arg, env):
CoercionNode.__init__(self, arg) CoercionNode.__init__(self, arg)
...@@ -11524,6 +11526,7 @@ class CoerceToBooleanNode(CoercionNode): ...@@ -11524,6 +11526,7 @@ class CoerceToBooleanNode(CoercionNode):
self.arg.py_result(), self.arg.py_result(),
code.error_goto_if_neg(self.result(), self.pos))) code.error_goto_if_neg(self.result(), self.pos)))
class CoerceToComplexNode(CoercionNode): class CoerceToComplexNode(CoercionNode):
def __init__(self, arg, dst_type, env): def __init__(self, arg, dst_type, env):
......
...@@ -2324,14 +2324,14 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin, ...@@ -2324,14 +2324,14 @@ class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
exception_value="-1") exception_value="-1")
_map_to_capi_len_function = { _map_to_capi_len_function = {
Builtin.unicode_type : "__Pyx_PyUnicode_GET_LENGTH", Builtin.unicode_type: "__Pyx_PyUnicode_GET_LENGTH",
Builtin.bytes_type : "PyBytes_GET_SIZE", Builtin.bytes_type: "PyBytes_GET_SIZE",
Builtin.list_type : "PyList_GET_SIZE", Builtin.list_type: "PyList_GET_SIZE",
Builtin.tuple_type : "PyTuple_GET_SIZE", Builtin.tuple_type: "PyTuple_GET_SIZE",
Builtin.dict_type : "PyDict_Size", Builtin.set_type: "PySet_GET_SIZE",
Builtin.set_type : "PySet_Size", Builtin.frozenset_type: "PySet_GET_SIZE",
Builtin.frozenset_type : "__Pyx_PyFrozenSet_Size", Builtin.dict_type: "PyDict_Size",
}.get }.get
_ext_types_with_pysize = set(["cpython.array.array"]) _ext_types_with_pysize = set(["cpython.array.array"])
......
...@@ -100,13 +100,10 @@ ...@@ -100,13 +100,10 @@
#if CYTHON_COMPILING_IN_PYPY #if CYTHON_COMPILING_IN_PYPY
#define __Pyx_PyUnicode_Concat(a, b) PyNumber_Add(a, b) #define __Pyx_PyUnicode_Concat(a, b) PyNumber_Add(a, b)
#define __Pyx_PyUnicode_ConcatSafe(a, b) PyNumber_Add(a, b) #define __Pyx_PyUnicode_ConcatSafe(a, b) PyNumber_Add(a, b)
// PyPy doesn't handle frozenset() in PySet_Size()
#define __Pyx_PyFrozenSet_Size(s) PyObject_Size(s)
#else #else
#define __Pyx_PyUnicode_Concat(a, b) PyUnicode_Concat(a, b) #define __Pyx_PyUnicode_Concat(a, b) PyUnicode_Concat(a, b)
#define __Pyx_PyUnicode_ConcatSafe(a, b) ((unlikely((a) == Py_None) || unlikely((b) == Py_None)) ? \ #define __Pyx_PyUnicode_ConcatSafe(a, b) ((unlikely((a) == Py_None) || unlikely((b) == Py_None)) ? \
PyNumber_Add(a, b) : __Pyx_PyUnicode_Concat(a, b)) PyNumber_Add(a, b) : __Pyx_PyUnicode_Concat(a, b))
#define __Pyx_PyFrozenSet_Size(s) PySet_Size(s)
#endif #endif
#define __Pyx_PyString_FormatSafe(a, b) ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b)) #define __Pyx_PyString_FormatSafe(a, b) ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b))
......
...@@ -10,6 +10,7 @@ def bool_list(list obj): ...@@ -10,6 +10,7 @@ def bool_list(list obj):
""" """
return bool(obj) return bool(obj)
def if_list(list obj): def if_list(list obj):
""" """
>>> if_list( [] ) >>> if_list( [] )
...@@ -24,6 +25,7 @@ def if_list(list obj): ...@@ -24,6 +25,7 @@ def if_list(list obj):
else: else:
return False return False
def if_list_nogil(list obj): def if_list_nogil(list obj):
""" """
>>> if_list_nogil( [] ) >>> if_list_nogil( [] )
...@@ -41,6 +43,7 @@ def if_list_nogil(list obj): ...@@ -41,6 +43,7 @@ def if_list_nogil(list obj):
result = False result = False
return result return result
def if_list_literal(t): def if_list_literal(t):
""" """
>>> if_list_literal(True) >>> if_list_literal(True)
...@@ -71,6 +74,7 @@ def bool_tuple(tuple obj): ...@@ -71,6 +74,7 @@ def bool_tuple(tuple obj):
""" """
return bool(obj) return bool(obj)
def if_tuple(tuple obj): def if_tuple(tuple obj):
""" """
>>> if_tuple( () ) >>> if_tuple( () )
...@@ -85,6 +89,7 @@ def if_tuple(tuple obj): ...@@ -85,6 +89,7 @@ def if_tuple(tuple obj):
else: else:
return False return False
def if_tuple_literal(t): def if_tuple_literal(t):
""" """
>>> if_tuple_literal(True) >>> if_tuple_literal(True)
...@@ -104,6 +109,97 @@ def if_tuple_literal(t): ...@@ -104,6 +109,97 @@ def if_tuple_literal(t):
return False return False
def bool_set(set obj):
"""
>>> bool_set( set() )
False
>>> bool_set( set([1]) )
True
>>> bool_set(None)
False
"""
return bool(obj)
def if_set(set obj):
"""
>>> if_set( set() )
False
>>> if_set( set([1]) )
True
>>> if_set(None)
False
"""
if obj:
return True
else:
return False
def if_set_nogil(set obj):
"""
>>> if_set_nogil( set() )
False
>>> if_set_nogil( set([1]) )
True
>>> if_set_nogil(None)
False
"""
cdef bint result
with nogil:
if obj:
result = True
else:
result = False
return result
def if_set_literal(t):
"""
>>> if_set_literal(True)
True
>>> if_set_literal(False)
False
"""
if t:
if {1,2,3}:
return True
else:
return False
else:
if set():
return True
else:
return False
def bool_frozenset(frozenset obj):
"""
>>> bool_frozenset( frozenset() )
False
>>> bool_frozenset( frozenset([1]) )
True
>>> bool_frozenset(None)
False
"""
return bool(obj)
def if_frozenset(frozenset obj):
"""
>>> if_frozenset( frozenset() )
False
>>> if_frozenset( frozenset([1]) )
True
>>> if_frozenset(None)
False
"""
if obj:
return True
else:
return False
b0 = b'' b0 = b''
b1 = b'abc' b1 = b'abc'
......
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