[Lldb-commits] [PATCH] D50225: Use a DenseMap for looking up functions by UID in CompileUnit::FindFunctionByUID

Raphael Isemann via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 2 22:47:49 PDT 2018


teemperor created this revision.
teemperor added a reviewer: vsk.

Instead of iterating over our vector of functions, we might as well use a map here to
directly get the function we need.

Thanks to Vedant for pointing this out.


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D50225

Files:
  include/lldb/Symbol/CompileUnit.h
  source/Symbol/CompileUnit.cpp


Index: source/Symbol/CompileUnit.cpp
===================================================================
--- source/Symbol/CompileUnit.cpp
+++ source/Symbol/CompileUnit.cpp
@@ -106,6 +106,7 @@
 // Add a function to this compile unit
 //----------------------------------------------------------------------
 void CompileUnit::AddFunction(FunctionSP &funcSP) {
+  m_function_uid_to_index[funcSP->GetID()] = m_functions.size();
   // TODO: order these by address
   m_functions.push_back(funcSP);
 }
@@ -163,18 +164,10 @@
 //}
 
 FunctionSP CompileUnit::FindFunctionByUID(lldb::user_id_t func_uid) {
-  FunctionSP funcSP;
-  if (!m_functions.empty()) {
-    std::vector<FunctionSP>::const_iterator pos;
-    std::vector<FunctionSP>::const_iterator end = m_functions.end();
-    for (pos = m_functions.begin(); pos != end; ++pos) {
-      if ((*pos)->GetID() == func_uid) {
-        funcSP = *pos;
-        break;
-      }
-    }
-  }
-  return funcSP;
+  auto it = m_function_uid_to_index.find(func_uid);
+  if (it == m_function_uid_to_index.end())
+    return FunctionSP();
+  return m_functions[it->second];
 }
 
 lldb::LanguageType CompileUnit::GetLanguage() {
Index: include/lldb/Symbol/CompileUnit.h
===================================================================
--- include/lldb/Symbol/CompileUnit.h
+++ include/lldb/Symbol/CompileUnit.h
@@ -18,6 +18,8 @@
 #include "lldb/Utility/UserID.h"
 #include "lldb/lldb-enumerations.h"
 
+#include "llvm/ADT/DenseMap.h"
+
 namespace lldb_private {
 //----------------------------------------------------------------------
 /// @class CompileUnit CompileUnit.h "lldb/Symbol/CompileUnit.h"
@@ -418,6 +420,9 @@
   std::vector<lldb::FunctionSP> m_functions; ///< The sparsely populated list of
                                              ///shared pointers to functions
   ///< that gets populated as functions get partially parsed.
+
+  /// Maps function UIDs to indexes in m_functions.
+  llvm::DenseMap<lldb::user_id_t, size_t> m_function_uid_to_index;
   std::vector<ConstString> m_imported_modules; ///< All modules, including the
                                                ///current module, imported by
                                                ///this


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50225.158876.patch
Type: text/x-patch
Size: 2219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20180803/f2fc1cc2/attachment-0001.bin>


More information about the lldb-commits mailing list