Commit 2b0954ca authored by Erik Verbruggen's avatar Erik Verbruggen
Browse files

C++: Remove class/function/enum extraction from CppLocatorData.



With every single change in a document the corresponding IndexItem
elements were extracted and put into an vector. The locator filters
then used these lists to iterate over and filter them.

This change removes that extraction, and the filtering now directly
iterates over the IndexItem elements with a callback.

The exception is the current document filter, because it also queries
for all declarations. Adding this to the model would result in a higher
memory usage, while that information is only used by this filter.

Change-Id: Ibe445cc11e9f68b5d807348fd46c7cac4aff4c85
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent 13d2d499
...@@ -125,7 +125,7 @@ public: ...@@ -125,7 +125,7 @@ public:
break; break;
if (m_fileNames.isEmpty() || m_fileNames.contains(it.value()->fileName())) { if (m_fileNames.isEmpty() || m_fileNames.contains(it.value()->fileName())) {
QVector<Core::SearchResultItem> resultItems; QVector<Core::SearchResultItem> resultItems;
search(it.value())->visitAllChildren([&](const IndexItem::Ptr &info) { auto filter = [&](const IndexItem::Ptr &info) -> IndexItem::VisitorResult {
if (matcher.indexIn(info->symbolName()) != -1) { if (matcher.indexIn(info->symbolName()) != -1) {
QString text = info->symbolName(); QString text = info->symbolName();
QString scope = info->symbolScope(); QString scope = info->symbolScope();
...@@ -148,7 +148,10 @@ public: ...@@ -148,7 +148,10 @@ public:
item.userData = qVariantFromValue(info); item.userData = qVariantFromValue(info);
resultItems << item; resultItems << item;
} }
});
return IndexItem::Recurse;
};
search(it.value())->visitAllChildren(filter);
if (!resultItems.isEmpty()) if (!resultItems.isEmpty())
future.reportResults(resultItems); future.reportResults(resultItems);
} }
......
...@@ -45,11 +45,6 @@ CppClassesFilter::~CppClassesFilter() ...@@ -45,11 +45,6 @@ CppClassesFilter::~CppClassesFilter()
{ {
} }
QList<QList<IndexItem::Ptr> > CppClassesFilter::itemsToMatchUserInputAgainst() const
{
return QList<QList<CppTools::IndexItem::Ptr> >() << m_data->classes();
}
Core::LocatorFilterEntry CppClassesFilter::filterEntryFromIndexItem(IndexItem::Ptr info) Core::LocatorFilterEntry CppClassesFilter::filterEntryFromIndexItem(IndexItem::Ptr info)
{ {
const QVariant id = qVariantFromValue(info); const QVariant id = qVariantFromValue(info);
......
...@@ -34,19 +34,22 @@ ...@@ -34,19 +34,22 @@
#include "cpplocatordata.h" #include "cpplocatordata.h"
#include "cpplocatorfilter.h" #include "cpplocatorfilter.h"
#include <utils/qtcoverride.h>
namespace CppTools { namespace CppTools {
// TODO: un-export this
class CPPTOOLS_EXPORT CppClassesFilter : public Internal::CppLocatorFilter class CPPTOOLS_EXPORT CppClassesFilter : public Internal::CppLocatorFilter
{ {
Q_OBJECT Q_OBJECT
public: public:
CppClassesFilter(Internal::CppLocatorData *locatorData); CppClassesFilter(CppLocatorData *locatorData);
~CppClassesFilter(); ~CppClassesFilter() QTC_OVERRIDE;
private: protected:
QList<QList<CppTools::IndexItem::Ptr> > itemsToMatchUserInputAgainst() const; IndexItem::ItemType matchTypes() const QTC_OVERRIDE { return IndexItem::Class; }
Core::LocatorFilterEntry filterEntryFromIndexItem(IndexItem::Ptr info); Core::LocatorFilterEntry filterEntryFromIndexItem(IndexItem::Ptr info) QTC_OVERRIDE;
}; };
} // namespace CppTools } // namespace CppTools
......
...@@ -78,10 +78,13 @@ QList<Core::LocatorFilterEntry> CppCurrentDocumentFilter::matchesFor( ...@@ -78,10 +78,13 @@ QList<Core::LocatorFilterEntry> CppCurrentDocumentFilter::matchesFor(
if (m_itemsOfCurrentDoc.isEmpty()) { if (m_itemsOfCurrentDoc.isEmpty()) {
Snapshot snapshot = m_modelManager->snapshot(); Snapshot snapshot = m_modelManager->snapshot();
Document::Ptr thisDocument = snapshot.document(m_currentFileName); Document::Ptr thisDocument = snapshot.document(m_currentFileName);
if (thisDocument) if (thisDocument) {
search(thisDocument)->visitAllChildren([&](const IndexItem::Ptr &info){ IndexItem::Ptr rootNode = search(thisDocument);
rootNode->visitAllChildren([&](const IndexItem::Ptr &info) -> IndexItem::VisitorResult {
m_itemsOfCurrentDoc.append(info); m_itemsOfCurrentDoc.append(info);
return IndexItem::Recurse;
}); });
}
} }
const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry); const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
......
...@@ -45,11 +45,6 @@ CppFunctionsFilter::~CppFunctionsFilter() ...@@ -45,11 +45,6 @@ CppFunctionsFilter::~CppFunctionsFilter()
{ {
} }
QList<QList<CppTools::IndexItem::Ptr> > CppFunctionsFilter::itemsToMatchUserInputAgainst() const
{
return QList<QList<CppTools::IndexItem::Ptr> >() << m_data->functions();
}
Core::LocatorFilterEntry CppFunctionsFilter::filterEntryFromIndexItem(IndexItem::Ptr info) Core::LocatorFilterEntry CppFunctionsFilter::filterEntryFromIndexItem(IndexItem::Ptr info)
{ {
const QVariant id = qVariantFromValue(info); const QVariant id = qVariantFromValue(info);
......
...@@ -33,6 +33,8 @@ ...@@ -33,6 +33,8 @@
#include "cpplocatordata.h" #include "cpplocatordata.h"
#include "cpplocatorfilter.h" #include "cpplocatorfilter.h"
#include <utils/qtcoverride.h>
namespace CppTools { namespace CppTools {
namespace Internal { namespace Internal {
...@@ -42,11 +44,11 @@ class CppFunctionsFilter : public CppLocatorFilter ...@@ -42,11 +44,11 @@ class CppFunctionsFilter : public CppLocatorFilter
public: public:
CppFunctionsFilter(CppLocatorData *locatorData); CppFunctionsFilter(CppLocatorData *locatorData);
~CppFunctionsFilter(); ~CppFunctionsFilter() QTC_OVERRIDE;
private: protected:
QList<QList<IndexItem::Ptr> > itemsToMatchUserInputAgainst() const; IndexItem::ItemType matchTypes() const QTC_OVERRIDE { return IndexItem::Function; }
Core::LocatorFilterEntry filterEntryFromIndexItem(IndexItem::Ptr info); Core::LocatorFilterEntry filterEntryFromIndexItem(IndexItem::Ptr info) QTC_OVERRIDE;
}; };
} // namespace Internal } // namespace Internal
......
...@@ -33,42 +33,17 @@ ...@@ -33,42 +33,17 @@
using namespace CppTools; using namespace CppTools;
using namespace CppTools::Internal; using namespace CppTools::Internal;
static const int MaxPendingDocuments = 10; enum { MaxPendingDocuments = 10 };
CppLocatorData::CppLocatorData(CppModelManager *modelManager) CppLocatorData::CppLocatorData()
: m_modelManager(modelManager) : m_strings(&CppToolsPlugin::stringTable())
, m_strings(CppToolsPlugin::stringTable()) , m_search(CppToolsPlugin::stringTable())
, m_search(m_strings)
, m_pendingDocumentsMutex(QMutex::Recursive) , m_pendingDocumentsMutex(QMutex::Recursive)
{ {
m_search.setSymbolsToSearchFor(SymbolSearcher::Enums m_search.setSymbolsToSearchFor(SymbolSearcher::Enums |
| SymbolSearcher::Classes SymbolSearcher::Classes |
| SymbolSearcher::Functions); SymbolSearcher::Functions);
m_pendingDocuments.reserve(MaxPendingDocuments); m_pendingDocuments.reserve(MaxPendingDocuments);
connect(m_modelManager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
this, SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
connect(m_modelManager, SIGNAL(aboutToRemoveFiles(QStringList)),
this, SLOT(onAboutToRemoveFiles(QStringList)));
}
QList<IndexItem::Ptr> CppLocatorData::enums()
{
flushPendingDocument(true);
return allIndexItems(m_allEnums);
}
QList<IndexItem::Ptr> CppLocatorData::classes()
{
flushPendingDocument(true);
return allIndexItems(m_allClasses);
}
QList<IndexItem::Ptr> CppLocatorData::functions()
{
flushPendingDocument(true);
return allIndexItems(m_allFunctions);
} }
void CppLocatorData::onDocumentUpdated(const CPlusPlus::Document::Ptr &document) void CppLocatorData::onDocumentUpdated(const CPlusPlus::Document::Ptr &document)
...@@ -93,57 +68,37 @@ void CppLocatorData::onDocumentUpdated(const CPlusPlus::Document::Ptr &document) ...@@ -93,57 +68,37 @@ void CppLocatorData::onDocumentUpdated(const CPlusPlus::Document::Ptr &document)
void CppLocatorData::onAboutToRemoveFiles(const QStringList &files) void CppLocatorData::onAboutToRemoveFiles(const QStringList &files)
{ {
QMutexLocker locker(&m_pendingDocumentsMutex); if (files.isEmpty())
return;
for (int i = 0; i < m_pendingDocuments.size(); ) { QMutexLocker locker(&m_pendingDocumentsMutex);
if (files.contains(m_pendingDocuments.at(i)->fileName()))
m_pendingDocuments.remove(i);
else
++i;
}
foreach (const QString &file, files) { foreach (const QString &file, files) {
m_allEnums.remove(file); m_infosByFile.remove(file);
m_allClasses.remove(file);
m_allFunctions.remove(file); for (int i = 0; i < m_pendingDocuments.size(); ++i) {
if (m_pendingDocuments.at(i)->fileName() == file) {
m_pendingDocuments.remove(i);
break;
}
}
} }
m_strings.scheduleGC(); m_strings->scheduleGC();
flushPendingDocument(false);
} }
void CppLocatorData::flushPendingDocument(bool force) void CppLocatorData::flushPendingDocument(bool force) const
{ {
// TODO: move this off the UI thread and into a future.
QMutexLocker locker(&m_pendingDocumentsMutex); QMutexLocker locker(&m_pendingDocumentsMutex);
if (!force && m_pendingDocuments.size() < MaxPendingDocuments) if (!force && m_pendingDocuments.size() < MaxPendingDocuments)
return; return;
if (m_pendingDocuments.isEmpty())
return;
foreach (CPlusPlus::Document::Ptr doc, m_pendingDocuments) { foreach (CPlusPlus::Document::Ptr doc, m_pendingDocuments)
const QString fileName = findOrInsertFilePath(doc->fileName()); m_infosByFile.insert(findOrInsertFilePath(doc->fileName()), m_search(doc));
QList<IndexItem::Ptr> resultsEnums;
QList<IndexItem::Ptr> resultsClasses;
QList<IndexItem::Ptr> resultsFunctions;
m_search(doc)->visitAllChildren([&](const IndexItem::Ptr &info) {
switch (info->type()) {
case IndexItem::Enum:
resultsEnums.append(info);
break;
case IndexItem::Class:
resultsClasses.append(info);
break;
case IndexItem::Function:
resultsFunctions.append(info);
break;
default:
break;
}
});
m_allEnums[fileName] = resultsEnums;
m_allClasses[fileName] = resultsClasses;
m_allFunctions[fileName] = resultsFunctions;
}
m_pendingDocuments.clear(); m_pendingDocuments.clear();
m_pendingDocuments.reserve(MaxPendingDocuments); m_pendingDocuments.reserve(MaxPendingDocuments);
......
...@@ -27,58 +27,66 @@ ...@@ -27,58 +27,66 @@
** **
****************************************************************************/ ****************************************************************************/
#ifndef CPPLOCATORDATA_H #ifndef CPPLOCATORDATA_H
#define CPPLOCATORDATA_H #define CPPLOCATORDATA_H
#include <functional>
#include <QHash> #include <QHash>
#include <QVector>
#include <cplusplus/CppDocument.h> #include <cplusplus/CppDocument.h>
#include "cpptools_global.h"
#include "cppmodelmanager.h" #include "cppmodelmanager.h"
#include "searchsymbols.h" #include "searchsymbols.h"
#include "stringtable.h" #include "stringtable.h"
namespace CppTools { namespace CppTools {
namespace Internal { namespace Internal {
class CppToolsPlugin;
} // Internal namespace
class CppLocatorData : public QObject class CppLocatorData : public QObject
{ {
Q_OBJECT Q_OBJECT
public:
explicit CppLocatorData(CppModelManager *modelManager);
QList<IndexItem::Ptr> enums(); // Only one instance, created by the CppToolsPlugin.
QList<IndexItem::Ptr> classes(); CppLocatorData();
QList<IndexItem::Ptr> functions(); friend class Internal::CppToolsPlugin;
private slots: public:
void filterAllFiles(IndexItem::Visitor func) const
{
flushPendingDocument(true);
QMutexLocker locker(&m_pendingDocumentsMutex);
QHash<QString, IndexItem::Ptr> infosByFile = m_infosByFile;
locker.unlock();
for (auto i = infosByFile.constBegin(), ei = infosByFile.constEnd(); i != ei; ++i)
if (i.value()->visitAllChildren(func) == IndexItem::Break)
return;
}
public slots:
void onDocumentUpdated(const CPlusPlus::Document::Ptr &document); void onDocumentUpdated(const CPlusPlus::Document::Ptr &document);
void onAboutToRemoveFiles(const QStringList &files); void onAboutToRemoveFiles(const QStringList &files);
private: private:
void flushPendingDocument(bool force); void flushPendingDocument(bool force) const;
QList<IndexItem::Ptr> allIndexItems(const QHash<QString, QList<IndexItem::Ptr>> &items) const; QList<IndexItem::Ptr> allIndexItems(const QHash<QString, QList<IndexItem::Ptr>> &items) const;
QString findOrInsertFilePath(const QString &path) QString findOrInsertFilePath(const QString &path) const
{ return m_strings.insert(path); } { return m_strings->insert(path); }
private: private:
CppModelManager *m_modelManager; Internal::StringTable *m_strings; // Used to avoid QString duplication
StringTable &m_strings; // Used to avoid QString duplication
SearchSymbols m_search; mutable SearchSymbols m_search;
QHash<QString, QList<IndexItem::Ptr> > m_allEnums; mutable QHash<QString, IndexItem::Ptr> m_infosByFile;
QHash<QString, QList<IndexItem::Ptr> > m_allClasses;
QHash<QString, QList<IndexItem::Ptr> > m_allFunctions;
mutable QMutex m_pendingDocumentsMutex; mutable QMutex m_pendingDocumentsMutex;
QVector<CPlusPlus::Document::Ptr> m_pendingDocuments; mutable QVector<CPlusPlus::Document::Ptr> m_pendingDocuments;
}; };
} // namespace Internal } // CppTools namespace
} // namespace CppTools
#endif // CPPLOCATORDATA_H #endif // CPPLOCATORDATA_H
...@@ -65,14 +65,6 @@ void CppLocatorFilter::refresh(QFutureInterface<void> &future) ...@@ -65,14 +65,6 @@ void CppLocatorFilter::refresh(QFutureInterface<void> &future)
Q_UNUSED(future) Q_UNUSED(future)
} }
QList<QList<CppTools::IndexItem::Ptr> > CppLocatorFilter::itemsToMatchUserInputAgainst() const
{
return QList<QList<CppTools::IndexItem::Ptr> >()
<< m_data->classes()
<< m_data->functions()
<< m_data->enums();
}
static bool compareLexigraphically(const Core::LocatorFilterEntry &a, static bool compareLexigraphically(const Core::LocatorFilterEntry &a,
const Core::LocatorFilterEntry &b) const Core::LocatorFilterEntry &b)
{ {
...@@ -93,16 +85,15 @@ QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor( ...@@ -93,16 +85,15 @@ QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
bool hasWildcard = (entry.contains(asterisk) || entry.contains(QLatin1Char('?'))); bool hasWildcard = (entry.contains(asterisk) || entry.contains(QLatin1Char('?')));
bool hasColonColon = entry.contains(QLatin1String("::")); bool hasColonColon = entry.contains(QLatin1String("::"));
const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry); const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
const IndexItem::ItemType wanted = matchTypes();
const QList<QList<CppTools::IndexItem::Ptr> > itemLists = itemsToMatchUserInputAgainst(); m_data->filterAllFiles([&](const IndexItem::Ptr &info) -> IndexItem::VisitorResult {
foreach (const QList<CppTools::IndexItem::Ptr> &items, itemLists) { if (future.isCanceled())
foreach (IndexItem::Ptr info, items) { return IndexItem::Break;
if (future.isCanceled()) if (info->type() & wanted) {
break; const QString matchString = hasColonColon ? info->scopedSymbolName() : info->symbolName();
const QString matchString = hasColonColon ? info->scopedSymbolName() if ((hasWildcard && regexp.exactMatch(matchString)) ||
: info->symbolName(); (!hasWildcard && matcher.indexIn(matchString) != -1)) {
if ((hasWildcard && regexp.exactMatch(matchString))
|| (!hasWildcard && matcher.indexIn(matchString) != -1)) {
const Core::LocatorFilterEntry filterEntry = filterEntryFromIndexItem(info); const Core::LocatorFilterEntry filterEntry = filterEntryFromIndexItem(info);
if (matchString.startsWith(entry, caseSensitivityForPrefix)) if (matchString.startsWith(entry, caseSensitivityForPrefix))
betterEntries.append(filterEntry); betterEntries.append(filterEntry);
...@@ -110,7 +101,12 @@ QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor( ...@@ -110,7 +101,12 @@ QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
goodEntries.append(filterEntry); goodEntries.append(filterEntry);
} }
} }
}
if (info->type() & IndexItem::Enum)
return IndexItem::Continue;
else
return IndexItem::Recurse;
});
if (goodEntries.size() < 1000) if (goodEntries.size() < 1000)
qStableSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically); qStableSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
......
...@@ -54,7 +54,7 @@ public: ...@@ -54,7 +54,7 @@ public:
void refresh(QFutureInterface<void> &future); void refresh(QFutureInterface<void> &future);
protected: protected:
virtual QList<QList<IndexItem::Ptr> > itemsToMatchUserInputAgainst() const; virtual IndexItem::ItemType matchTypes() const { return IndexItem::All; }
virtual Core::LocatorFilterEntry filterEntryFromIndexItem(IndexItem::Ptr info); virtual Core::LocatorFilterEntry filterEntryFromIndexItem(IndexItem::Ptr info);
protected: protected:
......
...@@ -131,7 +131,13 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error) ...@@ -131,7 +131,13 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
connect(DocumentManager::instance(), SIGNAL(filesChangedInternally(QStringList)), connect(DocumentManager::instance(), SIGNAL(filesChangedInternally(QStringList)),
modelManager, SLOT(updateSourceFiles(QStringList))); modelManager, SLOT(updateSourceFiles(QStringList)));
CppLocatorData *locatorData = new CppLocatorData(modelManager); CppLocatorData *locatorData = new CppLocatorData;
connect(modelManager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
locatorData, SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
connect(modelManager, SIGNAL(aboutToRemoveFiles(QStringList)),
locatorData, SLOT(onAboutToRemoveFiles(QStringList)));
addAutoReleasedObject(locatorData); addAutoReleasedObject(locatorData);
addAutoReleasedObject(new CppLocatorFilter(locatorData)); addAutoReleasedObject(new CppLocatorFilter(locatorData));
addAutoReleasedObject(new CppClassesFilter(locatorData)); addAutoReleasedObject(new CppClassesFilter(locatorData));
......
...@@ -44,12 +44,3 @@ void IndexItem::squeeze() ...@@ -44,12 +44,3 @@ void IndexItem::squeeze()
for (int i = 0, ei = m_children.size(); i != ei; ++i) for (int i = 0, ei = m_children.size(); i != ei; ++i)
m_children[i]->squeeze(); m_children[i]->squeeze();
} }
void IndexItem::visitAllChildren(std::function<void (const IndexItem::Ptr &)> f) const
{
foreach (const IndexItem::Ptr &child, m_children) {
f(child);
if (!child->m_children.isEmpty())
child->visitAllChildren(f);
}
}
...@@ -38,6 +38,8 @@ ...@@ -38,6 +38,8 @@
#include <QSharedPointer> #include <QSharedPointer>
#include <QMetaType> #include <QMetaType>
#include <functional>
namespace CppTools { namespace CppTools {
class CPPTOOLS_EXPORT IndexItem class CPPTOOLS_EXPORT IndexItem
...@@ -45,7 +47,14 @@ class CPPTOOLS_EXPORT IndexItem ...@@ -45,7 +47,14 @@ class CPPTOOLS_EXPORT IndexItem
Q_DISABLE_COPY(IndexItem) Q_DISABLE_COPY(IndexItem)
public: public:
enum ItemType { Enum, Class, Function, Declaration }; enum ItemType {
Enum = 1 << 0,
Class = 1 << 1,
Function = 1 << 2,
Declaration = 1 << 3,
All = Enum | Class | Function | Declaration
};
private: private:
IndexItem(const QString &symbolName, IndexItem(const QString &symbolName,
...@@ -139,7 +148,34 @@ public: ...@@ -139,7 +148,34 @@ public:
void addChild(IndexItem::Ptr childItem) { m_children.append(childItem); } void addChild(IndexItem::Ptr childItem) { m_children.append(childItem); }
void squeeze(); void squeeze();
void visitAllChildren(std::function<void (const IndexItem::Ptr &)> f) const; enum VisitorResult {
Break, /// terminates traversal
Continue, /// continues traversal with the next sibling
Recurse, /// continues traversal with the children
};
typedef std::function<VisitorResult (const IndexItem::Ptr &)