logic: Improved code view performance

* fixed bottleneck in TokenLocationCollection::findLocationFile()
* cache file ids and paths in PersistenStorage
This commit is contained in:
Eberhard Graether
2016-05-12 22:50:14 +02:00
parent 6a576bccc9
commit 245cafdc7b
5 changed files with 58 additions and 81 deletions
+35 -36
View File
@@ -248,6 +248,7 @@ void PersistentStorage::clearCaches()
{ {
m_elementIndex.clear(); m_elementIndex.clear();
m_fileNodeIds.clear(); m_fileNodeIds.clear();
m_fileNodePaths.clear();
m_hierarchyCache.clear(); m_hierarchyCache.clear();
} }
@@ -357,6 +358,7 @@ void PersistentStorage::startParsing()
void PersistentStorage::finishParsing() void PersistentStorage::finishParsing()
{ {
buildSearchIndex(); buildSearchIndex();
buildFilePathMaps();
buildHierarchyCache(); buildHierarchyCache();
optimizeFTSTable(); optimizeFTSTable();
} }
@@ -833,16 +835,16 @@ std::shared_ptr<TokenLocationCollection> PersistentStorage::getTokenLocationsFor
std::vector<Id> fileIds; std::vector<Id> fileIds;
std::vector<Id> nonFileIds; std::vector<Id> nonFileIds;
std::vector<Id> 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 else
{ {
nonFileIds.push_back(tokenIds[i]); nonFileIds.push_back(tokenId);
} }
} }
@@ -854,23 +856,15 @@ std::shared_ptr<TokenLocationCollection> PersistentStorage::getTokenLocationsFor
); );
} }
Cache<Id, std::string> filePathCache(
[this](Id id) -> std::string
{
return m_sqliteStorage.getFileById(id).filePath;
}
);
std::vector<StorageSourceLocation> locations = m_sqliteStorage.getTokenLocationsForElementIds(nonFileIds); std::vector<StorageSourceLocation> locations = m_sqliteStorage.getTokenLocationsForElementIds(nonFileIds);
for (size_t i = 0; i < locations.size(); i++) for (size_t i = 0; i < locations.size(); i++)
{ {
const StorageSourceLocation& location = locations[i]; const StorageSourceLocation& location = locations[i];
std::string filePath = filePathCache.getValue(location.fileNodeId);
TokenLocation* loc = collection->addTokenLocation( TokenLocation* loc = collection->addTokenLocation(
location.id, location.id,
location.elementId, location.elementId,
filePath, getFileNodePath(location.fileNodeId),
location.startLine, location.startLine,
location.startCol, location.startCol,
location.endLine, location.endLine,
@@ -1004,6 +998,12 @@ StorageStats PersistentStorage::getStorageStats() const
Id PersistentStorage::getFileNodeId(const FilePath& filePath) const Id PersistentStorage::getFileNodeId(const FilePath& filePath) const
{ {
if (filePath.empty())
{
LOG_ERROR("No file path set");
return 0;
}
std::map<FilePath, Id>::const_iterator it = m_fileNodeIds.find(filePath); std::map<FilePath, Id>::const_iterator it = m_fileNodeIds.find(filePath);
if (it != m_fileNodeIds.end()) if (it != m_fileNodeIds.end())
@@ -1011,35 +1011,25 @@ Id PersistentStorage::getFileNodeId(const FilePath& filePath) const
return it->second; return it->second;
} }
if (filePath.empty()) return 0;
{
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;
} }
FilePath PersistentStorage::getFileNodePath(Id fileId) const FilePath PersistentStorage::getFileNodePath(Id fileId) const
{ {
for (const std::pair<FilePath, Id>& p : m_fileNodeIds) if (fileId == 0)
{ {
if (p.second == fileId) LOG_ERROR("No file id set");
{ return FilePath();
return p.first;
}
} }
return m_sqliteStorage.getFileById(fileId).filePath; std::map<Id, FilePath>::const_iterator it = m_fileNodePaths.find(fileId);
if (it != m_fileNodePaths.end())
{
return it->second;
}
return FilePath();
} }
Id PersistentStorage::getLastVisibleParentNodeId(const Id nodeId) const Id PersistentStorage::getLastVisibleParentNodeId(const Id nodeId) const
@@ -1287,6 +1277,15 @@ void PersistentStorage::buildSearchIndex()
m_elementIndex.finishSetup(); 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() void PersistentStorage::buildHierarchyCache()
{ {
std::vector<StorageEdge> memberEdges = m_sqliteStorage.getEdgesByType(Edge::typeToInt(Edge::EDGE_MEMBER)); std::vector<StorageEdge> memberEdges = m_sqliteStorage.getEdgesByType(Edge::typeToInt(Edge::EDGE_MEMBER));
+3
View File
@@ -134,6 +134,7 @@ private:
void addComponentAccessToGraph(Graph* graph) const; void addComponentAccessToGraph(Graph* graph) const;
void buildSearchIndex(); void buildSearchIndex();
void buildFilePathMaps();
void buildHierarchyCache(); void buildHierarchyCache();
void optimizeFTSTable(); void optimizeFTSTable();
@@ -147,6 +148,8 @@ private:
SqliteStorage m_sqliteStorage; SqliteStorage m_sqliteStorage;
mutable std::map <FilePath, Id> m_fileNodeIds; mutable std::map <FilePath, Id> m_fileNodeIds;
mutable std::map <Id, FilePath> m_fileNodePaths;
HierarchyCache m_hierarchyCache; HierarchyCache m_hierarchyCache;
}; };
+2 -19
View File
@@ -482,23 +482,6 @@ std::vector<StorageFile> SqliteStorage::getFilesByPaths(const std::vector<FilePa
return getAll<StorageFile>("WHERE file.path IN ('" + utility::join(utility::toStrings(filePaths), "', '") + "')"); return getAll<StorageFile>("WHERE file.path IN ('" + utility::join(utility::toStrings(filePaths), "', '") + "')");
} }
std::vector<Id> SqliteStorage::getAllFileIds() const
{
std::vector<Id> 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<TextAccess> SqliteStorage::getFileContentByPath(const std::string& filePath) const std::shared_ptr<TextAccess> SqliteStorage::getFileContentByPath(const std::string& filePath) const
{ {
CppSQLite3Query q = m_database.execQuery(( CppSQLite3Query q = m_database.execQuery((
@@ -1113,8 +1096,8 @@ std::vector<StorageError> SqliteStorage::getAll<StorageError>(const std::string&
const std::string message = q.getStringField(0, ""); const std::string message = q.getStringField(0, "");
const bool fatal = q.getIntField(1, 0); const bool fatal = q.getIntField(1, 0);
const std::string filePath = q.getStringField(2, ""); const std::string filePath = q.getStringField(2, "");
const uint lineNumber = q.getIntField(3, -1); const int lineNumber = q.getIntField(3, -1);
const uint columnNumber = q.getIntField(4, -1); const int columnNumber = q.getIntField(4, -1);
if (lineNumber != -1 && columnNumber != -1) if (lineNumber != -1 && columnNumber != -1)
{ {
+17 -18
View File
@@ -79,7 +79,6 @@ public:
StorageFile getFileById(const Id id) const; StorageFile getFileById(const Id id) const;
StorageFile getFileByPath(const FilePath& filePath) const; StorageFile getFileByPath(const FilePath& filePath) const;
std::vector<Id> getAllFileIds() const;
std::vector<StorageFile> getFilesByPaths(const std::vector<FilePath>& filePaths) const; std::vector<StorageFile> getFilesByPaths(const std::vector<FilePath>& filePaths) const;
std::shared_ptr<TextAccess> getFileContentByPath(const std::string& filePath) const; std::shared_ptr<TextAccess> getFileContentByPath(const std::string& filePath) const;
@@ -129,23 +128,6 @@ private:
template <typename ResultType> template <typename ResultType>
std::vector<ResultType> getAll(const std::string& query) const; std::vector<ResultType> getAll(const std::string& query) const;
template <>
std::vector<StorageFile> getAll<StorageFile>(const std::string& query) const;
template <>
std::vector<StorageEdge> getAll<StorageEdge>(const std::string& query) const;
template <>
std::vector<StorageNode> getAll<StorageNode>(const std::string& query) const;
template <>
std::vector<StorageLocalSymbol> getAll<StorageLocalSymbol>(const std::string& query) const;
template <>
std::vector<StorageSourceLocation> getAll<StorageSourceLocation>(const std::string& query) const;
template <>
std::vector<StorageComponentAccess> getAll<StorageComponentAccess>(const std::string& query) const;
template <>
std::vector<StorageCommentLocation> getAll<StorageCommentLocation>(const std::string& query) const;
template <>
std::vector<StorageError> getAll<StorageError>(const std::string& query) const;
template <typename ResultType> template <typename ResultType>
ResultType getFirst(const std::string& query) const ResultType getFirst(const std::string& query) const
{ {
@@ -161,5 +143,22 @@ private:
FilePath m_dbFilePath; FilePath m_dbFilePath;
}; };
template <>
std::vector<StorageFile> SqliteStorage::getAll<StorageFile>(const std::string& query) const;
template <>
std::vector<StorageEdge> SqliteStorage::getAll<StorageEdge>(const std::string& query) const;
template <>
std::vector<StorageNode> SqliteStorage::getAll<StorageNode>(const std::string& query) const;
template <>
std::vector<StorageLocalSymbol> SqliteStorage::getAll<StorageLocalSymbol>(const std::string& query) const;
template <>
std::vector<StorageSourceLocation> SqliteStorage::getAll<StorageSourceLocation>(const std::string& query) const;
template <>
std::vector<StorageComponentAccess> SqliteStorage::getAll<StorageComponentAccess>(const std::string& query) const;
template <>
std::vector<StorageCommentLocation> SqliteStorage::getAll<StorageCommentLocation>(const std::string& query) const;
template <>
std::vector<StorageError> SqliteStorage::getAll<StorageError>(const std::string& query) const;
#endif // SQLITE_STORAGE_H #endif // SQLITE_STORAGE_H
@@ -101,14 +101,7 @@ TokenLocation* TokenLocationCollection::findTokenLocationById(Id id) const
TokenLocationFile* TokenLocationCollection::findTokenLocationFileByPath(const FilePath& filePath) const TokenLocationFile* TokenLocationCollection::findTokenLocationFileByPath(const FilePath& filePath) const
{ {
std::map<FilePath, std::shared_ptr<TokenLocationFile>>::const_iterator it = std::map<FilePath, std::shared_ptr<TokenLocationFile>>::const_iterator it = m_files.find(filePath);
find_if(m_files.begin(), m_files.end(),
[&](const std::pair<FilePath, std::shared_ptr<TokenLocationFile>>& p)
{
return p.first == filePath;
}
);
if (it != m_files.end()) if (it != m_files.end())
{ {
return it->second.get(); return it->second.get();