Skip to content

Commit 12c840c

Browse files
committed
Improve performance when there are many differences
1 parent 9fbd12f commit 12c840c

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/WinWebDiffLib/DOMUtils.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <rapidjson/prettywriter.h>
88
#include <rapidjson/stringbuffer.h>
99
#include <string>
10+
#include <unordered_map>
1011

1112
using WDocument = rapidjson::GenericDocument<rapidjson::UTF16<>>;
1213
using WValue = rapidjson::GenericValue<rapidjson::UTF16<>>;
@@ -92,6 +93,32 @@ namespace domutils
9293
}
9394
}
9495

96+
void makeNodeIdToNodeMap(WValue& nodeTree, std::unordered_map<int, WValue*>& map)
97+
{
98+
map.insert_or_assign(nodeTree[L"nodeId"].GetInt(), &nodeTree);
99+
if (nodeTree.HasMember(L"children") && nodeTree[L"children"].IsArray())
100+
{
101+
for (auto& child : nodeTree[L"children"].GetArray())
102+
{
103+
map.insert_or_assign(child[L"nodeId"].GetInt(), &child);
104+
makeNodeIdToNodeMap(child, map);
105+
}
106+
}
107+
if (nodeTree.HasMember(L"contentDocument"))
108+
{
109+
auto& contentDocument = nodeTree[L"contentDocument"];
110+
map.insert_or_assign(contentDocument[L"nodeId"].GetInt(), &contentDocument);
111+
makeNodeIdToNodeMap(contentDocument, map);
112+
}
113+
}
114+
115+
std::unordered_map<int, WValue*> makeNodeIdToNodeMap(WValue& nodeTree)
116+
{
117+
std::unordered_map<int, WValue*> map;
118+
makeNodeIdToNodeMap(nodeTree, map);
119+
return map;
120+
}
121+
95122
std::pair<WValue*, WValue*> findNodeId(WValue& nodeTree, int nodeId)
96123
{
97124
if (nodeTree[L"nodeId"].GetInt() == nodeId)

src/WinWebDiffLib/DiffHighlighter.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,9 @@ class Highlighter
709709

710710
void highlightNodes()
711711
{
712+
std::unordered_map<int, WValue*> map[3];
713+
for (size_t pane = 0; pane < m_documents.size(); ++pane)
714+
map[pane] = domutils::makeNodeIdToNodeMap(m_documents[pane][L"root"]);
712715
for (size_t i = 0; i < m_diffInfoList.size(); ++i)
713716
{
714717
const auto& diffInfo = m_diffInfoList[i];
@@ -717,8 +720,7 @@ class Highlighter
717720
std::vector<DiffInfo> wordDiffInfoList;
718721
for (size_t pane = 0; pane < m_documents.size(); ++pane)
719722
{
720-
std::pair<WValue*, WValue*> pair = domutils::findNodeId(m_documents[pane][L"root"], diffInfo.nodeIds[pane]);
721-
pvalues[pane] = pair.first;
723+
pvalues[pane] = map[pane][diffInfo.nodeIds[pane]];
722724
if (diffInfo.nodePos[pane] == 0 && pvalues[pane])
723725
textSegments[pane].Make((*pvalues[pane])[L"nodeValue"].GetString(), m_diffOptions.ignoreNumbers);
724726
else

0 commit comments

Comments
 (0)