qmldesigner: avoid reloading document when not needed

Creating a new ReadingContext is an expensive operation as it creates
and call a Link object.  This operation is needed only when the document
is actually changed, in all the other cases it is not needed.

Change-Id: Ia368122d60d2c7de86d10e169dd7f20a19f01115
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Marco Benelli
2018-03-15 09:57:55 +01:00
committed by Thomas Hartmann
parent 9624bf0508
commit 65c11b6446
2 changed files with 35 additions and 19 deletions

View File

@@ -739,16 +739,20 @@ const CppComponentValue *NodeMetaInfoPrivate::getCppComponentValue() const
} }
// get the qml object value that's available in the document // get the qml object value that's available in the document
foreach (const QmlJS::Import &import, context()->imports(document())->all()) { const QmlJS::Imports *importsPtr = context()->imports(document());
if (import.info.path() != QString::fromUtf8(module)) if (importsPtr) {
continue; const QList<QmlJS::Import> imports = importsPtr->all();
const Value *lookupResult = import.object->lookupMember(QString::fromUtf8(type), context()); foreach (const QmlJS::Import &import, imports) {
const CppComponentValue *cppValue = value_cast<CppComponentValue>(lookupResult); if (import.info.path() != QString::fromUtf8(module))
if (cppValue continue;
&& (m_majorVersion == -1 || m_majorVersion == cppValue->componentVersion().majorVersion()) const Value *lookupResult = import.object->lookupMember(QString::fromUtf8(type), context());
&& (m_minorVersion == -1 || m_minorVersion == cppValue->componentVersion().minorVersion()) const CppComponentValue *cppValue = value_cast<CppComponentValue>(lookupResult);
) if (cppValue
return cppValue; && (m_majorVersion == -1 || m_majorVersion == cppValue->componentVersion().majorVersion())
&& (m_minorVersion == -1 || m_minorVersion == cppValue->componentVersion().minorVersion())
)
return cppValue;
}
} }
const CppComponentValue *value = value_cast<CppComponentValue>(getObjectValue()); const CppComponentValue *value = value_cast<CppComponentValue>(getObjectValue());

View File

@@ -59,6 +59,8 @@
#include <QLoggingCategory> #include <QLoggingCategory>
#include <QRegularExpression> #include <QRegularExpression>
#include <memory>
using namespace LanguageUtils; using namespace LanguageUtils;
using namespace QmlJS; using namespace QmlJS;
@@ -357,11 +359,10 @@ class ReadingContext
public: public:
ReadingContext(const Snapshot &snapshot, const Document::Ptr &doc, ReadingContext(const Snapshot &snapshot, const Document::Ptr &doc,
const ViewerContext &vContext) const ViewerContext &vContext)
: m_snapshot(snapshot) : m_doc(doc)
, m_doc(doc) , m_context(
, m_link(snapshot, vContext, Link(snapshot, vContext, ModelManagerInterface::instance()->builtins(doc))
ModelManagerInterface::instance()->builtins(doc)) (doc, &m_diagnosticLinkMessages))
, m_context(m_link(doc, &m_diagnosticLinkMessages))
, m_scopeChain(doc, m_context) , m_scopeChain(doc, m_context)
, m_scopeBuilder(&m_scopeChain) , m_scopeBuilder(&m_scopeChain)
{ {
@@ -675,9 +676,7 @@ public:
{ return m_diagnosticLinkMessages; } { return m_diagnosticLinkMessages; }
private: private:
Snapshot m_snapshot;
Document::Ptr m_doc; Document::Ptr m_doc;
Link m_link;
QList<DiagnosticMessage> m_diagnosticLinkMessages; QList<DiagnosticMessage> m_diagnosticLinkMessages;
ContextPtr m_context; ContextPtr m_context;
ScopeChain m_scopeChain; ScopeChain m_scopeChain;
@@ -889,7 +888,9 @@ void TextToModelMerger::setupPossibleImports(const QmlJS::Snapshot &snapshot, co
QHash<QString, ImportKey> filteredPossibleImportKeys = QHash<QString, ImportKey> filteredPossibleImportKeys =
filterPossibleImportKeys(snapshot.importDependencies()->libraryImports(viewContext), m_rewriterView->model()); filterPossibleImportKeys(snapshot.importDependencies()->libraryImports(viewContext), m_rewriterView->model());
removeUsedImports(filteredPossibleImportKeys, m_scopeChain->context()->imports(m_document.data())->all()); const QmlJS::Imports *imports = m_scopeChain->context()->imports(m_document.data());
if (imports)
removeUsedImports(filteredPossibleImportKeys, imports->all());
QList<QmlDesigner::Import> possibleImports = generatePossibleLibraryImports(filteredPossibleImportKeys); QList<QmlDesigner::Import> possibleImports = generatePossibleLibraryImports(filteredPossibleImportKeys);
@@ -901,7 +902,11 @@ void TextToModelMerger::setupPossibleImports(const QmlJS::Snapshot &snapshot, co
void TextToModelMerger::setupUsedImports() void TextToModelMerger::setupUsedImports()
{ {
const QList<QmlJS::Import> allImports = m_scopeChain->context()->imports(m_document.data())->all(); const QmlJS::Imports *imports = m_scopeChain->context()->imports(m_document.data());
if (!imports)
return;
const QList<QmlJS::Import> allImports = imports->all();
QList<Import> usedImports; QList<Import> usedImports;
@@ -942,6 +947,7 @@ Document::MutablePtr TextToModelMerger::createParsedDocument(const QUrl &url, co
return doc; return doc;
} }
bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceHandler) bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceHandler)
{ {
qCInfo(rewriterBenchmark) << Q_FUNC_INFO; qCInfo(rewriterBenchmark) << Q_FUNC_INFO;
@@ -969,7 +975,10 @@ bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceH
QList<DocumentMessage> errors; QList<DocumentMessage> errors;
QList<DocumentMessage> warnings; QList<DocumentMessage> warnings;
if (Document::MutablePtr doc = createParsedDocument(url, data, &errors)) { if (Document::MutablePtr doc = createParsedDocument(url, data, &errors)) {
if (m_document && (m_document->fingerprint() == doc->fingerprint()))
return true;
snapshot.insert(doc); snapshot.insert(doc);
m_document = doc; m_document = doc;
qCInfo(rewriterBenchmark) << "parsed correctly: " << time.elapsed(); qCInfo(rewriterBenchmark) << "parsed correctly: " << time.elapsed();
@@ -979,6 +988,8 @@ bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceH
setActive(false); setActive(false);
return false; return false;
} }
m_vContext = ModelManagerInterface::instance()->defaultVContext(Dialect::Qml, m_document); m_vContext = ModelManagerInterface::instance()->defaultVContext(Dialect::Qml, m_document);
ReadingContext ctxt(snapshot, m_document, m_vContext); ReadingContext ctxt(snapshot, m_document, m_vContext);
m_scopeChain = QSharedPointer<const ScopeChain>( m_scopeChain = QSharedPointer<const ScopeChain>(
@@ -1017,6 +1028,7 @@ bool TextToModelMerger::load(const QString &data, DifferenceHandler &differenceH
qCInfo(rewriterBenchmark) << "synced nodes:" << time.elapsed(); qCInfo(rewriterBenchmark) << "synced nodes:" << time.elapsed();
setActive(false); setActive(false);
return true; return true;
} catch (Exception &e) { } catch (Exception &e) {
DocumentMessage error(&e); DocumentMessage error(&e);