From 245cafdc7b07319f61dc74518ba5ef0efcbdaeb2 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Thu, 12 May 2016 22:50:14 +0200 Subject: [PATCH] logic: Improved code view performance * fixed bottleneck in TokenLocationCollection::findLocationFile() * cache file ids and paths in PersistenStorage --- src/lib/data/PersistentStorage.cpp | 71 +++++++++---------- src/lib/data/PersistentStorage.h | 3 + src/lib/data/SqliteStorage.cpp | 21 +----- src/lib/data/SqliteStorage.h | 35 +++++---- .../data/location/TokenLocationCollection.cpp | 9 +-- 5 files changed, 58 insertions(+), 81 deletions(-) diff --git a/src/lib/data/PersistentStorage.cpp b/src/lib/data/PersistentStorage.cpp index d75b0811..57c8c75e 100644 --- a/src/lib/data/PersistentStorage.cpp +++ b/src/lib/data/PersistentStorage.cpp @@ -248,6 +248,7 @@ void PersistentStorage::clearCaches() { m_elementIndex.clear(); m_fileNodeIds.clear(); + m_fileNodePaths.clear(); m_hierarchyCache.clear(); } @@ -357,6 +358,7 @@ void PersistentStorage::startParsing() void PersistentStorage::finishParsing() { buildSearchIndex(); + buildFilePathMaps(); buildHierarchyCache(); optimizeFTSTable(); } @@ -833,16 +835,16 @@ std::shared_ptr PersistentStorage::getTokenLocationsFor std::vector fileIds; std::vector nonFileIds; - std::vector allFileIds = m_sqliteStorage.getAllFileIds(); - for (size_t i = 0; i < tokenIds.size(); i++) + + for (const Id tokenId : tokenIds) { - if (std::find(allFileIds.begin(), allFileIds.end(),tokenIds[i]) != allFileIds.end()) + if (!getFileNodePath(tokenId).empty()) { - fileIds.push_back(tokenIds[i]); + fileIds.push_back(tokenId); } else { - nonFileIds.push_back(tokenIds[i]); + nonFileIds.push_back(tokenId); } } @@ -854,23 +856,15 @@ std::shared_ptr PersistentStorage::getTokenLocationsFor ); } - Cache filePathCache( - [this](Id id) -> std::string - { - return m_sqliteStorage.getFileById(id).filePath; - } - ); - std::vector locations = m_sqliteStorage.getTokenLocationsForElementIds(nonFileIds); for (size_t i = 0; i < locations.size(); i++) { const StorageSourceLocation& location = locations[i]; - std::string filePath = filePathCache.getValue(location.fileNodeId); TokenLocation* loc = collection->addTokenLocation( location.id, location.elementId, - filePath, + getFileNodePath(location.fileNodeId), location.startLine, location.startCol, location.endLine, @@ -1004,6 +998,12 @@ StorageStats PersistentStorage::getStorageStats() const Id PersistentStorage::getFileNodeId(const FilePath& filePath) const { + if (filePath.empty()) + { + LOG_ERROR("No file path set"); + return 0; + } + std::map::const_iterator it = m_fileNodeIds.find(filePath); if (it != m_fileNodeIds.end()) @@ -1011,35 +1011,25 @@ Id PersistentStorage::getFileNodeId(const FilePath& filePath) const return it->second; } - if (filePath.empty()) - { - LOG_ERROR("No file path set"); - return 0; - } - - StorageFile storageFile = m_sqliteStorage.getFileByPath(filePath.str()); - - if (storageFile.id == 0) - { - return 0; - } - - m_fileNodeIds.emplace(filePath, storageFile.id); - - return storageFile.id; + return 0; } FilePath PersistentStorage::getFileNodePath(Id fileId) const { - for (const std::pair& p : m_fileNodeIds) + if (fileId == 0) { - if (p.second == fileId) - { - return p.first; - } + LOG_ERROR("No file id set"); + return FilePath(); } - return m_sqliteStorage.getFileById(fileId).filePath; + std::map::const_iterator it = m_fileNodePaths.find(fileId); + + if (it != m_fileNodePaths.end()) + { + return it->second; + } + + return FilePath(); } Id PersistentStorage::getLastVisibleParentNodeId(const Id nodeId) const @@ -1287,6 +1277,15 @@ void PersistentStorage::buildSearchIndex() m_elementIndex.finishSetup(); } +void PersistentStorage::buildFilePathMaps() +{ + for (StorageFile file: m_sqliteStorage.getAllFiles()) + { + m_fileNodeIds.emplace(file.filePath, file.id); + m_fileNodePaths.emplace(file.id, file.filePath); + } +} + void PersistentStorage::buildHierarchyCache() { std::vector memberEdges = m_sqliteStorage.getEdgesByType(Edge::typeToInt(Edge::EDGE_MEMBER)); diff --git a/src/lib/data/PersistentStorage.h b/src/lib/data/PersistentStorage.h index 6cd58fae..504f2e34 100644 --- a/src/lib/data/PersistentStorage.h +++ b/src/lib/data/PersistentStorage.h @@ -134,6 +134,7 @@ private: void addComponentAccessToGraph(Graph* graph) const; void buildSearchIndex(); + void buildFilePathMaps(); void buildHierarchyCache(); void optimizeFTSTable(); @@ -147,6 +148,8 @@ private: SqliteStorage m_sqliteStorage; mutable std::map m_fileNodeIds; + mutable std::map m_fileNodePaths; + HierarchyCache m_hierarchyCache; }; diff --git a/src/lib/data/SqliteStorage.cpp b/src/lib/data/SqliteStorage.cpp index f681cdd6..0a13acb5 100644 --- a/src/lib/data/SqliteStorage.cpp +++ b/src/lib/data/SqliteStorage.cpp @@ -482,23 +482,6 @@ std::vector SqliteStorage::getFilesByPaths(const std::vector("WHERE file.path IN ('" + utility::join(utility::toStrings(filePaths), "', '") + "')"); } -std::vector SqliteStorage::getAllFileIds() const -{ - std::vector ids; - - CppSQLite3Query q = m_database.execQuery( - "SELECT id FROM file;" - ); - - while (!q.eof()) - { - ids.push_back(q.getIntField(0,0)); - q.nextRow(); - } - - return ids; -} - std::shared_ptr SqliteStorage::getFileContentByPath(const std::string& filePath) const { CppSQLite3Query q = m_database.execQuery(( @@ -1113,8 +1096,8 @@ std::vector SqliteStorage::getAll(const std::string& const std::string message = q.getStringField(0, ""); const bool fatal = q.getIntField(1, 0); const std::string filePath = q.getStringField(2, ""); - const uint lineNumber = q.getIntField(3, -1); - const uint columnNumber = q.getIntField(4, -1); + const int lineNumber = q.getIntField(3, -1); + const int columnNumber = q.getIntField(4, -1); if (lineNumber != -1 && columnNumber != -1) { diff --git a/src/lib/data/SqliteStorage.h b/src/lib/data/SqliteStorage.h index bce3aa5f..998a14e7 100644 --- a/src/lib/data/SqliteStorage.h +++ b/src/lib/data/SqliteStorage.h @@ -79,7 +79,6 @@ public: StorageFile getFileById(const Id id) const; StorageFile getFileByPath(const FilePath& filePath) const; - std::vector getAllFileIds() const; std::vector getFilesByPaths(const std::vector& filePaths) const; std::shared_ptr getFileContentByPath(const std::string& filePath) const; @@ -129,23 +128,6 @@ private: template std::vector getAll(const std::string& query) const; - template <> - std::vector getAll(const std::string& query) const; - template <> - std::vector getAll(const std::string& query) const; - template <> - std::vector getAll(const std::string& query) const; - template <> - std::vector getAll(const std::string& query) const; - template <> - std::vector getAll(const std::string& query) const; - template <> - std::vector getAll(const std::string& query) const; - template <> - std::vector getAll(const std::string& query) const; - template <> - std::vector getAll(const std::string& query) const; - template ResultType getFirst(const std::string& query) const { @@ -161,5 +143,22 @@ private: FilePath m_dbFilePath; }; +template <> +std::vector SqliteStorage::getAll(const std::string& query) const; +template <> +std::vector SqliteStorage::getAll(const std::string& query) const; +template <> +std::vector SqliteStorage::getAll(const std::string& query) const; +template <> +std::vector SqliteStorage::getAll(const std::string& query) const; +template <> +std::vector SqliteStorage::getAll(const std::string& query) const; +template <> +std::vector SqliteStorage::getAll(const std::string& query) const; +template <> +std::vector SqliteStorage::getAll(const std::string& query) const; +template <> +std::vector SqliteStorage::getAll(const std::string& query) const; + #endif // SQLITE_STORAGE_H diff --git a/src/lib/data/location/TokenLocationCollection.cpp b/src/lib/data/location/TokenLocationCollection.cpp index a4e606b6..1f8e146f 100644 --- a/src/lib/data/location/TokenLocationCollection.cpp +++ b/src/lib/data/location/TokenLocationCollection.cpp @@ -101,14 +101,7 @@ TokenLocation* TokenLocationCollection::findTokenLocationById(Id id) const TokenLocationFile* TokenLocationCollection::findTokenLocationFileByPath(const FilePath& filePath) const { - std::map>::const_iterator it = - find_if(m_files.begin(), m_files.end(), - [&](const std::pair>& p) - { - return p.first == filePath; - } - ); - + std::map>::const_iterator it = m_files.find(filePath); if (it != m_files.end()) { return it->second.get();