Browse Source

Replace __builtin_expect macros with LIKELY/UNLIKELY

rdb 8 years ago
parent
commit
464cd5fc8b

+ 8 - 0
dtool/src/dtoolbase/dtoolbase.h

@@ -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

+ 1 - 1
dtool/src/interrogate/interfaceMakerPython.cxx

@@ -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)

+ 1 - 1
dtool/src/interrogate/interfaceMakerPythonNative.cxx

@@ -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.

+ 6 - 6
dtool/src/interrogatedb/py_panda.cxx

@@ -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

+ 2 - 2
dtool/src/interrogatedb/py_panda.h

@@ -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();

+ 2 - 10
dtool/src/prc/notifyCategory.I

@@ -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 __builtin_expect(is_on(NS_spam), 0);
-#else
-  return is_on(NS_spam);
-#endif
+  return UNLIKELY(is_on(NS_spam));
 }
 }
 
 
 /**
 /**
@@ -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 __builtin_expect(is_on(NS_debug), 0);
-#else
-  return is_on(NS_debug);
-#endif
+  return UNLIKELY(is_on(NS_debug));
 }
 }
 #else
 #else
 /**
 /**

+ 2 - 10
dtool/src/prc/notifyCategoryProxy.I

@@ -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 __builtin_expect(get_unsafe_ptr()->is_spam(), 0);
-#else
-  return get_unsafe_ptr()->is_spam();
-#endif
+  return UNLIKELY(get_unsafe_ptr()->is_spam());
 }
 }
 #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 __builtin_expect(get_unsafe_ptr()->is_debug(), 0);
-#else
-  return get_unsafe_ptr()->is_debug();
-#endif
+  return UNLIKELY(get_unsafe_ptr()->is_debug());
 }
 }
 #else
 #else
 template<class GetCategory>
 template<class GetCategory>