diff options
author | Lars Knoll <lars.knoll@digia.com> | 2014-01-17 12:19:34 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-01-20 21:14:35 +0100 |
commit | 393108500832dcefa4c4def442a08f20d3fbc4cd (patch) | |
tree | 64676214630008b9f5434c933590d9adf9baf094 /src/qml/jsruntime | |
parent | bf6191f090b326eb0776cd4e921ddb56c8daa8a9 (diff) |
Use lookups for indexed accesses
This speeds up reading array data from objects significantly.
Change-Id: I5d17a7b3e7583a16dc76d1ee6cbc1d7134e4c2fa
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src/qml/jsruntime')
-rw-r--r-- | src/qml/jsruntime/qv4lookup.cpp | 72 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4lookup_p.h | 6 |
2 files changed, 78 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4lookup.cpp b/src/qml/jsruntime/qv4lookup.cpp index 80b8b08a1a..c9d22523a9 100644 --- a/src/qml/jsruntime/qv4lookup.cpp +++ b/src/qml/jsruntime/qv4lookup.cpp @@ -46,6 +46,7 @@ QT_BEGIN_NAMESPACE using namespace QV4; + ReturnedValue Lookup::lookup(ValueRef thisObject, Object *obj, PropertyAttributes *attrs) { int i = 0; @@ -107,6 +108,77 @@ ReturnedValue Lookup::lookup(Object *obj, PropertyAttributes *attrs) return Primitive::emptyValue().asReturnedValue(); } +ReturnedValue Lookup::indexedGetterGeneric(Lookup *l, const ValueRef object, const ValueRef index) +{ + if (object->isObject() && index->asArrayIndex() < UINT_MAX) { + l->indexedGetter = indexedGetterObjectInt; + return indexedGetterObjectInt(l, object, index); + } + return indexedGetterFallback(l, object, index); +} + +ReturnedValue Lookup::indexedGetterFallback(Lookup *l, const ValueRef object, const ValueRef index) +{ + Q_UNUSED(l); + ExecutionContext *ctx = l->engine->currentContext(); + Scope scope(ctx); + uint idx = index->asArrayIndex(); + + Scoped<Object> o(scope, object); + if (!o) { + if (idx < UINT_MAX) { + if (String *str = object->asString()) { + if (idx >= (uint)str->toQString().length()) { + return Encode::undefined(); + } + const QString s = str->toQString().mid(idx, 1); + return scope.engine->newString(s)->asReturnedValue(); + } + } + + if (object->isNullOrUndefined()) { + QString message = QStringLiteral("Cannot read property '%1' of %2").arg(index->toQStringNoThrow()).arg(object->toQStringNoThrow()); + return ctx->throwTypeError(message); + } + + o = __qmljs_convert_to_object(ctx, object); + if (!o) // type error + return Encode::undefined(); + } + + if (idx < UINT_MAX) { + if (!o->arrayData->hasAttributes()) { + ScopedValue v(scope, o->arrayData->get(idx)); + if (!v->isEmpty()) + return v->asReturnedValue(); + } + + return o->getIndexed(idx); + } + + ScopedString name(scope, index->toString(ctx)); + if (scope.hasException()) + return Encode::undefined(); + return o->get(name); + +} + + +ReturnedValue Lookup::indexedGetterObjectInt(Lookup *l, const ValueRef object, const ValueRef index) +{ + uint idx = index->asArrayIndex(); + if (idx == UINT_MAX || !object->isObject()) + return indexedGetterGeneric(l, object, index); + + Object *o = object->objectValue(); + if (o->arrayData && o->arrayData->type == ArrayData::Simple) { + if (idx < static_cast<SimpleArrayData *>(o->arrayData)->len) + if (!o->arrayData->data[idx].isEmpty()) + return o->arrayData->data[idx].asReturnedValue(); + } + + return indexedGetterFallback(l, object, index); +} ReturnedValue Lookup::getterGeneric(QV4::Lookup *l, const ValueRef object) { diff --git a/src/qml/jsruntime/qv4lookup_p.h b/src/qml/jsruntime/qv4lookup_p.h index f47b603c15..50874411de 100644 --- a/src/qml/jsruntime/qv4lookup_p.h +++ b/src/qml/jsruntime/qv4lookup_p.h @@ -55,11 +55,13 @@ namespace QV4 { struct Lookup { enum { Size = 4 }; union { + ReturnedValue (*indexedGetter)(Lookup *l, const ValueRef object, const ValueRef index); ReturnedValue (*getter)(Lookup *l, const ValueRef object); ReturnedValue (*globalGetter)(Lookup *l, ExecutionContext *ctx); void (*setter)(Lookup *l, const ValueRef object, const ValueRef v); }; union { + ExecutionEngine *engine; InternalClass *classList[Size]; struct { void *dummy0; @@ -72,6 +74,10 @@ struct Lookup { uint index; String *name; + static ReturnedValue indexedGetterGeneric(Lookup *l, const ValueRef object, const ValueRef index); + static ReturnedValue indexedGetterFallback(Lookup *l, const ValueRef object, const ValueRef index); + static ReturnedValue indexedGetterObjectInt(Lookup *l, const ValueRef object, const ValueRef index); + static ReturnedValue getterGeneric(Lookup *l, const ValueRef object); static ReturnedValue getter0(Lookup *l, const ValueRef object); static ReturnedValue getter1(Lookup *l, const ValueRef object); |