Use map instead of hashmap in signal cache 64/282564/3
authorEunki, Hong <eunkiki.hong@samsung.com>
Thu, 6 Oct 2022 04:15:06 +0000 (13:15 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Thu, 6 Oct 2022 05:59:21 +0000 (14:59 +0900)
std::unordered_map is heavy than std::map for small case.
Let make the signal more lightweight.

Change-Id: I7868b44831fa095b8cdd0f963c6ac66ac741e6a7
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
dali/public-api/signals/base-signal.cpp
dali/public-api/signals/connection-tracker.cpp

index 5e7b66c..0729fab 100644 (file)
 #include <dali/public-api/signals/base-signal.h>
 
 // EXTERNAL INCLUDES
-#include <unordered_map>
+#include <map>
 
 // INTERNAL INCLUDES
 #include <dali/integration-api/debug.h>
 
 namespace
 {
-struct CallbackBasePtrHash
-{
-  std::size_t operator()(const Dali::CallbackBase* callback) const noexcept
-  {
-    std::size_t functionHash = reinterpret_cast<std::size_t>(reinterpret_cast<void*>(callback->mFunction));
-    std::size_t objectHash   = reinterpret_cast<std::size_t>(reinterpret_cast<void*>(callback->mImpl.mObjectPointer));
-    return functionHash ^ objectHash;
-  }
-};
-struct CallbackBasePtrEqual
+struct CallbackBasePtrCompare
 {
   bool operator()(const Dali::CallbackBase* lhs, const Dali::CallbackBase* rhs) const noexcept
   {
-    return (*lhs) == (*rhs);
+    const void* lhsFunctionPtr = reinterpret_cast<void*>(lhs->mFunction);
+    const void* rhsFunctionPtr = reinterpret_cast<void*>(rhs->mFunction);
+    if(lhsFunctionPtr < rhsFunctionPtr)
+    {
+      return true;
+    }
+    else if(lhsFunctionPtr > rhsFunctionPtr)
+    {
+      return false;
+    }
+
+    if(lhs->mImpl.mObjectPointer < rhs->mImpl.mObjectPointer)
+    {
+      return true;
+    }
+    else
+    {
+      return false;
+    }
   }
 };
 } // unnamed namespace
@@ -57,9 +66,9 @@ struct BaseSignal::Impl
   /**
    * @brief Get the iterator of connections list by the callback base pointer.
    * Note that we should compare the 'value' of callback, not pointer.
-   * So, we need to define custom hash & compare functor of callback base pointer.
+   * So, we need to define custom compare functor of callback base pointer.
    */
-  std::unordered_map<const CallbackBase*, std::list<SignalConnection>::iterator, CallbackBasePtrHash, CallbackBasePtrEqual> mCallbackCache;
+  std::map<const CallbackBase*, std::list<SignalConnection>::iterator, CallbackBasePtrCompare> mCallbackCache;
 };
 
 BaseSignal::BaseSignal()
index 42ed2d5..a83859e 100644 (file)
@@ -19,7 +19,7 @@
 #include <dali/public-api/signals/connection-tracker.h>
 
 // EXTERNAL INCLUDES
-#include <unordered_map>
+#include <map>
 
 // INTERNAL INCLUDES
 #include <dali/public-api/signals/callback.h>
@@ -35,7 +35,7 @@ struct ConnectionTracker::Impl
   Impl()  = default;
   ~Impl() = default;
 
-  std::unordered_map<CallbackBase*, SlotObserver*> mCallbackCache;
+  std::map<CallbackBase*, SlotObserver*> mCallbackCache;
 };
 
 ConnectionTracker::ConnectionTracker()
@@ -63,7 +63,6 @@ void ConnectionTracker::DisconnectAll()
   }
 
   mCacheImpl->mCallbackCache.clear();
-  mCacheImpl->mCallbackCache.rehash(0); ///< Note : unordered_map.clear() didn't deallocate memory.
 }
 
 void ConnectionTracker::SignalConnected(SlotObserver* slotObserver, CallbackBase* callback)