Commit a870c29a authored by Erik Verbruggen's avatar Erik Verbruggen

C++: use a global string table for SearchSymbols.

This string table uniques strings, so that multiple identical strings
share their contents. It is used by the locator and the symbol searcher,
and will later be used by the class view.

Change-Id: Ib8b50f69bbf994d0d7a39b66dc8caf1a3d9bfb42
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
parent a940c73d
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "cpppreprocessor.h" #include "cpppreprocessor.h"
#include "searchsymbols.h" #include "searchsymbols.h"
#include "cpptoolsconstants.h" #include "cpptoolsconstants.h"
#include "cpptoolsplugin.h"
#include "cppprojectfile.h" #include "cppprojectfile.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
...@@ -107,7 +108,7 @@ public: ...@@ -107,7 +108,7 @@ public:
future.setProgressValue(0); future.setProgressValue(0);
int progress = 0; int progress = 0;
SearchSymbols search; SearchSymbols search(CppToolsPlugin::stringTable());
search.setSymbolsToSearchFor(m_parameters.types); search.setSymbolsToSearchFor(m_parameters.types);
CPlusPlus::Snapshot::const_iterator it = m_snapshot.begin(); CPlusPlus::Snapshot::const_iterator it = m_snapshot.begin();
......
...@@ -36,8 +36,10 @@ ...@@ -36,8 +36,10 @@
using namespace CppTools::Internal; using namespace CppTools::Internal;
using namespace CPlusPlus; using namespace CPlusPlus;
CppCurrentDocumentFilter::CppCurrentDocumentFilter(CppModelManager *manager) CppCurrentDocumentFilter::CppCurrentDocumentFilter(CppModelManager *manager,
StringTable &stringTable)
: m_modelManager(manager) : m_modelManager(manager)
, search(stringTable)
{ {
setId("Methods in current Document"); setId("Methods in current Document");
setDisplayName(tr("C++ Symbols in Current Document")); setDisplayName(tr("C++ Symbols in Current Document"));
...@@ -57,7 +59,8 @@ CppCurrentDocumentFilter::CppCurrentDocumentFilter(CppModelManager *manager) ...@@ -57,7 +59,8 @@ CppCurrentDocumentFilter::CppCurrentDocumentFilter(CppModelManager *manager)
this, SLOT(onEditorAboutToClose(Core::IEditor*))); this, SLOT(onEditorAboutToClose(Core::IEditor*)));
} }
QList<Core::LocatorFilterEntry> CppCurrentDocumentFilter::matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, const QString & origEntry) QList<Core::LocatorFilterEntry> CppCurrentDocumentFilter::matchesFor(
QFutureInterface<Core::LocatorFilterEntry> &future, const QString & origEntry)
{ {
QString entry = trimWildcards(origEntry); QString entry = trimWildcards(origEntry);
QList<Core::LocatorFilterEntry> goodEntries; QList<Core::LocatorFilterEntry> goodEntries;
......
...@@ -45,7 +45,8 @@ class CppCurrentDocumentFilter : public Core::ILocatorFilter ...@@ -45,7 +45,8 @@ class CppCurrentDocumentFilter : public Core::ILocatorFilter
Q_OBJECT Q_OBJECT
public: public:
explicit CppCurrentDocumentFilter(CppModelManager *manager); explicit CppCurrentDocumentFilter(CppModelManager *manager,
StringTable &stringTable);
~CppCurrentDocumentFilter() {} ~CppCurrentDocumentFilter() {}
QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, const QString &entry); QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, const QString &entry);
......
...@@ -27,8 +27,8 @@ ...@@ -27,8 +27,8 @@
** **
****************************************************************************/ ****************************************************************************/
#include "cpplocatordata.h" #include "cpplocatordata.h"
#include "cpptoolsplugin.h"
using namespace CppTools; using namespace CppTools;
using namespace CppTools::Internal; using namespace CppTools::Internal;
...@@ -37,6 +37,8 @@ static const int MaxPendingDocuments = 10; ...@@ -37,6 +37,8 @@ static const int MaxPendingDocuments = 10;
CppLocatorData::CppLocatorData(CppModelManager *modelManager) CppLocatorData::CppLocatorData(CppModelManager *modelManager)
: m_modelManager(modelManager) : m_modelManager(modelManager)
, m_strings(CppToolsPlugin::stringTable())
, m_search(m_strings)
, m_pendingDocumentsMutex(QMutex::Recursive) , m_pendingDocumentsMutex(QMutex::Recursive)
{ {
m_search.setSymbolsToSearchFor(SymbolSearcher::Enums m_search.setSymbolsToSearchFor(SymbolSearcher::Enums
...@@ -104,8 +106,9 @@ void CppLocatorData::onAboutToRemoveFiles(const QStringList &files) ...@@ -104,8 +106,9 @@ void CppLocatorData::onAboutToRemoveFiles(const QStringList &files)
m_allEnums.remove(file); m_allEnums.remove(file);
m_allClasses.remove(file); m_allClasses.remove(file);
m_allFunctions.remove(file); m_allFunctions.remove(file);
removeFilePath(file);
} }
m_strings.scheduleGC();
} }
void CppLocatorData::flushPendingDocument(bool force) void CppLocatorData::flushPendingDocument(bool force)
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include "cppmodelmanager.h" #include "cppmodelmanager.h"
#include "searchsymbols.h" #include "searchsymbols.h"
#include "stringtable.h"
namespace CppTools { namespace CppTools {
namespace Internal { namespace Internal {
...@@ -62,15 +63,12 @@ private: ...@@ -62,15 +63,12 @@ private:
QList<ModelItemInfo> > &items) const; QList<ModelItemInfo> > &items) const;
QString findOrInsertFilePath(const QString &path) QString findOrInsertFilePath(const QString &path)
{ return *m_filePaths.insert(path); } { return m_strings.insert(path); }
void removeFilePath(const QString &path)
{ m_filePaths.remove(path); }
private: private:
CppModelManager *m_modelManager; CppModelManager *m_modelManager;
QSet<QString> m_filePaths; // Used to avoid QString duplication StringTable &m_strings; // Used to avoid QString duplication
SearchSymbols m_search; SearchSymbols m_search;
QHash<QString, QList<ModelItemInfo> > m_allEnums; QHash<QString, QList<ModelItemInfo> > m_allEnums;
......
...@@ -52,6 +52,7 @@ HEADERS += \ ...@@ -52,6 +52,7 @@ HEADERS += \
includeutils.h \ includeutils.h \
insertionpointlocator.h \ insertionpointlocator.h \
searchsymbols.h \ searchsymbols.h \
stringtable.h \
symbolfinder.h \ symbolfinder.h \
symbolsfindfilter.h \ symbolsfindfilter.h \
typehierarchybuilder.h typehierarchybuilder.h
...@@ -104,6 +105,7 @@ SOURCES += \ ...@@ -104,6 +105,7 @@ SOURCES += \
includeutils.cpp \ includeutils.cpp \
insertionpointlocator.cpp \ insertionpointlocator.cpp \
searchsymbols.cpp \ searchsymbols.cpp \
stringtable.cpp \
symbolfinder.cpp \ symbolfinder.cpp \
symbolsfindfilter.cpp \ symbolsfindfilter.cpp \
typehierarchybuilder.cpp typehierarchybuilder.cpp
......
...@@ -72,6 +72,7 @@ QtcPlugin { ...@@ -72,6 +72,7 @@ QtcPlugin {
"includeutils.cpp", "includeutils.h", "includeutils.cpp", "includeutils.h",
"insertionpointlocator.cpp", "insertionpointlocator.h", "insertionpointlocator.cpp", "insertionpointlocator.h",
"searchsymbols.cpp", "searchsymbols.h", "searchsymbols.cpp", "searchsymbols.h",
"stringtable.cpp", "stringtable.h",
"symbolfinder.cpp", "symbolfinder.h", "symbolfinder.cpp", "symbolfinder.h",
"symbolsfindfilter.cpp", "symbolsfindfilter.h", "symbolsfindfilter.cpp", "symbolsfindfilter.h",
"typehierarchybuilder.cpp", "typehierarchybuilder.h", "typehierarchybuilder.cpp", "typehierarchybuilder.h",
......
...@@ -136,7 +136,7 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error) ...@@ -136,7 +136,7 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
addAutoReleasedObject(new CppLocatorFilter(locatorData)); addAutoReleasedObject(new CppLocatorFilter(locatorData));
addAutoReleasedObject(new CppClassesFilter(locatorData)); addAutoReleasedObject(new CppClassesFilter(locatorData));
addAutoReleasedObject(new CppFunctionsFilter(locatorData)); addAutoReleasedObject(new CppFunctionsFilter(locatorData));
addAutoReleasedObject(new CppCurrentDocumentFilter(modelManager)); addAutoReleasedObject(new CppCurrentDocumentFilter(modelManager, m_stringTable));
addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings)); addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
addAutoReleasedObject(new CppCodeModelSettingsPage(m_codeModelSettings)); addAutoReleasedObject(new CppCodeModelSettingsPage(m_codeModelSettings));
addAutoReleasedObject(new SymbolsFindFilter(modelManager)); addAutoReleasedObject(new SymbolsFindFilter(modelManager));
...@@ -190,6 +190,11 @@ QSharedPointer<CppCodeModelSettings> CppToolsPlugin::codeModelSettings() const ...@@ -190,6 +190,11 @@ QSharedPointer<CppCodeModelSettings> CppToolsPlugin::codeModelSettings() const
return m_codeModelSettings; return m_codeModelSettings;
} }
StringTable &CppToolsPlugin::stringTable()
{
return instance()->m_stringTable;
}
void CppToolsPlugin::switchHeaderSource() void CppToolsPlugin::switchHeaderSource()
{ {
QString otherFile = correspondingHeaderOrSource( QString otherFile = correspondingHeaderOrSource(
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#define CPPTOOLS_H #define CPPTOOLS_H
#include "cpptools_global.h" #include "cpptools_global.h"
#include "stringtable.h"
#include <projectexplorer/projectexplorer.h> #include <projectexplorer/projectexplorer.h>
...@@ -73,6 +74,8 @@ public: ...@@ -73,6 +74,8 @@ public:
QSharedPointer<CppCodeModelSettings> codeModelSettings() const; QSharedPointer<CppCodeModelSettings> codeModelSettings() const;
static StringTable &stringTable();
public slots: public slots:
void switchHeaderSource(); void switchHeaderSource();
void switchHeaderSourceInNextSplit(); void switchHeaderSourceInNextSplit();
...@@ -163,6 +166,7 @@ private: ...@@ -163,6 +166,7 @@ private:
QSharedPointer<CppFileSettings> m_fileSettings; QSharedPointer<CppFileSettings> m_fileSettings;
QSharedPointer<CppCodeModelSettings> m_codeModelSettings; QSharedPointer<CppCodeModelSettings> m_codeModelSettings;
CppToolsSettings *m_settings; CppToolsSettings *m_settings;
StringTable m_stringTable;
}; };
} // namespace Internal } // namespace Internal
......
...@@ -42,8 +42,9 @@ SearchSymbols::SymbolTypes SearchSymbols::AllTypes = ...@@ -42,8 +42,9 @@ SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
| SymbolSearcher::Enums | SymbolSearcher::Enums
| SymbolSearcher::Declarations; | SymbolSearcher::Declarations;
SearchSymbols::SearchSymbols() : SearchSymbols::SearchSymbols(Internal::StringTable &stringTable)
symbolsToSearchFor(SymbolSearcher::Classes | SymbolSearcher::Functions | SymbolSearcher::Enums) : strings(stringTable)
, symbolsToSearchFor(SymbolSearcher::Classes | SymbolSearcher::Functions | SymbolSearcher::Enums)
{ {
} }
...@@ -62,7 +63,7 @@ QList<ModelItemInfo> SearchSymbols::operator()(Document::Ptr doc, int sizeHint, ...@@ -62,7 +63,7 @@ QList<ModelItemInfo> SearchSymbols::operator()(Document::Ptr doc, int sizeHint,
} }
(void) switchScope(previousScope); (void) switchScope(previousScope);
QList<ModelItemInfo> result = items; QList<ModelItemInfo> result = items;
strings.clear(); strings.scheduleGC();
items.clear(); items.clear();
m_paths.clear(); m_paths.clear();
return result; return result;
...@@ -291,7 +292,7 @@ void SearchSymbols::appendItem(const QString &symbolName, const QString &symbolT ...@@ -291,7 +292,7 @@ void SearchSymbols::appendItem(const QString &symbolName, const QString &symbolT
findOrInsert(symbolType), findOrInsert(symbolType),
findOrInsert(symbolScope), findOrInsert(symbolScope),
itemType, itemType,
path, findOrInsert(path),
symbol->line(), symbol->line(),
symbol->column() - 1, // 1-based vs 0-based column symbol->column() - 1, // 1-based vs 0-based column
icon)); icon));
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "cpptools_global.h" #include "cpptools_global.h"
#include "cppindexingsupport.h" #include "cppindexingsupport.h"
#include "stringtable.h"
#include <cplusplus/CppDocument.h> #include <cplusplus/CppDocument.h>
#include <cplusplus/Icons.h> #include <cplusplus/Icons.h>
...@@ -140,7 +141,7 @@ public: ...@@ -140,7 +141,7 @@ public:
static SymbolTypes AllTypes; static SymbolTypes AllTypes;
SearchSymbols(); SearchSymbols(Internal::StringTable &stringTable);
void setSymbolsToSearchFor(const SymbolTypes &types); void setSymbolsToSearchFor(const SymbolTypes &types);
...@@ -193,9 +194,9 @@ protected: ...@@ -193,9 +194,9 @@ protected:
private: private:
QString findOrInsert(const QString &s) QString findOrInsert(const QString &s)
{ return *strings.insert(s); } { return strings.insert(s); }
QSet<QString> strings; // Used to avoid QString duplication Internal::StringTable &strings; // Used to avoid QString duplication
QString _scope; QString _scope;
CPlusPlus::Overview overview; CPlusPlus::Overview overview;
......
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "stringtable.h"
#include <QDebug>
#include <QThreadPool>
#include <QTime>
using namespace CppTools::Internal;
enum {
GCTimeOut = 10 * 1000 // 10 seconds
};
StringTable::StringTable()
: m_gcRunner(*this)
, m_stopGCRequested(false)
{
m_strings.reserve(1000);
m_gcRunner.setAutoDelete(false);
m_gcCountDown.setSingleShot(true);
m_gcCountDown.setInterval(GCTimeOut);
connect(&m_gcCountDown, SIGNAL(timeout()),
this, SLOT(startGC()));
}
QString StringTable::insert(const QString &string)
{
if (string.isEmpty())
return string;
m_stopGCRequested.fetchAndStoreAcquire(true);
QMutexLocker locker(&m_lock);
QString result = *m_strings.insert(string);
m_stopGCRequested.storeRelease(false);
return result;
}
void StringTable::scheduleGC()
{
QMutexLocker locker(&m_lock);
m_gcCountDown.start();
}
void StringTable::startGC()
{
QThreadPool::globalInstance()->start(&m_gcRunner);
}
enum {
DebugStringTable = 0
};
void StringTable::GC()
{
QMutexLocker locker(&m_lock);
int initialSize = 0;
int startTime = 0;
if (DebugStringTable) {
initialSize = m_strings.size();
startTime = QTime::currentTime().msecsSinceStartOfDay();
}
// Collect all QStrings which have refcount 1. (One reference in m_strings and nowhere else.)
for (QSet<QString>::iterator i = m_strings.begin(); i != m_strings.end();) {
if (m_stopGCRequested.testAndSetRelease(true, false))
return;
const int refCount = const_cast<QString&>(*i).data_ptr()->ref.atomic.load();
if (refCount == 1)
i = m_strings.erase(i);
else
++i;
}
if (DebugStringTable) {
const int endTime = QTime::currentTime().msecsSinceStartOfDay();
const int currentSize = m_strings.size();
qDebug() << "StringTable::GC removed" << initialSize - currentSize
<< "strings in" << endTime - startTime
<< "ms, size is now" << currentSize;
}
}
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef STRINGTABLE_H
#define STRINGTABLE_H
#include <QAtomicInt>
#include <QMutex>
#include <QObject>
#include <QRunnable>
#include <QSet>
#include <QTimer>
namespace CppTools {
namespace Internal {
class StringTable: public QObject
{
Q_OBJECT
public:
StringTable();
QString insert(const QString &string);
void scheduleGC();
private slots:
void startGC();
private:
void GC();
class GCRunner: public QRunnable {
StringTable &m_stringTable;
public:
GCRunner(StringTable &stringTable): m_stringTable(stringTable) {}
virtual void run() { m_stringTable.GC(); }
} m_gcRunner;
friend class GCRunner;
private:
mutable QMutex m_lock;
QAtomicInt m_stopGCRequested;
QSet<QString> m_strings;
QTimer m_gcCountDown;
};
} // Internal namespace
} // CppTools namespace
#endif // STRINGTABLE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment