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

2018-08-11 Thread Raphael Isemann via Phabricator via lldb-commits
teemperor updated this revision to Diff 160245.
teemperor added a comment.

- Passing shared_ptr to lambda now as const-ref.


https://reviews.llvm.org/D50225

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

Index: source/Symbol/CompileUnit.cpp
===
--- source/Symbol/CompileUnit.cpp
+++ source/Symbol/CompileUnit.cpp
@@ -22,7 +22,7 @@
  lldb::LanguageType language,
  lldb_private::LazyBool is_optimized)
 : ModuleChild(module_sp), FileSpec(pathname, false), UserID(cu_sym_id),
-  m_user_data(user_data), m_language(language), m_flags(0), m_functions(),
+  m_user_data(user_data), m_language(language), m_flags(0),
   m_support_files(), m_line_table_ap(), m_variables(),
   m_is_optimized(is_optimized) {
   if (language != eLanguageTypeUnknown)
@@ -35,7 +35,7 @@
  lldb::LanguageType language,
  lldb_private::LazyBool is_optimized)
 : ModuleChild(module_sp), FileSpec(fspec), UserID(cu_sym_id),
-  m_user_data(user_data), m_language(language), m_flags(0), m_functions(),
+  m_user_data(user_data), m_language(language), m_flags(0),
   m_support_files(), m_line_table_ap(), m_variables(),
   m_is_optimized(is_optimized) {
   if (language != eLanguageTypeUnknown)
@@ -66,6 +66,22 @@
  << (const FileSpec &)*this << "\", language = \"" << language << '"';
 }
 
+void CompileUnit::ForeachFunction(
+llvm::function_ref lambda) const {
+  std::vector sorted_functions;
+  sorted_functions.reserve(m_functions_by_uid.size());
+  for (auto &p : m_functions_by_uid)
+sorted_functions.push_back(p.second);
+  std::sort(sorted_functions.begin(), sorted_functions.end(),
+[](const lldb::FunctionSP &a, const lldb::FunctionSP &b) {
+  return a->GetID() < b->GetID();
+});
+
+  for (auto &f : sorted_functions)
+if (lambda(f))
+  return;
+}
+
 //--
 // Dump the current contents of this object. No functions that cause on demand
 // parsing of functions, globals, statics are called, so this is a good
@@ -89,13 +105,12 @@
 s->IndentLess();
   }
 
-  if (!m_functions.empty()) {
+  if (!m_functions_by_uid.empty()) {
 s->IndentMore();
-std::vector::const_iterator pos;
-std::vector::const_iterator end = m_functions.end();
-for (pos = m_functions.begin(); pos != end; ++pos) {
-  (*pos)->Dump(s, show_context);
-}
+ForeachFunction([&s, show_context](const FunctionSP &f) {
+  f->Dump(s, show_context);
+  return false;
+});
 
 s->IndentLess();
 s->EOL();
@@ -106,15 +121,7 @@
 // Add a function to this compile unit
 //--
 void CompileUnit::AddFunction(FunctionSP &funcSP) {
-  // TODO: order these by address
-  m_functions.push_back(funcSP);
-}
-
-FunctionSP CompileUnit::GetFunctionAtIndex(size_t idx) {
-  FunctionSP funcSP;
-  if (idx < m_functions.size())
-funcSP = m_functions[idx];
-  return funcSP;
+  m_functions_by_uid[funcSP->GetID()] = funcSP;
 }
 
 //--
@@ -163,18 +170,10 @@
 //}
 
 FunctionSP CompileUnit::FindFunctionByUID(lldb::user_id_t func_uid) {
-  FunctionSP funcSP;
-  if (!m_functions.empty()) {
-std::vector::const_iterator pos;
-std::vector::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_functions_by_uid.find(func_uid);
+  if (it == m_functions_by_uid.end())
+return FunctionSP();
+  return it->second;
 }
 
 lldb::LanguageType CompileUnit::GetLanguage() {
Index: source/Core/Module.cpp
===
--- source/Core/Module.cpp
+++ source/Core/Module.cpp
@@ -371,15 +371,13 @@
 
   symbols->ParseCompileUnitFunctions(sc);
 
-  for (size_t func_idx = 0;
-   (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) !=
-   nullptr;
-   ++func_idx) {
+  sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
+sc.function = f.get();
 symbols->ParseFunctionBlocks(sc);
-
 // Parse the variables for this function and all its blocks
 symbols->ParseVariablesForContext(sc);
-  }
+return false;
+  });
 
   // Parse all types for this compile unit
   sc.function = nullptr;
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"
 
+#

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

2018-08-11 Thread Raphael Isemann via lldb-commits
Author: teemperor
Date: Sat Aug 11 16:40:27 2018
New Revision: 339504

URL: http://llvm.org/viewvc/llvm-project?rev=339504&view=rev
Log:
Use a DenseMap for looking up functions by UID in CompileUnit::FindFunctionByUID

Summary:
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.

Reviewers: vsk

Reviewed By: vsk

Subscribers: mgrang, lldb-commits

Differential Revision: https://reviews.llvm.org/D50225

Modified:
lldb/trunk/include/lldb/Symbol/CompileUnit.h
lldb/trunk/source/Core/Module.cpp
lldb/trunk/source/Symbol/CompileUnit.cpp

Modified: lldb/trunk/include/lldb/Symbol/CompileUnit.h
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Symbol/CompileUnit.h?rev=339504&r1=339503&r2=339504&view=diff
==
--- lldb/trunk/include/lldb/Symbol/CompileUnit.h (original)
+++ lldb/trunk/include/lldb/Symbol/CompileUnit.h Sat Aug 11 16:40:27 2018
@@ -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"
@@ -163,21 +165,19 @@ public:
   void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
 
   //--
-  /// Get a shared pointer to a function in this compile unit by index.
+  /// Apply a lambda to each function in this compile unit.
   ///
-  /// Typically called when iterating though all functions in a compile unit
-  /// after all functions have been parsed. This provides raw access to the
-  /// function shared pointer list and will not cause the SymbolFile plug-in
-  /// to parse any unparsed functions.
+  /// This provides raw access to the function shared pointer list and will not
+  /// cause the SymbolFile plug-in to parse any unparsed functions.
   ///
-  /// @param[in] idx
-  /// An index into the function list.
+  /// @note Prefer using FindFunctionByUID over this if possible.
   ///
-  /// @return
-  /// A shared pointer to a function that might contain a NULL
-  /// Function class pointer.
+  /// @param[in] lambda
+  /// The lambda that should be applied to every function. The lambda can
+  /// return true if the iteration should be aborted earlier.
   //--
-  lldb::FunctionSP GetFunctionAtIndex(size_t idx);
+  void ForeachFunction(
+  llvm::function_ref lambda) const;
 
   //--
   /// Dump the compile unit contents to the stream \a s.
@@ -415,9 +415,9 @@ protected:
   lldb::LanguageType
   m_language; ///< The programming language enumeration value.
   Flags m_flags;  ///< Compile unit flags that help with partial parsing.
-  std::vector m_functions; ///< The sparsely populated list 
of
- ///shared pointers to functions
-  ///< that gets populated as functions get partially parsed.
+
+  /// Maps UIDs to functions.
+  llvm::DenseMap m_functions_by_uid;
   std::vector m_imported_modules; ///< All modules, including the
///current module, imported by
///this

Modified: lldb/trunk/source/Core/Module.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Module.cpp?rev=339504&r1=339503&r2=339504&view=diff
==
--- lldb/trunk/source/Core/Module.cpp (original)
+++ lldb/trunk/source/Core/Module.cpp Sat Aug 11 16:40:27 2018
@@ -371,15 +371,13 @@ void Module::ParseAllDebugSymbols() {
 
   symbols->ParseCompileUnitFunctions(sc);
 
-  for (size_t func_idx = 0;
-   (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) !=
-   nullptr;
-   ++func_idx) {
+  sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
+sc.function = f.get();
 symbols->ParseFunctionBlocks(sc);
-
 // Parse the variables for this function and all its blocks
 symbols->ParseVariablesForContext(sc);
-  }
+return false;
+  });
 
   // Parse all types for this compile unit
   sc.function = nullptr;

Modified: lldb/trunk/source/Symbol/CompileUnit.cpp
URL: 
http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/CompileUnit.cpp?rev=339504&r1=339503&r2=339504&view=diff
==
--- lldb/trunk/source/Symbol/CompileUnit.cpp (original)
+++ lldb/trunk/source/Symbol/CompileUnit.cpp Sat Aug 11 16:40:27 2018
@@ -22,7 +22,7 @@ CompileUnit::CompileUnit(const lldb::Mod
   

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

2018-08-11 Thread Raphael Isemann via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL339504: Use a DenseMap for looking up functions by UID in 
CompileUnit::FindFunctionByUID (authored by teemperor, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D50225?vs=160245&id=160246#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D50225

Files:
  lldb/trunk/include/lldb/Symbol/CompileUnit.h
  lldb/trunk/source/Core/Module.cpp
  lldb/trunk/source/Symbol/CompileUnit.cpp

Index: lldb/trunk/include/lldb/Symbol/CompileUnit.h
===
--- lldb/trunk/include/lldb/Symbol/CompileUnit.h
+++ lldb/trunk/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"
@@ -163,21 +165,19 @@
   void GetDescription(Stream *s, lldb::DescriptionLevel level) const;
 
   //--
-  /// Get a shared pointer to a function in this compile unit by index.
+  /// Apply a lambda to each function in this compile unit.
   ///
-  /// Typically called when iterating though all functions in a compile unit
-  /// after all functions have been parsed. This provides raw access to the
-  /// function shared pointer list and will not cause the SymbolFile plug-in
-  /// to parse any unparsed functions.
+  /// This provides raw access to the function shared pointer list and will not
+  /// cause the SymbolFile plug-in to parse any unparsed functions.
   ///
-  /// @param[in] idx
-  /// An index into the function list.
+  /// @note Prefer using FindFunctionByUID over this if possible.
   ///
-  /// @return
-  /// A shared pointer to a function that might contain a NULL
-  /// Function class pointer.
+  /// @param[in] lambda
+  /// The lambda that should be applied to every function. The lambda can
+  /// return true if the iteration should be aborted earlier.
   //--
-  lldb::FunctionSP GetFunctionAtIndex(size_t idx);
+  void ForeachFunction(
+  llvm::function_ref lambda) const;
 
   //--
   /// Dump the compile unit contents to the stream \a s.
@@ -415,9 +415,9 @@
   lldb::LanguageType
   m_language; ///< The programming language enumeration value.
   Flags m_flags;  ///< Compile unit flags that help with partial parsing.
-  std::vector m_functions; ///< The sparsely populated list of
- ///shared pointers to functions
-  ///< that gets populated as functions get partially parsed.
+
+  /// Maps UIDs to functions.
+  llvm::DenseMap m_functions_by_uid;
   std::vector m_imported_modules; ///< All modules, including the
///current module, imported by
///this
Index: lldb/trunk/source/Core/Module.cpp
===
--- lldb/trunk/source/Core/Module.cpp
+++ lldb/trunk/source/Core/Module.cpp
@@ -371,15 +371,13 @@
 
   symbols->ParseCompileUnitFunctions(sc);
 
-  for (size_t func_idx = 0;
-   (sc.function = sc.comp_unit->GetFunctionAtIndex(func_idx).get()) !=
-   nullptr;
-   ++func_idx) {
+  sc.comp_unit->ForeachFunction([&sc, &symbols](const FunctionSP &f) {
+sc.function = f.get();
 symbols->ParseFunctionBlocks(sc);
-
 // Parse the variables for this function and all its blocks
 symbols->ParseVariablesForContext(sc);
-  }
+return false;
+  });
 
   // Parse all types for this compile unit
   sc.function = nullptr;
Index: lldb/trunk/source/Symbol/CompileUnit.cpp
===
--- lldb/trunk/source/Symbol/CompileUnit.cpp
+++ lldb/trunk/source/Symbol/CompileUnit.cpp
@@ -22,7 +22,7 @@
  lldb::LanguageType language,
  lldb_private::LazyBool is_optimized)
 : ModuleChild(module_sp), FileSpec(pathname, false), UserID(cu_sym_id),
-  m_user_data(user_data), m_language(language), m_flags(0), m_functions(),
+  m_user_data(user_data), m_language(language), m_flags(0),
   m_support_files(), m_line_table_ap(), m_variables(),
   m_is_optimized(is_optimized) {
   if (language != eLanguageTypeUnknown)
@@ -35,7 +35,7 @@
  lldb::LanguageType language,
  lldb_private::LazyBool is_optimized)
 : ModuleChild(module_sp), FileSpec(fspec), UserID(cu_sym_id),
-  m_user_data(user_data), m_language(language), m_flags(0), m_functions(),
+