diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2010-03-05 11:23:09 (GMT) |
---|---|---|
committer | Kent Hansen <kent.hansen@nokia.com> | 2010-03-05 13:24:22 (GMT) |
commit | 8a9a160c8f509511b7e44b66a20d60fb37d614cc (patch) | |
tree | b7723c1e7ae6aef1a0a71f8dac2c93bbd671104a /src/script | |
parent | 88e9515cc4300d841b16f17fe6b5c8c0d6de3562 (diff) | |
download | Qt-8a9a160c8f509511b7e44b66a20d60fb37d614cc.zip Qt-8a9a160c8f509511b7e44b66a20d60fb37d614cc.tar.gz Qt-8a9a160c8f509511b7e44b66a20d60fb37d614cc.tar.bz2 |
QtScript: Don't needlessly make deep copies of function names
The QObject binding uses the function name to determine whether
a meta-method is an overload. This was implemented quite naively
by copying the name from the signature into another array before
comparing it. This could cause several unnecessary memory
allocations, since storing the name is really only needed when
there is an exception (e.g. ambiguous call).
Instead, use strncmp to compare only the relevant characters of
the original (non-copied) method signature.
This makes normal slot invocation from QtScript up to 15% faster.
Reviewed-by: Jedrzej Nowacki
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/bridge/qscriptqobject.cpp | 89 | ||||
-rw-r--r-- | src/script/bridge/qscriptqobject_p.h | 1 |
2 files changed, 55 insertions, 35 deletions
diff --git a/src/script/bridge/qscriptqobject.cpp b/src/script/bridge/qscriptqobject.cpp index 91636da..fbb29f5 100644 --- a/src/script/bridge/qscriptqobject.cpp +++ b/src/script/bridge/qscriptqobject.cpp @@ -163,10 +163,40 @@ static bool isEnumerableMetaProperty(const QMetaProperty &prop, && (mo->indexOfProperty(prop.name()) == index); } -static inline QByteArray methodName(const QMetaMethod &method) +/*! \internal + Calculates the length of the name of the given \a method by looking + for the first '(' character. +*/ +static inline int methodNameLength(const QMetaMethod &method) { - QByteArray signature = method.signature(); - return signature.left(signature.indexOf('(')); + const char *signature = method.signature(); + const char *s = signature; + while (*s && (*s != '(')) + ++s; + return s - signature; +} + +/*! \internal + Makes a deep copy of the first \a nameLength characters of the given + method \a signature and returns the copy. +*/ +static inline QByteArray methodName(const char *signature, int nameLength) +{ + return QByteArray(signature, nameLength); +} + +/*! \internal + + Returns true if the name of the given \a method is the same as that + specified by the (signature, nameLength) pair, otherwise returns + false. +*/ +static inline bool methodNameEquals(const QMetaMethod &method, + const char *signature, int nameLength) +{ + const char *otherSignature = method.signature(); + return !qstrncmp(otherSignature, signature, nameLength) + && (otherSignature[nameLength] == '('); } static QVariant variantFromValue(JSC::ExecState *exec, int targetType, JSC::JSValue value) @@ -310,25 +340,16 @@ QList<int> QScript::QtFunction::overloadedIndexes() const if (!maybeOverloaded()) return QList<int>(); QList<int> result; - QString name = functionName(); const QMetaObject *meta = metaObject(); + QMetaMethod method = meta->method(initialIndex()); + int nameLength = methodNameLength(method); for (int index = mostGeneralMethod() - 1; index >= 0; --index) { - QString otherName = QString::fromLatin1(methodName(meta->method(index))); - if (otherName == name) + if (methodNameEquals(meta->method(index), method.signature(), nameLength)) result.append(index); } return result; } -QString QtFunction::functionName() const -{ - const QMetaObject *meta = metaObject(); - if (!meta) - return QString(); - QMetaMethod method = meta->method(initialIndex()); - return QLatin1String(methodName(method)); -} - class QScriptMetaType { public: @@ -415,8 +436,8 @@ class QScriptMetaMethod public: inline QScriptMetaMethod() { } - inline QScriptMetaMethod(const QByteArray &name, const QVector<QScriptMetaType> &types) - : m_name(name), m_types(types), m_firstUnresolvedIndex(-1) + inline QScriptMetaMethod(const QVector<QScriptMetaType> &types) + : m_types(types), m_firstUnresolvedIndex(-1) { QVector<QScriptMetaType>::const_iterator it; for (it = m_types.constBegin(); it != m_types.constEnd(); ++it) { @@ -429,9 +450,6 @@ public: inline bool isValid() const { return !m_types.isEmpty(); } - QByteArray name() const - { return m_name; } - inline QScriptMetaType returnType() const { return m_types.at(0); } @@ -460,7 +478,6 @@ public: { return m_types; } private: - QByteArray m_name; QVector<QScriptMetaType> m_types; int m_firstUnresolvedIndex; }; @@ -497,7 +514,6 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c const QMetaObject *meta, int initialIndex, bool maybeOverloaded) { - QByteArray funName; QScriptMetaMethod chosenMethod; int chosenIndex = -1; QVarLengthArray<QVariant, 9> args; @@ -506,15 +522,18 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c QVector<int> tooFewArgs; QVector<int> conversionFailed; int index; + int nameLength = 0; + const char *initialMethodSignature = 0; exec->clearException(); QScriptEnginePrivate *engine = QScript::scriptEngineFromExec(exec); for (index = initialIndex; index >= 0; --index) { QMetaMethod method = metaMethod(meta, callType, index); - if (index == initialIndex) - funName = methodName(method); - else { - if (methodName(method) != funName) + if (index == initialIndex) { + initialMethodSignature = method.signature(); + nameLength = methodNameLength(method); + } else { + if (!methodNameEquals(method, initialMethodSignature, nameLength)) continue; } @@ -555,7 +574,7 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c } } - QScriptMetaMethod mtd = QScriptMetaMethod(methodName(method), types); + QScriptMetaMethod mtd = QScriptMetaMethod(types); if (int(scriptArgs.size()) < mtd.argumentCount()) { tooFewArgs.append(index); @@ -830,9 +849,10 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c //#ifndef Q_SCRIPT_NO_EVENT_NOTIFY // engine->notifyFunctionEntry(context); //#endif + QString funName = QString::fromLatin1(methodName(initialMethodSignature, nameLength)); if (!conversionFailed.isEmpty()) { QString message = QString::fromLatin1("incompatible type of argument(s) in call to %0(); candidates were\n") - .arg(QLatin1String(funName)); + .arg(funName); for (int i = 0; i < conversionFailed.size(); ++i) { if (i > 0) message += QLatin1String("\n"); @@ -847,7 +867,7 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c QScriptMetaType unresolvedType = argsInstance.method.type(unresolvedIndex); QString unresolvedTypeName = QString::fromLatin1(unresolvedType.name()); QString message = QString::fromLatin1("cannot call %0(): ") - .arg(QString::fromLatin1(funName)); + .arg(funName); if (unresolvedIndex > 0) { message.append(QString::fromLatin1("argument %0 has unknown type `%1'"). arg(unresolvedIndex).arg(unresolvedTypeName)); @@ -859,7 +879,7 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c result = JSC::throwError(exec, JSC::TypeError, message); } else { QString message = QString::fromLatin1("too few arguments in call to %0(); candidates are\n") - .arg(QLatin1String(funName)); + .arg(funName); for (int i = 0; i < tooFewArgs.size(); ++i) { if (i > 0) message += QLatin1String("\n"); @@ -875,6 +895,7 @@ static JSC::JSValue callQtMethod(JSC::ExecState *exec, QMetaMethod::MethodType c && (metaArgs.args.count() == candidates.at(1).args.count()) && (metaArgs.matchDistance == candidates.at(1).matchDistance)) { // ambiguous call + QByteArray funName = methodName(initialMethodSignature, nameLength); QString message = QString::fromLatin1("ambiguous call of overloaded function %0(); candidates were\n") .arg(QLatin1String(funName)); for (int i = 0; i < candidates.size(); ++i) { @@ -1240,7 +1261,7 @@ bool QObjectDelegate::getOwnPropertySlot(QScriptObject *object, JSC::ExecState * for (index = meta->methodCount() - 1; index >= offset; --index) { QMetaMethod method = meta->method(index); if (hasMethodAccess(method, index, opt) - && (methodName(method) == name)) { + && methodNameEquals(method, name.constData(), name.length())) { QtFunction *fun = new (exec)QtFunction( object, index, /*maybeOverloaded=*/true, &exec->globalData(), eng->originalGlobalObject()->functionStructure(), @@ -1372,7 +1393,7 @@ bool QObjectDelegate::getOwnPropertyDescriptor(QScriptObject *object, JSC::ExecS for (index = meta->methodCount() - 1; index >= offset; --index) { QMetaMethod method = meta->method(index); if (hasMethodAccess(method, index, opt) - && (methodName(method) == name)) { + && methodNameEquals(method, name.constData(), name.length())) { QtFunction *fun = new (exec)QtFunction( object, index, /*maybeOverloaded=*/true, &exec->globalData(), eng->originalGlobalObject()->functionStructure(), @@ -1486,7 +1507,7 @@ void QObjectDelegate::put(QScriptObject *object, JSC::ExecState* exec, for (index = meta->methodCount() - 1; index >= offset; --index) { QMetaMethod method = meta->method(index); if (hasMethodAccess(method, index, opt) - && (methodName(method) == name)) { + && methodNameEquals(method, name.constData(), name.length())) { data->cachedMembers.insert(name, value); return; } @@ -1605,7 +1626,7 @@ bool QObjectDelegate::getPropertyAttributes(const QScriptObject *object, for (index = meta->methodCount() - 1; index >= offset; --index) { QMetaMethod method = meta->method(index); if (hasMethodAccess(method, index, opt) - && (methodName(method) == name)) { + && methodNameEquals(method, name.constData(), name.length())) { attributes = QObjectMemberAttribute; if (opt & QScriptEngine::SkipMethodsInEnumeration) attributes |= JSC::DontEnum; diff --git a/src/script/bridge/qscriptqobject_p.h b/src/script/bridge/qscriptqobject_p.h index 448fa99..8b05d6b 100644 --- a/src/script/bridge/qscriptqobject_p.h +++ b/src/script/bridge/qscriptqobject_p.h @@ -212,7 +212,6 @@ public: bool maybeOverloaded() const; int mostGeneralMethod(QMetaMethod *out = 0) const; QList<int> overloadedIndexes() const; - QString functionName() const; private: Data *data; |