summaryrefslogtreecommitdiffstats
path: root/src/script/api
diff options
context:
space:
mode:
authorKent Hansen <kent.hansen@nokia.com>2010-06-23 15:22:47 (GMT)
committerKent Hansen <kent.hansen@nokia.com>2010-06-25 06:59:13 (GMT)
commita91f8d704c3ff3826f0ea8b7e73fc6d91dd5b836 (patch)
tree26c0bb1921eb8b3fc73b01be94dc1823d7322e19 /src/script/api
parentf793344d17b0ec5df4987e123cc1e3b008a949bb (diff)
downloadQt-a91f8d704c3ff3826f0ea8b7e73fc6d91dd5b836.zip
Qt-a91f8d704c3ff3826f0ea8b7e73fc6d91dd5b836.tar.gz
Qt-a91f8d704c3ff3826f0ea8b7e73fc6d91dd5b836.tar.bz2
Use custom static scopes to improve QML/JavaScript performance
This commit introduces a new internal JS object type, QScriptStaticScopeObject, that enables the JS compiler to make more aggressive optimizations of scoped property access. QScriptStaticScopeObject registers all its properties in a symbol table that the JS compiler has access to. If the compiler finds the property in the symbol table, it will generate the fast index-based op_{get,put}_scoped_var bytecodes, rather than the dynamic (slow) op_resolve and friends. If the compiler _doesn't_ find the property in the symbol table, it infers that it's safe to skip the scope object when later resolving the property, which will also improve performance (see op_resolve_skip bytecode). QScriptStaticScopeObject is only safe to use when all relevant properties are known at JS compile time; that is, when a function that has the static scope object in its scope chain is compiled. It's up to the user of the class (e.g. QtDeclarative) to ensure that this constraint is not violated. The API for constructing QScriptStaticScopeObject instances is not public; it lives in QScriptDeclarativeClass for now, an internal class exported for the purpose of QML. The instance is returned as a QScriptValue and can be manipulated like any other JS object (e.g. by QScriptValue::setProperty()). The other part of this commit utilizes QScriptStaticScopeObject in QtDeclarative in the two major places where it's currently possible: 1) QML disallows adding properties to the Global Object. Furthermore, it's not possible for QML IDs and properties to "shadow" global variables. Hence, a QScriptStaticScopeObject can be used to hold all the standard ECMA properties, and this scope object can come _before_ the QML component in the scope chain. This enables binding expressions and scripts to have optimized (direct) access to e.g. Math.sin. 2) Imported scripts can have their properties (resulting from variable declarations ("var" statements) and function declarations) added to a static scope object. This enables functions in the script to have optimized (direct) access to the script's own properties, as well as to global properties such as Math. With this change, it's no longer possible to delete properties of the Global Object, nor delete properties of an imported script. It's a compromise we make in order to make the optimization safe. Task-number: QTBUG-8576 Reviewed-by: Aaron Kennedy Reviewed-by: Olivier Goffart Reviewed-by: Jedrzej Nowacki
Diffstat (limited to 'src/script/api')
-rw-r--r--src/script/api/qscriptengine.cpp12
-rw-r--r--src/script/api/qscriptengine_p.h15
2 files changed, 18 insertions, 9 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp
index e2999c1..655026c 100644
--- a/src/script/api/qscriptengine.cpp
+++ b/src/script/api/qscriptengine.cpp
@@ -64,6 +64,7 @@
#include "bridge/qscriptqobject_p.h"
#include "bridge/qscriptglobalobject_p.h"
#include "bridge/qscriptactivationobject_p.h"
+#include "bridge/qscriptstaticscopeobject_p.h"
#ifndef QT_NO_QOBJECT
#include <QtCore/qcoreapplication.h>
@@ -905,6 +906,7 @@ QScriptEnginePrivate::QScriptEnginePrivate()
JSC::ExecState* exec = globalObject->globalExec();
scriptObjectStructure = QScriptObject::createStructure(globalObject->objectPrototype());
+ staticScopeObjectStructure = QScriptStaticScopeObject::createStructure(JSC::jsNull());
qobjectPrototype = new (exec) QScript::QObjectPrototype(exec, QScript::QObjectPrototype::createStructure(globalObject->objectPrototype()), globalObject->prototypeFunctionStructure());
qobjectWrapperObjectStructure = QScriptObject::createStructure(qobjectPrototype);
@@ -1770,15 +1772,7 @@ void QScriptEnginePrivate::setProperty(JSC::ExecState *exec, JSC::JSValue object
} else if (flags != QScriptValue::KeepExistingFlags) {
if (thisObject->hasOwnProperty(exec, id))
thisObject->deleteProperty(exec, id); // ### hmmm - can't we just update the attributes?
- unsigned attribs = 0;
- if (flags & QScriptValue::ReadOnly)
- attribs |= JSC::ReadOnly;
- if (flags & QScriptValue::SkipInEnumeration)
- attribs |= JSC::DontEnum;
- if (flags & QScriptValue::Undeletable)
- attribs |= JSC::DontDelete;
- attribs |= flags & QScriptValue::UserRange;
- thisObject->putWithAttributes(exec, id, value, attribs);
+ thisObject->putWithAttributes(exec, id, value, propertyFlagsToJSCAttributes(flags));
} else {
JSC::PutPropertySlot slot;
thisObject->put(exec, id, value, slot);
diff --git a/src/script/api/qscriptengine_p.h b/src/script/api/qscriptengine_p.h
index 56366e2..1b35704 100644
--- a/src/script/api/qscriptengine_p.h
+++ b/src/script/api/qscriptengine_p.h
@@ -205,6 +205,7 @@ public:
inline QScriptValue scriptValueFromJSCValue(JSC::JSValue value);
inline JSC::JSValue scriptValueToJSCValue(const QScriptValue &value);
+ static inline unsigned propertyFlagsToJSCAttributes(const QScriptValue::PropertyFlags &flags);
static inline JSC::JSValue jscValueFromVariant(JSC::ExecState*, const QVariant &value);
static QVariant jscValueToVariant(JSC::ExecState*, JSC::JSValue value, int targetType);
@@ -346,6 +347,7 @@ public:
JSC::ExecState *currentFrame;
WTF::RefPtr<JSC::Structure> scriptObjectStructure;
+ WTF::RefPtr<JSC::Structure> staticScopeObjectStructure;
QScript::QObjectPrototype *qobjectPrototype;
WTF::RefPtr<JSC::Structure> qobjectWrapperObjectStructure;
@@ -639,6 +641,19 @@ inline JSC::JSValue QScriptEnginePrivate::scriptValueToJSCValue(const QScriptVal
return vv->jscValue;
}
+inline unsigned QScriptEnginePrivate::propertyFlagsToJSCAttributes(const QScriptValue::PropertyFlags &flags)
+{
+ unsigned attribs = 0;
+ if (flags & QScriptValue::ReadOnly)
+ attribs |= JSC::ReadOnly;
+ if (flags & QScriptValue::SkipInEnumeration)
+ attribs |= JSC::DontEnum;
+ if (flags & QScriptValue::Undeletable)
+ attribs |= JSC::DontDelete;
+ attribs |= flags & QScriptValue::UserRange;
+ return attribs;
+}
+
inline QScriptValuePrivate::~QScriptValuePrivate()
{
if (engine)