Skip to content

Commit 1e461af

Browse files
committed
Refactoring: reduce lock contention when writing tooltips
1 parent dbfd3c1 commit 1e461af

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/tooltip.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
#include "doxygen.h"
2929
#include "config.h"
3030

31-
static std::mutex g_tooltipsMutex;
31+
static std::mutex g_tooltipsFileMutex;
32+
static std::mutex g_tooltipsTipMutex;
3233
static std::unordered_map<int, std::unordered_set<std::string> > g_tooltipsWrittenPerFile;
3334

3435
class TooltipManager::Private
@@ -87,20 +88,34 @@ void TooltipManager::addTooltip(const Definition *d)
8788

8889
void TooltipManager::writeTooltips(OutputCodeList &ol)
8990
{
91+
std::unordered_map<int, std::unordered_set<std::string> >::iterator it;
9092
// critical section
91-
std::lock_guard<std::mutex> lock(g_tooltipsMutex);
92-
93-
int id = ol.id();
94-
auto it = g_tooltipsWrittenPerFile.find(id);
95-
if (it==g_tooltipsWrittenPerFile.end()) // new file
9693
{
97-
it = g_tooltipsWrittenPerFile.insert(std::make_pair(id,std::unordered_set<std::string>())).first;
94+
std::lock_guard<std::mutex> lock(g_tooltipsFileMutex);
95+
96+
int id = ol.id();
97+
it = g_tooltipsWrittenPerFile.find(id);
98+
if (it==g_tooltipsWrittenPerFile.end()) // new file
99+
{
100+
it = g_tooltipsWrittenPerFile.insert(std::make_pair(id,std::unordered_set<std::string>())).first;
101+
}
98102
}
99103

100104
for (const auto &[name,d] : p->tooltipInfo)
101105
{
102-
bool written = it->second.find(name)!=it->second.end();
103-
if (!written) // only write tooltips once
106+
bool written = false;
107+
108+
// critical section
109+
{
110+
std::lock_guard<std::mutex> lock(g_tooltipsTipMutex);
111+
it->second.find(name)!=it->second.end();
112+
if (!written) // only write tooltips once
113+
{
114+
it->second.insert(name); // remember we wrote this tooltip for the given file id
115+
}
116+
}
117+
118+
if (!written)
104119
{
105120
//printf("%p: writeTooltips(%s) ol=%d\n",this,name.c_str(),ol.id());
106121
DocLinkInfo docInfo;
@@ -133,7 +148,6 @@ void TooltipManager::writeTooltips(OutputCodeList &ol)
133148
defInfo,
134149
declInfo
135150
);
136-
it->second.insert(name); // remember we wrote this tooltip for the given file id
137151
}
138152
}
139153
}

0 commit comments

Comments
 (0)