[Lldb-commits] [lldb] r106116 - in /lldb/trunk: include/lldb/Symbol/Symtab.h source/Symbol/Symtab.cpp

Owen Anderson resistor at mac.com
Wed Jun 16 10:34:06 PDT 2010


Author: resistor
Date: Wed Jun 16 12:34:05 2010
New Revision: 106116

URL: http://llvm.org/viewvc/llvm-project?rev=106116&view=rev
Log:
Switch from qsort_r to std::stable_sort for a performance win and improved portability.

Modified:
    lldb/trunk/include/lldb/Symbol/Symtab.h
    lldb/trunk/source/Symbol/Symtab.cpp

Modified: lldb/trunk/include/lldb/Symbol/Symtab.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Symbol/Symtab.h?rev=106116&r1=106115&r2=106116&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Symbol/Symtab.h (original)
+++ lldb/trunk/include/lldb/Symbol/Symtab.h Wed Jun 16 12:34:05 2010
@@ -59,8 +59,6 @@
     typedef collection::iterator        iterator;
     typedef collection::const_iterator  const_iterator;
 
-    static  int         CompareSymbolValueByIndex (void *thunk, const void *a, const void *b);
-    static  int         CompareSymbolValueByIndexLinux (const void *a, const void *b, void *thunk);
             void        InitNameIndexes ();
             void        InitAddressIndexes ();
 

Modified: lldb/trunk/source/Symbol/Symtab.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/Symtab.cpp?rev=106116&r1=106115&r2=106116&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/Symtab.cpp (original)
+++ lldb/trunk/source/Symbol/Symtab.cpp Wed Jun 16 12:34:05 2010
@@ -214,62 +214,48 @@
     const Symbol *symbols;
 };
 
-int
-Symtab::CompareSymbolValueByIndex (void *thunk, const void *a, const void *b)
-{
-    const Symbol *symbols = (const Symbol *)thunk;
-    uint32_t index_a = *((uint32_t *) a);
-    uint32_t index_b = *((uint32_t *) b);
-
-    addr_t value_a;
-    addr_t value_b;
-    if (symbols[index_a].GetValue().GetSection() == symbols[index_b].GetValue().GetSection())
-    {
-        value_a = symbols[index_a].GetValue ().GetOffset();
-        value_b = symbols[index_b].GetValue ().GetOffset();
-    }
-    else
-    {
-        value_a = symbols[index_a].GetValue ().GetFileAddress();
-        value_b = symbols[index_b].GetValue ().GetFileAddress();
-    }
-
-    if (value_a == value_b)
-    {
-        // The if the values are equal, use the original symbol user ID
-        lldb::user_id_t uid_a = symbols[index_a].GetID();
-        lldb::user_id_t uid_b = symbols[index_b].GetID();
-        if (uid_a < uid_b)
-            return -1;
-        if (uid_a > uid_b)
-            return 1;
-        return 0;
-    }
-    else if (value_a < value_b)
-        return -1;
-
-    return 1;
-}
+namespace {
+    struct SymbolIndexComparator {
+        const std::vector<Symbol>& symbols;
+        SymbolIndexComparator(const std::vector<Symbol>& s) : symbols(s) { }
+        bool operator()(uint32_t index_a, uint32_t index_b) {
+            addr_t value_a;
+            addr_t value_b;
+            if (symbols[index_a].GetValue().GetSection() == symbols[index_b].GetValue().GetSection()) {
+                value_a = symbols[index_a].GetValue ().GetOffset();
+                value_b = symbols[index_b].GetValue ().GetOffset();
+            } else {
+                value_a = symbols[index_a].GetValue ().GetFileAddress();
+                value_b = symbols[index_b].GetValue ().GetFileAddress();
+            }
 
-int Symtab::CompareSymbolValueByIndexLinux(const void* a, const void* b, void* thunk) 
-{
-    return CompareSymbolValueByIndex(thunk, a, b);
+            if (value_a == value_b) {
+                // The if the values are equal, use the original symbol user ID
+                lldb::user_id_t uid_a = symbols[index_a].GetID();
+                lldb::user_id_t uid_b = symbols[index_b].GetID();
+                if (uid_a < uid_b)
+                    return true;
+                if (uid_a > uid_b)
+                    return false;
+                return false;
+            } else if (value_a < value_b)
+                return true;
+        
+            return false;
+        }
+    };
 }
 
 void
 Symtab::SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const
 {
+    Timer scoped_timer (__PRETTY_FUNCTION__,__PRETTY_FUNCTION__);
     // No need to sort if we have zero or one items...
     if (indexes.size() <= 1)
         return;
 
-    // Sort the indexes in place using qsort
-    // FIXME: (WRONGDEFINE) Need a better define for this! 
-#ifdef __APPLE__
-    ::qsort_r (&indexes[0], indexes.size(), sizeof(uint32_t), (void *)&m_symbols[0], Symtab::CompareSymbolValueByIndex);
-#else
-    ::qsort_r (&indexes[0], indexes.size(), sizeof(uint32_t), CompareSymbolValueByIndexLinux, (void *)&m_symbols[0]);
-#endif
+    // Sort the indexes in place using std::sort
+    std::stable_sort(indexes.begin(), indexes.end(), SymbolIndexComparator(m_symbols));
 
     // Remove any duplicates if requested
     if (remove_duplicates)





More information about the lldb-commits mailing list