From 9d26104d2f16a52e6f518f5bf5503eae9e9bad4d Mon Sep 17 00:00:00 2001 From: technateNG Date: Wed, 29 Jan 2020 17:30:22 +0000 Subject: [PATCH] perf: PersistentStorage::getGraphForAll - avoiding second sqlite call. (#890) * perf: PersistentStorage::getGraphForAll - avoiding second sqlite call. * std::map -> std::unordered_map for m_symbolDefinitionKinds. * src: Added PersistentStorage::addFileNodeToGraph. * src: Refactor and Bugfixes in new implementation of getGraphForAll. * std::map -> std::unordered_map for m_fileNodeIndexed. * Added PersistentStorage::addNodeToGraph. --- src/lib/data/storage/PersistentStorage.cpp | 111 +++++++++++---------- src/lib/data/storage/PersistentStorage.h | 7 +- 2 files changed, 65 insertions(+), 53 deletions(-) diff --git a/src/lib/data/storage/PersistentStorage.cpp b/src/lib/data/storage/PersistentStorage.cpp index c4e9a7b1..710354af 100644 --- a/src/lib/data/storage/PersistentStorage.cpp +++ b/src/lib/data/storage/PersistentStorage.cpp @@ -1024,35 +1024,36 @@ std::shared_ptr PersistentStorage::getGraphForAll() const { TRACE(); - std::vector tokenIds; - - m_sqliteIndexStorage.forEach([&](StorageNode&& node) { - bool showNode = true; - if (m_symbolDefinitionKinds.size()) + std::shared_ptr graph = std::make_shared(); + const size_t sdk_size = m_symbolDefinitionKinds.size(); + m_sqliteIndexStorage.forEach([&, sdk_size](StorageNode&& storageNode) { + const NodeType type(NodeType::intToType(storageNode.type)); + if (type.isFile()) { - auto it = m_symbolDefinitionKinds.find(node.id); - showNode = (it != m_symbolDefinitionKinds.end() && it->second == DEFINITION_EXPLICIT); + auto fn_it = m_fileNodeIndexed.find(storageNode.id); + if (fn_it != m_fileNodeIndexed.end() && fn_it->second) + { + addFileNodeToGraph(storageNode, graph.get()); + } } - - if (showNode && - (NodeType(NodeType::intToType(node.type)).isPackage() || - !m_hierarchyCache.isChildOfVisibleNodeOrInvisible(node.id))) + else { - tokenIds.push_back(node.id); + bool showNode = true; + if (sdk_size) + { + auto it = m_symbolDefinitionKinds.find(storageNode.id); + showNode = (it != m_symbolDefinitionKinds.end() && it->second == DEFINITION_EXPLICIT); + } + if (showNode && ( + type.isPackage() || + !m_hierarchyCache.isChildOfVisibleNodeOrInvisible(storageNode.id) + ) + ) + { + addNodeToGraph(storageNode, type, graph.get(), false); + } } }); - - for (const auto& p: m_fileNodeIndexed) - { - if (p.second) - { - tokenIds.push_back(p.first); - } - } - - std::shared_ptr graph = std::make_shared(); - addNodesToGraph(tokenIds, graph.get(), false); - return graph; } @@ -2843,42 +2844,52 @@ void PersistentStorage::addNodesToGraph( for (const StorageNode& storageNode: m_sqliteIndexStorage.getAllByIds(nodeIds)) { - NameHierarchy nameHierarchy = NameHierarchy::deserialize(storageNode.serializedName); - const NodeType type(NodeType::intToType(storageNode.type)); if (type.isFile()) { - const FilePath filePath(nameHierarchy.getRawName()); - - bool complete = getFileNodeComplete(storageNode.id); - bool indexed = getFileNodeIndexed(storageNode.id); - - Node* node = graph->createNode( - storageNode.id, - type, - NameHierarchy(filePath.fileName(), NAME_DELIMITER_FILE), - indexed ? DEFINITION_EXPLICIT : DEFINITION_NONE); - node->addComponent(std::make_shared(filePath, complete)); + addFileNodeToGraph(storageNode, graph); } else { - DefinitionKind defKind = DEFINITION_NONE; - auto it = m_symbolDefinitionKinds.find(storageNode.id); - if (it != m_symbolDefinitionKinds.end()) - { - defKind = it->second; - } - - Node* node = graph->createNode(storageNode.id, type, std::move(nameHierarchy), defKind); - - if (addChildCount) - { - node->setChildCount(m_hierarchyCache.getFirstChildIdsCountForNodeId(storageNode.id)); - } + addNodeToGraph(storageNode, type, graph, addChildCount); } } } +void PersistentStorage::addFileNodeToGraph(const StorageNode& storageNode, Graph* const graph) const +{ + NameHierarchy nameHierarchy = NameHierarchy::deserialize(storageNode.serializedName); + const FilePath filePath(nameHierarchy.getRawName()); + + bool complete = getFileNodeComplete(storageNode.id); + bool indexed = getFileNodeIndexed(storageNode.id); + + Node* node = graph->createNode( + storageNode.id, + NodeType::NODE_FILE, + NameHierarchy(filePath.fileName(), NAME_DELIMITER_FILE), + indexed ? DEFINITION_EXPLICIT : DEFINITION_NONE); + node->addComponent(std::make_shared(filePath, complete)); +} + +void PersistentStorage::addNodeToGraph(const StorageNode& newNode, const NodeType& type, Graph* graph, bool addChildCount) const +{ + NameHierarchy nameHierarchy = NameHierarchy::deserialize(newNode.serializedName); + DefinitionKind defKind = DEFINITION_NONE; + auto it = m_symbolDefinitionKinds.find(newNode.id); + if (it != m_symbolDefinitionKinds.end()) + { + defKind = it->second; + } + + Node* node = graph->createNode(newNode.id, type, std::move(nameHierarchy), defKind); + + if (addChildCount) + { + node->setChildCount(m_hierarchyCache.getFirstChildIdsCountForNodeId(newNode.id)); + } +} + void PersistentStorage::addEdgesToGraph(const std::vector& newEdgeIds, Graph* graph) const { TRACE(); diff --git a/src/lib/data/storage/PersistentStorage.h b/src/lib/data/storage/PersistentStorage.h index b8cfa56e..24ff8164 100644 --- a/src/lib/data/storage/PersistentStorage.h +++ b/src/lib/data/storage/PersistentStorage.h @@ -241,7 +241,8 @@ private: const std::vector& edgeIds, Graph* graphh, bool addChildCount) const; - + inline void addFileNodeToGraph(const StorageNode& storageNode, Graph* const graph) const; + void addNodeToGraph(const StorageNode& newNode, const NodeType& type, Graph* graph, bool addChildCount) const; void addAggregationEdgesToGraph( Id nodeId, const std::vector& edgesToAggregate, Graph* graph) const; void addFileContentsToGraph(Id fileId, Graph* graph) const; @@ -276,10 +277,10 @@ private: std::map m_lowerCasefileNodeIds; std::map m_fileNodePaths; std::map m_fileNodeComplete; - std::map m_fileNodeIndexed; + std::unordered_map m_fileNodeIndexed; std::map m_fileNodeLanguage; - std::map m_symbolDefinitionKinds; + std::unordered_map m_symbolDefinitionKinds; std::map m_memberEdgeIdOrderMap; HierarchyCache m_hierarchyCache;