[Lldb-commits] [lldb] r155873 - /lldb/trunk/source/Symbol/Symtab.cpp

Jim Ingham jingham at apple.com
Mon Apr 30 18:34:15 PDT 2012


Author: jingham
Date: Mon Apr 30 20:34:15 2012
New Revision: 155873

URL: http://llvm.org/viewvc/llvm-project?rev=155873&view=rev
Log:
Use a cache of the results of "GetFileAddress" from a symbol in the Comparator we are using to sort the various lookup indices by symbol address.  When we switched to weak pointers,
this lookup got slightly slower.  Not enough to matter for most uses, but in the sort algorithm it does matter.

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

Modified: lldb/trunk/source/Symbol/Symtab.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/Symtab.cpp?rev=155873&r1=155872&r2=155873&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/Symtab.cpp (original)
+++ lldb/trunk/source/Symbol/Symtab.cpp Mon Apr 30 20:34:15 2012
@@ -440,10 +440,35 @@
 namespace {
     struct SymbolIndexComparator {
         const std::vector<Symbol>& symbols;
-        SymbolIndexComparator(const std::vector<Symbol>& s) : symbols(s) { }
+        std::vector<lldb::addr_t>  &addr_cache;
+        
+        // Getting from the symbol to the Address to the File Address involves some work.
+        // Since there are potentially many symbols here, and we're using this for sorting so
+        // we're going to be computing the address many times, cache that in addr_cache.
+        // The array passed in has to be the same size as the symbols array passed into the
+        // member variable symbols, and should be initialized with LLDB_INVALID_ADDRESS.
+        // NOTE: You have to make addr_cache externally and pass it in because std::stable_sort
+        // makes copies of the comparator it is initially passed in, and you end up spending
+        // huge amounts of time copying this array...
+        
+        SymbolIndexComparator(const std::vector<Symbol>& s, std::vector<lldb::addr_t> &a) : symbols(s), addr_cache(a)  {
+            assert (symbols.size() == addr_cache.size());
+        }
         bool operator()(uint32_t index_a, uint32_t index_b) {
-            addr_t value_a = symbols[index_a].GetAddress().GetFileAddress();
-            addr_t value_b = symbols[index_b].GetAddress().GetFileAddress();
+            addr_t value_a = addr_cache[index_a];
+            if (value_a == LLDB_INVALID_ADDRESS)
+            {
+                value_a = symbols[index_a].GetAddress().GetFileAddress();
+                addr_cache[index_a] = value_a;
+            }
+            
+            addr_t value_b = addr_cache[index_b];
+            if (value_b == LLDB_INVALID_ADDRESS)
+            {
+                value_b = symbols[index_b].GetAddress().GetFileAddress();
+                addr_cache[index_b] = value_b;
+            }
+            
 
             if (value_a == value_b) {
                 // The if the values are equal, use the original symbol user ID
@@ -476,7 +501,11 @@
     // NOTE: The use of std::stable_sort instead of std::sort here is strictly for performance,
     // not correctness.  The indexes vector tends to be "close" to sorted, which the
     // stable sort handles better.
-    std::stable_sort(indexes.begin(), indexes.end(), SymbolIndexComparator(m_symbols));
+    
+    std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS);
+    
+    SymbolIndexComparator comparator(m_symbols, addr_cache);
+    std::stable_sort(indexes.begin(), indexes.end(), comparator);
 
     // Remove any duplicates if requested
     if (remove_duplicates)





More information about the lldb-commits mailing list