src: Improved code view snippet generation performance

This commit is contained in:
Eberhard Graether
2018-03-22 01:46:35 +01:00
parent ce78412190
commit 0d79639c96
10 changed files with 137 additions and 104 deletions
+32 -55
View File
@@ -468,11 +468,6 @@ std::vector<CodeSnippetParams> CodeController::getSnippetsForFileWithState(
return snippets;
}
if (!addSourceLocations)
{
file->setIsWhole(false);
}
snippets = getSnippetsForFile(file, addSourceLocations);
}
break;
@@ -594,45 +589,33 @@ std::vector<CodeSnippetParams> CodeController::getSnippetsForFile(
{
TRACE();
std::shared_ptr<SourceLocationFile> fileLocations =
m_storageAccess->getSourceLocationsForFile(activeSourceLocations->getFilePath());
std::shared_ptr<SourceLocationFile> scopeLocations = fileLocations->getFilteredByType(LOCATION_SCOPE);
std::shared_ptr<TextAccess> textAccess = m_storageAccess->getFileContent(activeSourceLocations->getFilePath());
std::deque<SnippetMerger::Range> ranges;
if (activeSourceLocations->isWhole())
{
ranges.push_back(SnippetMerger::Range(
SnippetMerger::Border(1, true),
SnippetMerger::Border(textAccess->getLineCount(), true)
));
}
else
{
SnippetMerger fileScopedMerger(1, textAccess->getLineCount());
std::map<int, std::shared_ptr<SnippetMerger>> mergers;
activeSourceLocations->forEachStartSourceLocation(
[&](SourceLocation* location)
{
buildMergerHierarchy(location, scopeLocations, fileScopedMerger, mergers);
}
);
SnippetMerger fileScopedMerger(1, textAccess->getLineCount());
std::map<int, std::shared_ptr<SnippetMerger>> mergers;
std::vector<SnippetMerger::Range> atomicRanges;
m_storageAccess->getCommentLocationsInFile(activeSourceLocations->getFilePath())->forEachStartSourceLocation(
[&](SourceLocation* location)
{
atomicRanges.push_back(SnippetMerger::Range(
SnippetMerger::Border(location->getLineNumber(), false),
SnippetMerger::Border(location->getOtherLocation()->getLineNumber(), false)
));
}
);
atomicRanges = SnippetMerger::Range::mergeAdjacent(atomicRanges);
std::shared_ptr<SourceLocationFile> scopeLocations =
m_storageAccess->getSourceLocationsOfTypeInFile(activeSourceLocations->getFilePath(), LOCATION_SCOPE);
activeSourceLocations->forEachStartSourceLocation(
[&](SourceLocation* location)
{
buildMergerHierarchy(location, scopeLocations, fileScopedMerger, mergers);
}
);
ranges = fileScopedMerger.merge(atomicRanges);
}
std::vector<SnippetMerger::Range> atomicRanges;
m_storageAccess->getCommentLocationsInFile(activeSourceLocations->getFilePath())->forEachStartSourceLocation(
[&](SourceLocation* location)
{
atomicRanges.push_back(SnippetMerger::Range(
SnippetMerger::Border(location->getLineNumber(), false),
SnippetMerger::Border(location->getOtherLocation()->getLineNumber(), false)
));
}
);
atomicRanges = SnippetMerger::Range::mergeAdjacent(atomicRanges);
std::deque<SnippetMerger::Range> ranges = fileScopedMerger.merge(atomicRanges);
const int snippetExpandRange = ApplicationSettings::getInstance()->getCodeSnippetExpandRange();
std::vector<CodeSnippetParams> snippets;
@@ -650,8 +633,8 @@ std::vector<CodeSnippetParams> CodeController::getSnippetsForFile(
params.titleId = 0;
params.footerId = 0;
std::shared_ptr<SourceLocationFile> tempFile =
fileLocations->getFilteredByLines(params.startLineNumber, params.endLineNumber);
std::shared_ptr<SourceLocationFile> tempFile = m_storageAccess->getSourceLocationsForLinesInFile(
activeSourceLocations->getFilePath(), params.startLineNumber, params.endLineNumber);
const SourceLocation* firstSourceLocation =
tempFile->getSourceLocations().size() ? tempFile->getSourceLocations().begin()->get() : nullptr;
@@ -671,7 +654,7 @@ std::vector<CodeSnippetParams> CodeController::getSnippetsForFile(
);
}
if (!activeSourceLocations->isWhole() && params.titleId == 0)
if (params.titleId == 0)
{
params.title = activeSourceLocations->getFilePath().wstr();
}
@@ -698,25 +681,19 @@ std::vector<CodeSnippetParams> CodeController::getSnippetsForFile(
params.code += line;
}
snippets.push_back(params);
}
if (addSourceLocations && !activeSourceLocations->isWhole())
{
for (CodeSnippetParams& params : snippets)
if (addSourceLocations)
{
std::shared_ptr<SourceLocationFile> lines =
fileLocations->getFilteredByLines(params.startLineNumber, params.endLineNumber);
params.locationFile->forEachSourceLocation(
[&lines](SourceLocation* location)
[&tempFile](SourceLocation* location)
{
lines->addSourceLocationCopy(location);
tempFile->addSourceLocationCopy(location);
}
);
params.locationFile = lines;
params.locationFile = tempFile;
}
snippets.push_back(params);
}
return snippets;
+5 -1
View File
@@ -11,6 +11,7 @@
#include "data/bookmark/EdgeBookmark.h"
#include "data/bookmark/NodeBookmark.h"
#include "data/graph/Node.h"
#include "data/location/LocationType.h"
#include "data/search/SearchMatch.h"
#include "data/storage/type/StorageEdge.h"
#include "data/storage/StorageStats.h"
@@ -66,9 +67,12 @@ public:
const std::vector<Id>& tokenIds) const = 0;
virtual std::shared_ptr<SourceLocationCollection> getSourceLocationsForLocationIds(
const std::vector<Id>& locationIds) const = 0;
virtual std::shared_ptr<SourceLocationFile> getSourceLocationsForFile(const FilePath& filePath) const = 0;
virtual std::shared_ptr<SourceLocationFile> getSourceLocationsForLinesInFile(
const FilePath& filePath, uint firstLineNumber, uint lastLineNumber) const = 0;
const FilePath& filePath, size_t startLine, size_t endLine) const = 0;
virtual std::shared_ptr<SourceLocationFile> getSourceLocationsOfTypeInFile(
const FilePath& filePath, LocationType type) const = 0;
virtual std::shared_ptr<SourceLocationFile> getCommentLocationsInFile(const FilePath& filePath) const = 0;
+14 -2
View File
@@ -250,12 +250,24 @@ std::shared_ptr<SourceLocationFile> StorageAccessProxy::getSourceLocationsForFil
}
std::shared_ptr<SourceLocationFile> StorageAccessProxy::getSourceLocationsForLinesInFile(
const FilePath& filePath, uint firstLineNumber, uint lastLineNumber
const FilePath& filePath, size_t startLine, size_t endLine
) const
{
if (hasSubject())
{
return m_subject->getSourceLocationsForLinesInFile(filePath, firstLineNumber, lastLineNumber);
return m_subject->getSourceLocationsForLinesInFile(filePath, startLine, endLine);
}
return std::make_shared<SourceLocationFile>(FilePath(), false, false);
}
std::shared_ptr<SourceLocationFile> StorageAccessProxy::getSourceLocationsOfTypeInFile(
const FilePath& filePath, LocationType type
) const
{
if (hasSubject())
{
return m_subject->getSourceLocationsOfTypeInFile(filePath, type);
}
return std::make_shared<SourceLocationFile>(FilePath(), false, false);
+4 -2
View File
@@ -52,10 +52,12 @@ public:
virtual std::shared_ptr<SourceLocationCollection> getSourceLocationsForLocationIds(
const std::vector<Id>& locationIds
) const override;
virtual std::shared_ptr<SourceLocationFile> getSourceLocationsForFile(const FilePath& filePath) const override;
virtual std::shared_ptr<SourceLocationFile> getSourceLocationsForLinesInFile(
const FilePath& filePath, uint firstLineNumber, uint lastLineNumber
) const override;
const FilePath& filePath, size_t startLine, size_t endLine) const override;
virtual std::shared_ptr<SourceLocationFile> getSourceLocationsOfTypeInFile(
const FilePath& filePath, LocationType type) const override;
virtual std::shared_ptr<SourceLocationFile> getCommentLocationsInFile(const FilePath& filePath) const override;
+50 -36
View File
@@ -1118,21 +1118,23 @@ std::vector<Id> PersistentStorage::getActiveTokenIdsForId(Id tokenId, Id* declar
std::vector<Id> activeTokenIds;
if (!(m_sqliteIndexStorage.isEdge(tokenId) || m_sqliteIndexStorage.isNode(tokenId)))
bool isNode = m_sqliteIndexStorage.isNode(tokenId);
bool isEdge = m_sqliteIndexStorage.isEdge(tokenId);
if (!isEdge && !isNode)
{
return activeTokenIds;
}
activeTokenIds.push_back(tokenId);
if (m_sqliteIndexStorage.isNode(tokenId))
if (isNode)
{
*declarationId = tokenId;
const std::vector<StorageEdge> incomingEdges = m_sqliteIndexStorage.getEdgesByTargetId(tokenId);
for (size_t i = 0; i < incomingEdges.size(); i++)
for (const StorageEdge& edge : m_sqliteIndexStorage.getEdgesByTargetId(tokenId))
{
activeTokenIds.push_back(incomingEdges[i].id);
activeTokenIds.push_back(edge.id);
}
}
@@ -1247,42 +1249,44 @@ std::shared_ptr<SourceLocationCollection> PersistentStorage::getSourceLocationsF
for (const StorageSourceLocation& sourceLocation: m_sqliteIndexStorage.getAllByIds<StorageSourceLocation>(locationIds))
{
auto it = locationIdToElementIdMap.find(sourceLocation.id);
if (it != locationIdToElementIdMap.end())
const LocationType type = intToLocationType(sourceLocation.type);
if (type == LOCATION_QUALIFIER)
{
const LocationType type = intToLocationType(sourceLocation.type);
if (type == LOCATION_QUALIFIER)
{
continue;
}
continue;
}
FilePath path = getFileNodePath(sourceLocation.fileNodeId);
if (path.empty())
auto it = locationIdToElementIdMap.find(sourceLocation.id);
if (it == locationIdToElementIdMap.end())
{
continue;
}
FilePath path = getFileNodePath(sourceLocation.fileNodeId);
if (path.empty())
{
const StorageNode fileNode = m_sqliteIndexStorage.getNodeById(sourceLocation.fileNodeId);
if (fileNode.id)
{
const StorageNode fileNode = m_sqliteIndexStorage.getNodeById(sourceLocation.fileNodeId);
if (fileNode.id)
const FilePath path2 = FilePath(NameHierarchy::deserialize(fileNode.serializedName).getQualifiedName());
if (path2.exists())
{
const FilePath path2 = FilePath(NameHierarchy::deserialize(fileNode.serializedName).getQualifiedName());
if (path2.exists())
{
path = path2;
}
path = path2;
}
}
}
if (!path.empty())
{
collection->addSourceLocation(
type,
sourceLocation.id,
std::vector<Id>(1, it->second),
path,
sourceLocation.startLine,
sourceLocation.startCol,
sourceLocation.endLine,
sourceLocation.endCol
);
}
if (!path.empty())
{
collection->addSourceLocation(
type,
sourceLocation.id,
std::vector<Id>(1, it->second),
path,
sourceLocation.startLine,
sourceLocation.startCol,
sourceLocation.endLine,
sourceLocation.endCol
);
}
}
}
@@ -1333,12 +1337,22 @@ std::shared_ptr<SourceLocationFile> PersistentStorage::getSourceLocationsForFile
}
std::shared_ptr<SourceLocationFile> PersistentStorage::getSourceLocationsForLinesInFile(
const FilePath& filePath, uint firstLineNumber, uint lastLineNumber
const FilePath& filePath, size_t startLine, size_t endLine
) const
{
TRACE();
return getSourceLocationsForFile(filePath)->getFilteredByLines(firstLineNumber, lastLineNumber);
return m_sqliteIndexStorage.getSourceLocationsForLinesInFile(
filePath, startLine, endLine)->getFilteredByLines(startLine, endLine);
}
std::shared_ptr<SourceLocationFile> PersistentStorage::getSourceLocationsOfTypeInFile(
const FilePath& filePath, LocationType type
) const
{
TRACE();
return m_sqliteIndexStorage.getSourceLocationsOfTypeInFile(filePath, type);
}
std::shared_ptr<SourceLocationFile> PersistentStorage::getCommentLocationsInFile(const FilePath& filePath) const
+3 -2
View File
@@ -109,8 +109,9 @@ public:
virtual std::shared_ptr<SourceLocationFile> getSourceLocationsForFile(const FilePath& filePath) const override;
virtual std::shared_ptr<SourceLocationFile> getSourceLocationsForLinesInFile(
const FilePath& filePath, uint firstLineNumber, uint lastLineNumber
) const override;
const FilePath& filePath, size_t startLine, size_t endLine) const override;
virtual std::shared_ptr<SourceLocationFile> getSourceLocationsOfTypeInFile(
const FilePath& filePath, LocationType type) const override;
virtual std::shared_ptr<SourceLocationFile> getCommentLocationsInFile(const FilePath& filePath) const override;
@@ -643,7 +643,8 @@ void SqliteIndexStorage::setNodeType(int type, Id nodeId)
);
}
std::shared_ptr<SourceLocationFile> SqliteIndexStorage::getSourceLocationsForFile(const FilePath& filePath) const
std::shared_ptr<SourceLocationFile> SqliteIndexStorage::getSourceLocationsForFile(
const FilePath& filePath, const std::string& query) const
{
std::shared_ptr<SourceLocationFile> ret = std::make_shared<SourceLocationFile>(filePath, true, false);
@@ -658,7 +659,7 @@ std::shared_ptr<SourceLocationFile> SqliteIndexStorage::getSourceLocationsForFil
std::vector<Id> sourceLocationIds;
std::unordered_map<Id, StorageSourceLocation> sourceLocationIdToData;
for (const StorageSourceLocation& storageLocation:
doGetAll<StorageSourceLocation>("WHERE file_node_id == " + std::to_string(file.id)))
doGetAll<StorageSourceLocation>("WHERE file_node_id == " + std::to_string(file.id) + " " + query))
{
sourceLocationIds.push_back(storageLocation.id);
sourceLocationIdToData[storageLocation.id] = storageLocation;
@@ -690,6 +691,19 @@ std::shared_ptr<SourceLocationFile> SqliteIndexStorage::getSourceLocationsForFil
return ret;
}
std::shared_ptr<SourceLocationFile> SqliteIndexStorage::getSourceLocationsForLinesInFile(
const FilePath& filePath, size_t startLine, size_t endLine) const
{
return getSourceLocationsForFile(filePath,
"AND start_line <= " + std::to_string(endLine) + " AND end_line >= " + std::to_string(startLine));
}
std::shared_ptr<SourceLocationFile> SqliteIndexStorage::getSourceLocationsOfTypeInFile(
const FilePath& filePath, LocationType type) const
{
return getSourceLocationsForFile(filePath, "AND type == " + std::to_string(locationTypeToInt(type)));
}
std::vector<StorageOccurrence> SqliteIndexStorage::getOccurrencesForLocationId(Id locationId) const
{
std::vector<Id> locationIds {locationId};
@@ -779,6 +793,10 @@ std::vector<std::pair<int, SqliteDatabaseIndex>> SqliteIndexStorage::getIndices(
STORAGE_MODE_READ | STORAGE_MODE_CLEAR,
SqliteDatabaseIndex("source_location_file_node_id_index", "source_location(file_node_id)")
));
indices.push_back(std::make_pair(
STORAGE_MODE_READ,
SqliteDatabaseIndex("source_location_file_node_id_type_index", "source_location(file_node_id, type)")
));
indices.push_back(std::make_pair(
STORAGE_MODE_WRITE,
SqliteDatabaseIndex("source_location_all_data_index", "source_location(file_node_id, start_line, start_column, end_line, end_column, type)")
@@ -88,7 +88,12 @@ public:
void setFileComplete(bool complete, Id fileId);
void setNodeType(int type, Id nodeId);
std::shared_ptr<SourceLocationFile> getSourceLocationsForFile(const FilePath& filePath) const;
std::shared_ptr<SourceLocationFile> getSourceLocationsForFile(
const FilePath& filePath, const std::string& query = "") const;
std::shared_ptr<SourceLocationFile> getSourceLocationsForLinesInFile(
const FilePath& filePath, size_t startLine, size_t endLine) const;
std::shared_ptr<SourceLocationFile> getSourceLocationsOfTypeInFile(
const FilePath& filePath, LocationType type) const;
std::vector<StorageOccurrence> getOccurrencesForLocationId(Id locationId) const;
std::vector<StorageOccurrence> getOccurrencesForLocationIds(const std::vector<Id>& locationIds) const;
+1 -1
View File
@@ -24,6 +24,7 @@ std::shared_ptr<TraceEvent> Tracer::startEvent(const std::string& eventName)
std::shared_ptr<TraceEvent> event =
std::make_shared<TraceEvent>(eventName, s_nextTraceId++, m_startedEvents[id].size());
m_events[id].push_back(event);
m_startedEvents[id].push(event.get());
return event;
@@ -36,7 +37,6 @@ void Tracer::finishEvent(std::shared_ptr<TraceEvent> event)
const std::thread::id id = std::this_thread::get_id();
m_startedEvents[id].pop();
m_events[id].push_back(event);
}
void Tracer::printTraces()
+2 -2
View File
@@ -146,8 +146,8 @@ ScopedTrace<TracerType>::~ScopedTrace()
#define PRINT_TRACES() \
Tracer::getInstance()->printTraces()
#endif
#else
#define TRACE(__name__)
#define PRINT_TRACES()