Replace __builtin_expect macros with LIKELY/UNLIKELY

This commit is contained in:
rdb 2017-10-09 00:05:00 +02:00
parent bf190f7306
commit 464cd5fc8b
7 changed files with 22 additions and 30 deletions

View File

@ -94,6 +94,14 @@
#define RETURNS_ALIGNED(x) #define RETURNS_ALIGNED(x)
#endif #endif
#ifdef __GNUC__
#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif
/* /*
include win32 defns for everything up to WinServer2003, and assume include win32 defns for everything up to WinServer2003, and assume
I'm smart enough to use GetProcAddress for backward compat on I'm smart enough to use GetProcAddress for backward compat on

View File

@ -51,7 +51,7 @@ test_assert(ostream &out, int indent_level) const {
indent(out, indent_level) indent(out, indent_level)
<< "Notify *notify = Notify::ptr();\n"; << "Notify *notify = Notify::ptr();\n";
indent(out, indent_level) indent(out, indent_level)
<< "if (notify->has_assert_failed()) {\n"; << "if (UNLIKELY(notify->has_assert_failed())) {\n";
indent(out, indent_level + 2) indent(out, indent_level + 2)
<< "PyErr_SetString(PyExc_AssertionError, notify->get_assert_error_message().c_str());\n"; << "PyErr_SetString(PyExc_AssertionError, notify->get_assert_error_message().c_str());\n";
indent(out, indent_level + 2) indent(out, indent_level + 2)

View File

@ -6000,7 +6000,7 @@ write_function_instance(ostream &out, FunctionRemap *remap,
indent(out, indent_level) indent(out, indent_level)
<< "Notify *notify = Notify::ptr();\n"; << "Notify *notify = Notify::ptr();\n";
indent(out, indent_level) indent(out, indent_level)
<< "if (notify->has_assert_failed()) {\n"; << "if (UNLIKELY(notify->has_assert_failed())) {\n";
if (manage_return) { if (manage_return) {
// Output code to delete any temporary object we may have allocated. // Output code to delete any temporary object we may have allocated.

View File

@ -322,11 +322,11 @@ PyObject *_Dtool_Raise_BadArgumentsError() {
* NULL, otherwise Py_None. * NULL, otherwise Py_None.
*/ */
PyObject *_Dtool_Return_None() { PyObject *_Dtool_Return_None() {
if (_PyErr_OCCURRED()) { if (UNLIKELY(_PyErr_OCCURRED())) {
return NULL; return NULL;
} }
#ifndef NDEBUG #ifndef NDEBUG
if (Notify::ptr()->has_assert_failed()) { if (UNLIKELY(Notify::ptr()->has_assert_failed())) {
return Dtool_Raise_AssertionError(); return Dtool_Raise_AssertionError();
} }
#endif #endif
@ -339,11 +339,11 @@ PyObject *_Dtool_Return_None() {
* NULL, otherwise the given boolean value as a PyObject *. * NULL, otherwise the given boolean value as a PyObject *.
*/ */
PyObject *Dtool_Return_Bool(bool value) { PyObject *Dtool_Return_Bool(bool value) {
if (_PyErr_OCCURRED()) { if (UNLIKELY(_PyErr_OCCURRED())) {
return NULL; return NULL;
} }
#ifndef NDEBUG #ifndef NDEBUG
if (Notify::ptr()->has_assert_failed()) { if (UNLIKELY(Notify::ptr()->has_assert_failed())) {
return Dtool_Raise_AssertionError(); return Dtool_Raise_AssertionError();
} }
#endif #endif
@ -358,11 +358,11 @@ PyObject *Dtool_Return_Bool(bool value) {
* increased. * increased.
*/ */
PyObject *_Dtool_Return(PyObject *value) { PyObject *_Dtool_Return(PyObject *value) {
if (_PyErr_OCCURRED()) { if (UNLIKELY(_PyErr_OCCURRED())) {
return NULL; return NULL;
} }
#ifndef NDEBUG #ifndef NDEBUG
if (Notify::ptr()->has_assert_failed()) { if (UNLIKELY(Notify::ptr()->has_assert_failed())) {
return Dtool_Raise_AssertionError(); return Dtool_Raise_AssertionError();
} }
#endif #endif

View File

@ -320,9 +320,9 @@ EXPCL_INTERROGATEDB bool _Dtool_CheckErrorOccurred();
#endif #endif
#ifdef NDEBUG #ifdef NDEBUG
#define Dtool_CheckErrorOccurred() (_PyErr_OCCURRED() != NULL) #define Dtool_CheckErrorOccurred() (UNLIKELY(_PyErr_OCCURRED() != NULL))
#else #else
#define Dtool_CheckErrorOccurred() _Dtool_CheckErrorOccurred() #define Dtool_CheckErrorOccurred() (UNLIKELY(_Dtool_CheckErrorOccurred()))
#endif #endif
EXPCL_INTERROGATEDB PyObject *Dtool_Raise_AssertionError(); EXPCL_INTERROGATEDB PyObject *Dtool_Raise_AssertionError();

View File

@ -71,11 +71,7 @@ is_on(NotifySeverity severity) const {
INLINE bool NotifyCategory:: INLINE bool NotifyCategory::
is_spam() const { is_spam() const {
// Instruct the compiler to optimize for the usual case. // Instruct the compiler to optimize for the usual case.
#ifdef __GNUC__ return UNLIKELY(is_on(NS_spam));
return __builtin_expect(is_on(NS_spam), 0);
#else
return is_on(NS_spam);
#endif
} }
/** /**
@ -84,11 +80,7 @@ is_spam() const {
INLINE bool NotifyCategory:: INLINE bool NotifyCategory::
is_debug() const { is_debug() const {
// Instruct the compiler to optimize for the usual case. // Instruct the compiler to optimize for the usual case.
#ifdef __GNUC__ return UNLIKELY(is_on(NS_debug));
return __builtin_expect(is_on(NS_debug), 0);
#else
return is_on(NS_debug);
#endif
} }
#else #else
/** /**

View File

@ -70,11 +70,7 @@ template<class GetCategory>
INLINE bool NotifyCategoryProxy<GetCategory>:: INLINE bool NotifyCategoryProxy<GetCategory>::
is_spam() { is_spam() {
// Instruct the compiler to optimize for the usual case. // Instruct the compiler to optimize for the usual case.
#ifdef __GNUC__ return UNLIKELY(get_unsafe_ptr()->is_spam());
return __builtin_expect(get_unsafe_ptr()->is_spam(), 0);
#else
return get_unsafe_ptr()->is_spam();
#endif
} }
#else #else
template<class GetCategory> template<class GetCategory>
@ -92,11 +88,7 @@ template<class GetCategory>
INLINE bool NotifyCategoryProxy<GetCategory>:: INLINE bool NotifyCategoryProxy<GetCategory>::
is_debug() { is_debug() {
// Instruct the compiler to optimize for the usual case. // Instruct the compiler to optimize for the usual case.
#ifdef __GNUC__ return UNLIKELY(get_unsafe_ptr()->is_debug());
return __builtin_expect(get_unsafe_ptr()->is_debug(), 0);
#else
return get_unsafe_ptr()->is_debug();
#endif
} }
#else #else
template<class GetCategory> template<class GetCategory>