diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2010-06-23 15:22:47 (GMT) |
---|---|---|
committer | Kent Hansen <kent.hansen@nokia.com> | 2010-06-25 06:59:13 (GMT) |
commit | a91f8d704c3ff3826f0ea8b7e73fc6d91dd5b836 (patch) | |
tree | 26c0bb1921eb8b3fc73b01be94dc1823d7322e19 /tests | |
parent | f793344d17b0ec5df4987e123cc1e3b008a949bb (diff) | |
download | Qt-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 'tests')
-rw-r--r-- | tests/auto/qscriptengine/tst_qscriptengine.cpp | 242 | ||||
-rw-r--r-- | tests/benchmarks/script/qscriptengine/tst_qscriptengine.cpp | 52 |
2 files changed, 294 insertions, 0 deletions
diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index 5e59950..6885adf 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -51,6 +51,8 @@ #include <QtCore/qnumeric.h> #include <stdlib.h> +#include <QtScript/private/qscriptdeclarativeclass_p.h> + Q_DECLARE_METATYPE(QList<int>) Q_DECLARE_METATYPE(QObjectList) Q_DECLARE_METATYPE(QScriptProgram) @@ -169,6 +171,8 @@ private slots: void qRegExpInport_data(); void qRegExpInport(); void reentrency(); + void newFixedStaticScopeObject(); + void newGrowingStaticScopeObject(); }; tst_QScriptEngine::tst_QScriptEngine() @@ -4955,5 +4959,243 @@ void tst_QScriptEngine::reentrency() QCOMPARE(eng.evaluate("foo() + hello").toInt32(), 5+6+9); } +void tst_QScriptEngine::newFixedStaticScopeObject() +{ + QScriptEngine eng; + static const int propertyCount = 4; + QString names[] = { "foo", "bar", "baz", "Math" }; + QScriptValue values[] = { 123, "ciao", true, false }; + QScriptValue::PropertyFlags flags[] = { QScriptValue::Undeletable, + QScriptValue::ReadOnly | QScriptValue::Undeletable, + QScriptValue::SkipInEnumeration | QScriptValue::Undeletable, + QScriptValue::Undeletable }; + QScriptValue scope = QScriptDeclarativeClass::newStaticScopeObject(&eng, propertyCount, names, values, flags); + + // Query property. + for (int i = 0; i < propertyCount; ++i) { + for (int x = 0; x < 2; ++x) { + if (x) { + // Properties can't be deleted. + scope.setProperty(names[i], QScriptValue()); + } + QVERIFY(scope.property(names[i]).equals(values[i])); + QCOMPARE(scope.propertyFlags(names[i]), flags[i]); + } + } + + // Property that doesn't exist. + QVERIFY(!scope.property("noSuchProperty").isValid()); + QCOMPARE(scope.propertyFlags("noSuchProperty"), QScriptValue::PropertyFlags()); + + // Write to writable property. + { + QScriptValue oldValue = scope.property("foo"); + QVERIFY(oldValue.isNumber()); + QScriptValue newValue = oldValue.toNumber() * 2; + scope.setProperty("foo", newValue); + QVERIFY(scope.property("foo").equals(newValue)); + scope.setProperty("foo", oldValue); + QVERIFY(scope.property("foo").equals(oldValue)); + } + + // Write to read-only property. + scope.setProperty("bar", 456); + QVERIFY(scope.property("bar").equals("ciao")); + + // Iterate. + { + QScriptValueIterator it(scope); + QSet<QString> iteratedNames; + while (it.hasNext()) { + it.next(); + iteratedNames.insert(it.name()); + } + for (int i = 0; i < propertyCount; ++i) + QVERIFY(iteratedNames.contains(names[i])); + } + + // Push it on the scope chain of a new context. + QScriptContext *ctx = eng.pushContext(); + ctx->pushScope(scope); + QCOMPARE(ctx->scopeChain().size(), 3); // Global Object, native activation, custom scope + QVERIFY(ctx->activationObject().equals(scope)); + + // Read property from JS. + for (int i = 0; i < propertyCount; ++i) { + for (int x = 0; x < 2; ++x) { + if (x) { + // Property can't be deleted from JS. + QScriptValue ret = eng.evaluate(QString::fromLatin1("delete %0").arg(names[i])); + QVERIFY(ret.equals(false)); + } + QVERIFY(eng.evaluate(names[i]).equals(values[i])); + } + } + + // Property that doesn't exist. + QVERIFY(eng.evaluate("noSuchProperty").equals("ReferenceError: Can't find variable: noSuchProperty")); + + // Write property from JS. + { + QScriptValue oldValue = eng.evaluate("foo"); + QVERIFY(oldValue.isNumber()); + QScriptValue newValue = oldValue.toNumber() * 2; + QVERIFY(eng.evaluate("foo = foo * 2; foo").equals(newValue)); + scope.setProperty("foo", oldValue); + QVERIFY(eng.evaluate("foo").equals(oldValue)); + } + + // Write to read-only property. + QVERIFY(eng.evaluate("bar = 456; bar").equals("ciao")); + + // Create a closure and return properties from there. + { + QScriptValue props = eng.evaluate("(function() { var baz = 'shadow'; return [foo, bar, baz, Math, Array]; })()"); + QVERIFY(props.isArray()); + // "foo" and "bar" come from scope object. + QVERIFY(props.property(0).equals(scope.property("foo"))); + QVERIFY(props.property(1).equals(scope.property("bar"))); + // "baz" shadows property in scope object. + QVERIFY(props.property(2).equals("shadow")); + // "Math" comes from scope object, and shadows Global Object's "Math". + QVERIFY(props.property(3).equals(scope.property("Math"))); + QVERIFY(!props.property(3).equals(eng.globalObject().property("Math"))); + // "Array" comes from Global Object. + QVERIFY(props.property(4).equals(eng.globalObject().property("Array"))); + } + + // As with normal JS, assigning to an undefined variable will create + // the property on the Global Object, not the inner scope. + QVERIFY(!eng.globalObject().property("newProperty").isValid()); + QVERIFY(eng.evaluate("(function() { newProperty = 789; })()").isUndefined()); + QVERIFY(!scope.property("newProperty").isValid()); + QVERIFY(eng.globalObject().property("newProperty").isNumber()); + + // Nested static scope. + { + static const int propertyCount2 = 2; + QString names2[] = { "foo", "hum" }; + QScriptValue values2[] = { 321, "hello" }; + QScriptValue::PropertyFlags flags2[] = { QScriptValue::Undeletable, + QScriptValue::ReadOnly | QScriptValue::Undeletable }; + QScriptValue scope2 = QScriptDeclarativeClass::newStaticScopeObject(&eng, propertyCount2, names2, values2, flags2); + ctx->pushScope(scope2); + + // "foo" shadows scope.foo. + QVERIFY(eng.evaluate("foo").equals(scope2.property("foo"))); + QVERIFY(!eng.evaluate("foo").equals(scope.property("foo"))); + // "hum" comes from scope2. + QVERIFY(eng.evaluate("hum").equals(scope2.property("hum"))); + // "Array" comes from Global Object. + QVERIFY(eng.evaluate("Array").equals(eng.globalObject().property("Array"))); + + ctx->popScope(); + } + + QScriptValue fun = eng.evaluate("(function() { return foo; })"); + QVERIFY(fun.isFunction()); + eng.popContext(); + // Function's scope chain persists after popContext(). + QVERIFY(fun.call().equals(scope.property("foo"))); +} + +void tst_QScriptEngine::newGrowingStaticScopeObject() +{ + QScriptEngine eng; + QScriptValue scope = QScriptDeclarativeClass::newStaticScopeObject(&eng); + + // Initially empty. + QVERIFY(!QScriptValueIterator(scope).hasNext()); + QVERIFY(!scope.property("foo").isValid()); + + // Add a static property. + scope.setProperty("foo", 123); + QVERIFY(scope.property("foo").equals(123)); + QCOMPARE(scope.propertyFlags("foo"), QScriptValue::Undeletable); + + // Modify existing property. + scope.setProperty("foo", 456); + QVERIFY(scope.property("foo").equals(456)); + + // Add a read-only property. + scope.setProperty("bar", "ciao", QScriptValue::ReadOnly); + QVERIFY(scope.property("bar").equals("ciao")); + QCOMPARE(scope.propertyFlags("bar"), QScriptValue::ReadOnly | QScriptValue::Undeletable); + + // Attempt to modify read-only property. + scope.setProperty("bar", "hello"); + QVERIFY(scope.property("bar").equals("ciao")); + + // Properties can't be deleted. + scope.setProperty("foo", QScriptValue()); + QVERIFY(scope.property("foo").equals(456)); + scope.setProperty("bar", QScriptValue()); + QVERIFY(scope.property("bar").equals("ciao")); + + // Iterate. + { + QScriptValueIterator it(scope); + QSet<QString> iteratedNames; + while (it.hasNext()) { + it.next(); + iteratedNames.insert(it.name()); + } + QCOMPARE(iteratedNames.size(), 2); + QVERIFY(iteratedNames.contains("foo")); + QVERIFY(iteratedNames.contains("bar")); + } + + // Push it on the scope chain of a new context. + QScriptContext *ctx = eng.pushContext(); + ctx->pushScope(scope); + QCOMPARE(ctx->scopeChain().size(), 3); // Global Object, native activation, custom scope + QVERIFY(ctx->activationObject().equals(scope)); + + // Read property from JS. + QVERIFY(eng.evaluate("foo").equals(scope.property("foo"))); + QVERIFY(eng.evaluate("bar").equals(scope.property("bar"))); + + // Write property from JS. + { + QScriptValue oldValue = eng.evaluate("foo"); + QVERIFY(oldValue.isNumber()); + QScriptValue newValue = oldValue.toNumber() * 2; + QVERIFY(eng.evaluate("foo = foo * 2; foo").equals(newValue)); + scope.setProperty("foo", oldValue); + QVERIFY(eng.evaluate("foo").equals(oldValue)); + } + + // Write to read-only property. + QVERIFY(eng.evaluate("bar = 456; bar").equals("ciao")); + + // Shadow property. + QVERIFY(eng.evaluate("Math").equals(eng.globalObject().property("Math"))); + scope.setProperty("Math", "fake Math"); + QVERIFY(eng.evaluate("Math").equals(scope.property("Math"))); + + // Variable declarations will create properties on the scope. + eng.evaluate("var baz = 456"); + QVERIFY(scope.property("baz").equals(456)); + + // Function declarations will create properties on the scope. + eng.evaluate("function fun() { return baz; }"); + QVERIFY(scope.property("fun").isFunction()); + QVERIFY(scope.property("fun").call().equals(scope.property("baz"))); + + // Demonstrate the limitation of a growable static scope: Once a function that + // uses the scope has been compiled, it won't pick up properties that are added + // to the scope later. + { + QScriptValue fun = eng.evaluate("(function() { return futureProperty; })"); + QVERIFY(fun.isFunction()); + QCOMPARE(fun.call().toString(), QString::fromLatin1("ReferenceError: Can't find variable: futureProperty")); + scope.setProperty("futureProperty", "added after the function was compiled"); + // If scope were dynamic, this would return the new property. + QCOMPARE(fun.call().toString(), QString::fromLatin1("ReferenceError: Can't find variable: futureProperty")); + } + + eng.popContext(); +} + QTEST_MAIN(tst_QScriptEngine) #include "tst_qscriptengine.moc" diff --git a/tests/benchmarks/script/qscriptengine/tst_qscriptengine.cpp b/tests/benchmarks/script/qscriptengine/tst_qscriptengine.cpp index 35e2f28..4610046 100644 --- a/tests/benchmarks/script/qscriptengine/tst_qscriptengine.cpp +++ b/tests/benchmarks/script/qscriptengine/tst_qscriptengine.cpp @@ -42,6 +42,8 @@ #include <qtest.h> #include <QtScript> +#include <QtScript/private/qscriptdeclarativeclass_p.h> + //TESTED_FILES= class tst_QScriptEngine : public QObject @@ -74,6 +76,8 @@ private slots: void nativeCall(); void translation_data(); void translation(); + void readScopeProperty_data(); + void readScopeProperty(); }; tst_QScriptEngine::tst_QScriptEngine() @@ -288,5 +292,53 @@ void tst_QScriptEngine::translation() } } +void tst_QScriptEngine::readScopeProperty_data() +{ + QTest::addColumn<bool>("staticScope"); + QTest::addColumn<bool>("nestedScope"); + QTest::newRow("single dynamic scope") << false << false; + QTest::newRow("single static scope") << true << false; + QTest::newRow("double dynamic scope") << false << true; + QTest::newRow("double static scope") << true << true; +} + +void tst_QScriptEngine::readScopeProperty() +{ + QFETCH(bool, staticScope); + QFETCH(bool, nestedScope); + + QScriptEngine engine; + QScriptContext *ctx = engine.pushContext(); + + QScriptValue scope; + if (staticScope) + scope = QScriptDeclarativeClass::newStaticScopeObject(&engine); + else + scope = engine.newObject(); + scope.setProperty("foo", 123); + ctx->pushScope(scope); + + if (nestedScope) { + QScriptValue scope2; + if (staticScope) + scope2 = QScriptDeclarativeClass::newStaticScopeObject(&engine); + else + scope2 = engine.newObject(); + scope2.setProperty("bar", 456); // ensure a miss in inner scope + ctx->pushScope(scope2); + } + + QScriptValue fun = engine.evaluate("(function() {\n" + " for (var i = 0; i < 10000; ++i) {\n" + " foo; foo; foo; foo; foo; foo; foo; foo;\n" + " }\n" + "})"); + engine.popContext(); + QVERIFY(fun.isFunction()); + QBENCHMARK { + fun.call(); + } +} + QTEST_MAIN(tst_QScriptEngine) #include "tst_qscriptengine.moc" |