]> granicus.if.org Git - icinga2/commitdiff
Improve performance for locks and value conversions
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 19 Apr 2016 07:37:41 +0000 (09:37 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 19 Apr 2016 07:38:22 +0000 (09:38 +0200)
refs #11612

lib/base/i2-base.hpp
lib/base/object.hpp
lib/base/objectlock.hpp
lib/base/value.hpp
lib/config/vmops.hpp

index 1c46815a1bf73701c2da92f1fbe967ec3c667ba5..3d3b67395a9bbc13d43cd8f40766392b78a0b93e 100644 (file)
 #      define I2_BASE_API I2_IMPORT
 #endif /* I2_BASE_BUILD */
 
+#if defined(__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
+
+
 #endif /* I2BASE_H */
index 654e569503dfb7727fd4feff4899664cb44bc4c1..c454bdc12873e08c4bde2af83cb6135983ac497e 100644 (file)
@@ -191,7 +191,7 @@ inline void intrusive_ptr_release(Object *object)
        refs = __sync_sub_and_fetch(&object->m_References, 1);
 #endif /* _WIN32 */
 
-       if (refs == 0) {
+       if (unlikely(refs == 0)) {
 #ifdef I2_LEAK_DEBUG
                TypeRemoveObject(object);
 #endif /* I2_LEAK_DEBUG */
index ea2e05484ad1d8a802c175ab819e663b8aa111f7..9368a25bccd05da329b9252938cd8d446e28593b 100644 (file)
@@ -62,14 +62,14 @@ public:
 
 #ifdef _WIN32
 #      ifdef _WIN64
-               while (InterlockedCompareExchange64((LONGLONG *)&object->m_Mutex, I2MUTEX_LOCKED, I2MUTEX_UNLOCKED) != I2MUTEX_UNLOCKED) {
+               while (likely(InterlockedCompareExchange64((LONGLONG *)&object->m_Mutex, I2MUTEX_LOCKED, I2MUTEX_UNLOCKED) != I2MUTEX_UNLOCKED)) {
 #      else /* _WIN64 */
-               while (InterlockedCompareExchange(&object->m_Mutex, I2MUTEX_LOCKED, I2MUTEX_UNLOCKED) != I2MUTEX_UNLOCKED) {
+               while (likely(InterlockedCompareExchange(&object->m_Mutex, I2MUTEX_LOCKED, I2MUTEX_UNLOCKED) != I2MUTEX_UNLOCKED)) {
 #      endif /* _WIN64 */
 #else /* _WIN32 */
-               while (!__sync_bool_compare_and_swap(&object->m_Mutex, I2MUTEX_UNLOCKED, I2MUTEX_LOCKED)) {
+               while (likely(!__sync_bool_compare_and_swap(&object->m_Mutex, I2MUTEX_UNLOCKED, I2MUTEX_LOCKED))) {
 #endif /* _WIN32 */
-                       if (object->m_Mutex > I2MUTEX_LOCKED) {
+                       if (likely(object->m_Mutex > I2MUTEX_LOCKED)) {
                                boost::recursive_mutex *mtx = reinterpret_cast<boost::recursive_mutex *>(object->m_Mutex);
                                mtx->lock();
 
index df904ce22340ebf508db9addf9da8c475c760af9..34fe7cbd6c1bbf775a963ab69fbd6dd7b9d34e15 100644 (file)
@@ -148,7 +148,7 @@ public:
                if (!IsObject())
                        BOOST_THROW_EXCEPTION(std::runtime_error("Cannot convert value of type '" + GetTypeName() + "' to an object."));
 
-               Object::Ptr object = boost::get<Object::Ptr>(m_Value);
+               const Object::Ptr& object = boost::get<Object::Ptr>(m_Value);
 
                ASSERT(object);
 
index c330b6cd8416db71dd79b37c51b61f16f6f56c28..1479da91f909e0fdcf6e0d0e4b7783e48e70af27 100644 (file)
@@ -193,10 +193,10 @@ public:
 
        static inline Value GetField(const Value& context, const String& field, bool sandboxed = false, const DebugInfo& debugInfo = DebugInfo())
        {
-               if (context.IsEmpty() && !context.IsString())
+               if (unlikely(context.IsEmpty() && !context.IsString()))
                        return Empty;
 
-               if (!context.IsObject())
+               if (unlikely(!context.IsObject()))
                        return GetPrototypeField(context, field, true, debugInfo);
 
                Object::Ptr object = context;