From d259157bfe473ccb2cff4bdf37aee2e539ccee49 Mon Sep 17 00:00:00 2001 From: Arttu Tarkiainen Date: Mon, 24 Oct 2022 17:16:04 +0300 Subject: InstallerProxy: optimize querying components by name For some API compatibility with QtScript, the QInstaller::ScriptEngine's implementation of newQObject(QObject *object) method adds findChild() and findChildren() functions as properties for the JS object wrapping the QObject, QInstaller::Component in this case. This results in two extra QJSEngine::evaluate() calls, adding some overhead. The Component class does not utilize the QObject's object tree & ownership feature in the C++ side, so the functions do not add extra value in this context. InstallerProxy::ComponentByName() creates the JavaScript object of the component with the aforementioned ScriptEngine::newQObject() implementation. We inject a snippet resulting in a call to InstallerProxy's componentByName() function for each component script. Measuring with callgrind, the time spent in the extra evaluations was around fifth of the whole evaluation cycle. Modify the function to to make the addition of the extra properties optional, and omit them when querying components by name from script. Task-number: QTIFW-2790 Change-Id: I58040809dcf69599e0fabd98def2dc9464aadf84 Reviewed-by: Katja Marttila --- src/libs/installer/scriptengine.cpp | 13 ++++++++----- src/libs/installer/scriptengine.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'src/libs') diff --git a/src/libs/installer/scriptengine.cpp b/src/libs/installer/scriptengine.cpp index 009215909..3b2e3ae1d 100644 --- a/src/libs/installer/scriptengine.cpp +++ b/src/libs/installer/scriptengine.cpp @@ -95,7 +95,7 @@ QJSValue InstallerProxy::components(const QString ®exp) const QJSValue InstallerProxy::componentByName(const QString &componentName) { if (m_core) - return m_engine->newQObject(m_core->componentByName(componentName)); + return m_engine->newQObject(m_core->componentByName(componentName), false); return QJSValue(); } @@ -418,9 +418,9 @@ ScriptEngine::ScriptEngine(PackageManagerCore *core) : /*! Creates a JavaScript object that wraps the given QObject \a object. - Signals and slots, properties and children of \a object are - available as properties of the created QJSValue. In addition some helper methods and properties - are added: + Signals and slots, properties and children of \a object are available as properties + of the created QJSValue. If \a qtScriptCompat is set to \c true (default), some helper + methods and properties from the legacy \c QtScript module are added: \list \li findChild(), findChildren() recursively search for child objects with the given @@ -429,7 +429,7 @@ ScriptEngine::ScriptEngine(PackageManagerCore *core) : names. \endlist */ -QJSValue ScriptEngine::newQObject(QObject *object) +QJSValue ScriptEngine::newQObject(QObject *object, bool qtScriptCompat) { QJSValue jsValue = m_engine.newQObject(object); if (!jsValue.isQObject()) @@ -437,6 +437,9 @@ QJSValue ScriptEngine::newQObject(QObject *object) QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership); + if (!qtScriptCompat) // skip adding the extra properties + return jsValue; + // add findChild(), findChildren() methods known from QtScript QJSValue findChild = m_engine.evaluate( QLatin1String("(function() { return gui.findChild(this, arguments[0]); })")); diff --git a/src/libs/installer/scriptengine.h b/src/libs/installer/scriptengine.h index ae3cdd04f..a7c0ef253 100644 --- a/src/libs/installer/scriptengine.h +++ b/src/libs/installer/scriptengine.h @@ -48,7 +48,7 @@ public: explicit ScriptEngine(PackageManagerCore *core = 0); QJSValue globalObject() const { return m_engine.globalObject(); } - QJSValue newQObject(QObject *object); + QJSValue newQObject(QObject *object, bool qtScriptCompat = true); QJSValue newArray(uint length = 0); QJSValue evaluate(const QString &program, const QString &fileName = QString(), int lineNumber = 1); -- cgit v1.2.3