logic: improved fulltext search performance (issue #680)

* move to multithreaded implementation
* omit unnecessary work
This commit is contained in:
mlangkabel
2019-03-12 11:16:49 +01:00
parent b6031fdb3a
commit db569ee551
4 changed files with 84 additions and 57 deletions
@@ -29,16 +29,21 @@ std::vector<FullTextSearchResult> FullTextSearchIndex::searchForTerm(const std::
TRACE();
std::vector<FullTextSearchResult> ret;
FullTextSearchResult hit;
{
std::lock_guard<std::mutex> lock(m_filesMutex);
for (auto& f : m_files)
{
FullTextSearchResult hit;
hit.fileId = f.fileId;
hit.positions = f.array.searchForTerm(term);
ret.push_back(hit);
std::sort(hit.positions.begin(), hit.positions.end());
if (!hit.positions.empty())
{
ret.push_back(hit);
}
}
}
return ret;
}
+61 -44
View File
@@ -552,58 +552,75 @@ std::shared_ptr<SourceLocationCollection> PersistentStorage::getFullTextSearchLo
false, true
).dispatch();
const int termLength = searchTerm.length();
for (const FullTextSearchResult& fileHits : m_fullTextSearchIndex.searchForTerm(searchTerm))
{
const FilePath filePath = getFileNodePath(fileHits.fileId);
std::shared_ptr<TextAccess> fileContent = getFileContent(filePath, false);
int charsTotal = 0;
int lineNumber = 1;
std::wstring line = codec.decode(fileContent->getLine(lineNumber));
for (int pos : fileHits.positions)
std::vector<std::shared_ptr<std::thread>> threads;
std::mutex collectionMutex;
for (std::vector<FullTextSearchResult> fileResults : utility::splitToEqualySizedParts(m_fullTextSearchIndex.searchForTerm(searchTerm), utility::getIdealThreadCount()))
{
while (charsTotal + (int)line.length() <= pos)
{
charsTotal += line.length();
lineNumber++;
line = codec.decode(fileContent->getLine(lineNumber));
}
std::shared_ptr<std::thread> thread = std::make_shared<std::thread>(
[this, &searchTerm, &caseSensitive, &codec, /*no ref here!*/fileResults, &collection, &collectionMutex]()
{
const int termLength = searchTerm.length();
for (const FullTextSearchResult& fileResult : fileResults)
{
const FilePath filePath = getFileNodePath(fileResult.fileId);
std::shared_ptr<TextAccess> fileContent = getFileContent(filePath, false);
ParseLocation location;
location.startLineNumber = lineNumber;
location.startColumnNumber = pos - charsTotal + 1;
int charsTotal = 0;
int lineNumber = 1;
std::wstring line = codec.decode(fileContent->getLine(lineNumber));
if (caseSensitive && line.substr(location.startColumnNumber - 1, termLength) != searchTerm)
{
continue;
}
for (int pos : fileResult.positions)
{
while (charsTotal + (int)line.length() <= pos)
{
charsTotal += line.length();
lineNumber++;
line = codec.decode(fileContent->getLine(lineNumber));
}
while ((charsTotal + (int)line.length()) < pos + termLength)
{
charsTotal += line.length();
lineNumber++;
line = codec.decode(fileContent->getLine(lineNumber));
}
ParseLocation location;
location.startLineNumber = lineNumber;
location.startColumnNumber = pos - charsTotal + 1;
location.endLineNumber = lineNumber;
location.endColumnNumber = pos + termLength - charsTotal;
if (caseSensitive && line.substr(location.startColumnNumber - 1, termLength) != searchTerm)
{
continue;
}
while ((charsTotal + (int)line.length()) < pos + termLength)
{
charsTotal += line.length();
lineNumber++;
line = codec.decode(fileContent->getLine(lineNumber));
}
location.endLineNumber = lineNumber;
location.endColumnNumber = pos + termLength - charsTotal;
// Set first bit to 1 to avoid collisions
const Id locationId = ~(~Id(0) >> 1) + collection->getSourceLocationCount() + 1;
collection->addSourceLocation(
LOCATION_FULLTEXT_SEARCH,
locationId,
std::vector<Id>(),
filePath,
location.startLineNumber,
location.startColumnNumber,
location.endLineNumber,
location.endColumnNumber
{
std::lock_guard<std::mutex> lock(collectionMutex);
// Set first bit to 1 to avoid collisions
const Id locationId = ~(~Id(0) >> 1) + collection->getSourceLocationCount() + 1;
collection->addSourceLocation(
LOCATION_FULLTEXT_SEARCH,
locationId,
std::vector<Id>(),
filePath,
location.startLineNumber,
location.startColumnNumber,
location.endLineNumber,
location.endColumnNumber
);
}
}
}
}
);
threads.push_back(thread);
}
for (std::shared_ptr<std::thread> thread : threads)
{
thread->join();
}
}
+8 -11
View File
@@ -5,6 +5,9 @@
TextCodec::TextCodec(const std::string& name)
: m_name(name)
{
m_codec = QTextCodec::codecForName(m_name.c_str());
m_decoder = std::make_shared<QTextDecoder>(m_codec);
m_encoder = std::make_shared<QTextEncoder>(m_codec);
}
std::string TextCodec::getName() const
@@ -14,8 +17,7 @@ std::string TextCodec::getName() const
bool TextCodec::isValid() const
{
QTextCodec* codec = QTextCodec::codecForName(m_name.c_str());
if (codec)
if (m_codec)
{
return true;
}
@@ -24,23 +26,18 @@ bool TextCodec::isValid() const
std::wstring TextCodec::decode(const std::string& unicodeString) const
{
QTextCodec* codec = QTextCodec::codecForName(m_name.c_str());
if (codec)
if (m_decoder)
{
QTextDecoder decoder(codec);
return decoder.toUnicode(unicodeString.c_str()).toStdWString();
return m_decoder->toUnicode(unicodeString.c_str()).toStdWString();
}
return QString::fromStdString(unicodeString).toStdWString();
}
std::string TextCodec::encode(const std::wstring& string) const
{
QTextCodec* codec = QTextCodec::codecForName(m_name.c_str());
if (codec)
if (m_encoder)
{
QTextEncoder encoder(codec);
return encoder.fromUnicode(QString::fromStdWString(string)).toStdString();
return m_encoder->fromUnicode(QString::fromStdWString(string)).toStdString();
}
return QString::fromStdWString(string).toStdString();
}
+8
View File
@@ -2,6 +2,11 @@
#define TEXT_CODEC_H
#include <string>
#include <memory>
class QTextCodec;
class QTextDecoder;
class QTextEncoder;
class TextCodec
{
@@ -17,6 +22,9 @@ public:
private:
const std::string m_name;
QTextCodec* m_codec;
std::shared_ptr<QTextDecoder> m_decoder;
std::shared_ptr<QTextEncoder> m_encoder;
};
#endif // TEXT_CODEC_H