From: Raphael Isemann Date: Sat, 11 Aug 2018 23:40:27 +0000 (+0000) Subject: Use a DenseMap for looking up functions by UID in CompileUnit::FindFunctionByUID X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a7f19e5fda36ac148528c43ecb1b7d15803e4196;p=platform%2Fupstream%2Fllvm.git 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 llvm-svn: 339504 --- diff --git a/lldb/include/lldb/Symbol/CompileUnit.h b/lldb/include/lldb/Symbol/CompileUnit.h index b816439..0c9ae2f 100644 --- a/lldb/include/lldb/Symbol/CompileUnit.h +++ b/lldb/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 @@ 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 diff --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp index b078cd30ae..8cb60dc 100644 --- a/lldb/source/Core/Module.cpp +++ b/lldb/source/Core/Module.cpp @@ -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; diff --git a/lldb/source/Symbol/CompileUnit.cpp b/lldb/source/Symbol/CompileUnit.cpp index a4f0d42..7302561 100644 --- a/lldb/source/Symbol/CompileUnit.cpp +++ b/lldb/source/Symbol/CompileUnit.cpp @@ -22,7 +22,7 @@ CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data, 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 @@ CompileUnit::CompileUnit(const lldb::ModuleSP &module_sp, void *user_data, 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 @@ void CompileUnit::GetDescription(Stream *s, << (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 @@ void CompileUnit::Dump(Stream *s, bool show_context) const { 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 @@ void CompileUnit::Dump(Stream *s, bool show_context) const { // 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::GetFunctionAtIndex(size_t idx) { //} 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() {