qml: Avoid heap allocations due to QString::fromLatin1()
authorSérgio Martins <sergio.martins@kdab.com>
Wed, 12 Aug 2015 12:17:58 +0000 (13:17 +0100)
committerSérgio Martins <sergio.martins@kdab.com>
Thu, 13 Aug 2015 16:11:48 +0000 (16:11 +0000)
By using QStringLiteral when the argument is a literal.

Change-Id: Ib25042d10f3d9d0aca81af74cde0107aba4f9432
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
src/qml/compiler/qv4ssa.cpp
src/qml/jsruntime/qv4errorobject.cpp
src/qml/jsruntime/qv4numberobject.cpp
src/qml/jsruntime/qv4objectproto.cpp
src/qml/jsruntime/qv4qobjectwrapper.cpp
src/qml/jsruntime/qv4variantobject.cpp
src/qml/qml/qqmldirparser.cpp
src/qml/qml/qqmlengine.cpp
src/qml/qml/qqmlglobal.cpp
src/qml/qml/v8/qqmlbuiltinfunctions.cpp
src/qml/types/qqmllistmodel.cpp

index cc60dde4cc30ef55c1b0cef7a788f31f174240d2..62e661c98aa4af95241763b9743584367b9bf9ab 100644 (file)
@@ -3912,7 +3912,7 @@ void cfg2dot(IR::Function *f, const QVector<LoopDetection::LoopInfo *> &loops =
 
     QString name;
     if (f->name) name = *f->name;
-    else name = QString::fromLatin1("%1").arg((unsigned long long)f);
+    else name = QStringLiteral("%1").arg((unsigned long long)f);
     qout << "digraph \"" << name << "\" { ordering=out;\n";
 
     foreach (LoopDetection::LoopInfo *l, loops) {
index 3c0a05483cc6b46250438b89f371b4379d23787e..a6c2a25b91e79a799d3c9a00d9cd3e1595a66140 100644 (file)
@@ -361,11 +361,11 @@ ReturnedValue ErrorPrototype::method_toString(CallContext *ctx)
     ScopedValue name(scope, o->get(ctx->d()->engine->id_name()));
     QString qname;
     if (name->isUndefined())
-        qname = QString::fromLatin1("Error");
+        qname = QStringLiteral("Error");
     else
         qname = name->toQString();
 
-    ScopedString s(scope, ctx->d()->engine->newString(QString::fromLatin1("message")));
+    ScopedString s(scope, ctx->d()->engine->newString(QStringLiteral("message")));
     ScopedValue message(scope, o->get(s));
     QString qmessage;
     if (!message->isUndefined())
index 36b83090e18fefc283c3e9d013fdb8fa9d02d68f..096fe9fb4ce32ae12cfbe2eb70091e8b093ca54e 100644 (file)
@@ -124,7 +124,7 @@ ReturnedValue NumberPrototype::method_toString(CallContext *ctx)
     if (ctx->argc() && !ctx->args()[0].isUndefined()) {
         int radix = ctx->args()[0].toInt32();
         if (radix < 2 || radix > 36)
-            return ctx->engine()->throwError(QString::fromLatin1("Number.prototype.toString: %0 is not a valid radix")
+            return ctx->engine()->throwError(QStringLiteral("Number.prototype.toString: %0 is not a valid radix")
                             .arg(radix));
 
         if (std::isnan(num)) {
@@ -202,7 +202,7 @@ ReturnedValue NumberPrototype::method_toFixed(CallContext *ctx)
 
     QString str;
     if (std::isnan(v))
-        str = QString::fromLatin1("NaN");
+        str = QStringLiteral("NaN");
     else if (qIsInf(v))
         str = QString::fromLatin1(v < 0 ? "-Infinity" : "Infinity");
     else if (v < 1.e21) {
index 41483e9ef58e4fa2e38dc0bb6b1edd75f5df05ca..1edf76e2de2b2991f4c476c87d0ec979d9762250 100644 (file)
@@ -392,7 +392,7 @@ ReturnedValue ObjectPrototype::method_toString(CallContext *ctx)
     } else {
         ScopedObject obj(scope, RuntimeHelpers::toObject(scope.engine, ctx->thisObject()));
         QString className = obj->className();
-        return ctx->d()->engine->newString(QString::fromLatin1("[object %1]").arg(className))->asReturnedValue();
+        return ctx->d()->engine->newString(QStringLiteral("[object %1]").arg(className))->asReturnedValue();
     }
 }
 
index 31abab2e2ffb19a0465d27bc1b9bcf115909e49f..529703bda41cd10c3ed265adaa0e7066eb682d2b 100644 (file)
@@ -835,7 +835,7 @@ struct QObjectSlotDispatcher : public QtPrivate::QSlotObjectBase
                 QQmlError error = v4->catchExceptionAsQmlError();
                 if (error.description().isEmpty()) {
                     QV4::ScopedString name(scope, f->name());
-                    error.setDescription(QString::fromLatin1("Unknown exception occurred during evaluation of connected function: %1").arg(name->toQString()));
+                    error.setDescription(QStringLiteral("Unknown exception occurred during evaluation of connected function: %1").arg(name->toQString()));
                 }
                 if (QQmlEngine *qmlEngine = v4->qmlEngine()) {
                     QQmlEnginePrivate::get(qmlEngine)->warning(error);
@@ -1371,7 +1371,7 @@ static QV4::ReturnedValue CallPrecise(const QQmlObjectOrGadget &object, const QQ
 
     if (returnType == QMetaType::UnknownType) {
         QString typeName = QString::fromLatin1(unknownTypeError);
-        QString error = QString::fromLatin1("Unknown method return type: %1").arg(typeName);
+        QString error = QStringLiteral("Unknown method return type: %1").arg(typeName);
         return engine->throwError(error);
     }
 
@@ -1384,7 +1384,7 @@ static QV4::ReturnedValue CallPrecise(const QQmlObjectOrGadget &object, const QQ
 
         if (!args) {
             QString typeName = QString::fromLatin1(unknownTypeError);
-            QString error = QString::fromLatin1("Unknown method parameter type: %1").arg(typeName);
+            QString error = QStringLiteral("Unknown method parameter type: %1").arg(typeName);
             return engine->throwError(error);
         }
 
index c919d2446138e824e5cc92a42f9a0443cdc2a071..f9e26efe71aadb7b52b4c8ae1af3d0e326431481 100644 (file)
@@ -129,7 +129,7 @@ QV4::ReturnedValue VariantPrototype::method_toString(CallContext *ctx)
         return Encode::undefined();
     QString result = o->d()->data.toString();
     if (result.isEmpty() && !o->d()->data.canConvert(QVariant::String))
-        result = QString::fromLatin1("QVariant(%0)").arg(QString::fromLatin1(o->d()->data.typeName()));
+        result = QStringLiteral("QVariant(%0)").arg(QString::fromLatin1(o->d()->data.typeName()));
     return Encode(ctx->d()->engine->newString(result));
 }
 
index a45152e0a0b12a2cb9985543d7c630c45354fcc6..57b50733ea92283a6bc28a918bdc7b51b9062ffd 100644 (file)
@@ -146,7 +146,7 @@ bool QQmlDirParser::parse(const QString &source)
 
         if (invalidLine) {
             reportError(lineNumber, 0,
-                        QString::fromLatin1("invalid qmldir directive contains too many tokens"));
+                        QStringLiteral("invalid qmldir directive contains too many tokens"));
             continue;
         } else if (sectionCount == 0) {
             continue; // no sections, no party.
@@ -154,17 +154,17 @@ bool QQmlDirParser::parse(const QString &source)
         } else if (sections[0] == QLatin1String("module")) {
             if (sectionCount != 2) {
                 reportError(lineNumber, 0,
-                            QString::fromLatin1("module identifier directive requires one argument, but %1 were provided").arg(sectionCount - 1));
+                            QStringLiteral("module identifier directive requires one argument, but %1 were provided").arg(sectionCount - 1));
                 continue;
             }
             if (!_typeNamespace.isEmpty()) {
                 reportError(lineNumber, 0,
-                            QString::fromLatin1("only one module identifier directive may be defined in a qmldir file"));
+                            QStringLiteral("only one module identifier directive may be defined in a qmldir file"));
                 continue;
             }
             if (!firstLine) {
                 reportError(lineNumber, 0,
-                            QString::fromLatin1("module identifier directive must be the first directive in a qmldir file"));
+                            QStringLiteral("module identifier directive must be the first directive in a qmldir file"));
                 continue;
             }
 
@@ -173,7 +173,7 @@ bool QQmlDirParser::parse(const QString &source)
         } else if (sections[0] == QLatin1String("plugin")) {
             if (sectionCount < 2 || sectionCount > 3) {
                 reportError(lineNumber, 0,
-                            QString::fromLatin1("plugin directive requires one or two arguments, but %1 were provided").arg(sectionCount - 1));
+                            QStringLiteral("plugin directive requires one or two arguments, but %1 were provided").arg(sectionCount - 1));
 
                 continue;
             }
@@ -185,7 +185,7 @@ bool QQmlDirParser::parse(const QString &source)
         } else if (sections[0] == QLatin1String("internal")) {
             if (sectionCount != 3) {
                 reportError(lineNumber, 0,
-                            QString::fromLatin1("internal types require 2 arguments, but %1 were provided").arg(sectionCount - 1));
+                            QStringLiteral("internal types require 2 arguments, but %1 were provided").arg(sectionCount - 1));
                 continue;
             }
             Component entry(sections[1], sections[2], -1, -1);
@@ -194,7 +194,7 @@ bool QQmlDirParser::parse(const QString &source)
         } else if (sections[0] == QLatin1String("singleton")) {
             if (sectionCount < 3 || sectionCount > 4) {
                 reportError(lineNumber, 0,
-                            QString::fromLatin1("singleton types require 2 or 3 arguments, but %1 were provided").arg(sectionCount - 1));
+                            QStringLiteral("singleton types require 2 or 3 arguments, but %1 were provided").arg(sectionCount - 1));
                 continue;
             } else if (sectionCount == 3) {
                 // handle qmldir directory listing case where singleton is defined in the following pattern:
@@ -218,7 +218,7 @@ bool QQmlDirParser::parse(const QString &source)
         } else if (sections[0] == QLatin1String("typeinfo")) {
             if (sectionCount != 2) {
                 reportError(lineNumber, 0,
-                            QString::fromLatin1("typeinfo requires 1 argument, but %1 were provided").arg(sectionCount - 1));
+                            QStringLiteral("typeinfo requires 1 argument, but %1 were provided").arg(sectionCount - 1));
                 continue;
             }
 #ifdef QT_CREATOR
@@ -228,13 +228,13 @@ bool QQmlDirParser::parse(const QString &source)
 
         } else if (sections[0] == QLatin1String("designersupported")) {
             if (sectionCount != 1)
-                reportError(lineNumber, 0, QString::fromLatin1("designersupported does not expect any argument"));
+                reportError(lineNumber, 0, QStringLiteral("designersupported does not expect any argument"));
             else
                 _designerSupported = true;
         } else if (sections[0] == QLatin1String("depends")) {
             if (sectionCount != 3) {
                 reportError(lineNumber, 0,
-                            QString::fromLatin1("depends requires 2 arguments, but %1 were provided").arg(sectionCount - 1));
+                            QStringLiteral("depends requires 2 arguments, but %1 were provided").arg(sectionCount - 1));
                 continue;
             }
 
@@ -268,7 +268,7 @@ bool QQmlDirParser::parse(const QString &source)
             }
         } else {
             reportError(lineNumber, 0,
-                        QString::fromLatin1("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
+                        QStringLiteral("a component declaration requires two or three arguments, but %1 were provided").arg(sectionCount));
         }
 
         firstLine = false;
@@ -364,14 +364,14 @@ bool QQmlDirParser::designerSupported() const
 
 QDebug &operator<< (QDebug &debug, const QQmlDirParser::Component &component)
 {
-    const QString output = QString::fromLatin1("{%1 %2.%3}").
+    const QString output = QStringLiteral("{%1 %2.%3}").
         arg(component.typeName).arg(component.majorVersion).arg(component.minorVersion);
     return debug << qPrintable(output);
 }
 
 QDebug &operator<< (QDebug &debug, const QQmlDirParser::Script &script)
 {
-    const QString output = QString::fromLatin1("{%1 %2.%3}").
+    const QString output = QStringLiteral("{%1 %2.%3}").
         arg(script.nameSpace).arg(script.majorVersion).arg(script.minorVersion);
     return debug << qPrintable(output);
 }
index df5f50644c0c431b99932f7266bcb353d2983c7d..70c682dedd6efdfe4f0aad5cf6e33337d1c1bde2 100644 (file)
@@ -1638,7 +1638,7 @@ void QQmlData::destroyed(QObject *object)
                 if (location.sourceFile.isEmpty())
                     location.sourceFile = QStringLiteral("<Unknown File>");
                 locationString.append(location.sourceFile);
-                locationString.append(QString::fromLatin1(":%0: ").arg(location.line));
+                locationString.append(QStringLiteral(":%0: ").arg(location.line));
                 QString source = expr->expression();
                 if (source.size() > 100) {
                     source.truncate(96);
index d904242f9379b9615ad3c9c181a978cd769d8bb1..0a2f4079c2cc4754d16b0e895f198620cb0007c4 100644 (file)
@@ -359,7 +359,7 @@ QObject *QQmlGuiProvider::inputMethod()
 {
     // We don't have any input method code by default
     QObject *o = new QObject();
-    o->setObjectName(QString::fromLatin1("No inputMethod available"));
+    o->setObjectName(QStringLiteral("No inputMethod available"));
     QQmlEngine::setObjectOwnership(o, QQmlEngine::JavaScriptOwnership);
     return o;
 }
@@ -368,7 +368,7 @@ QObject *QQmlGuiProvider::inputMethod()
 QObject *QQmlGuiProvider::styleHints()
 {
     QObject *o = new QObject();
-    o->setObjectName(QString::fromLatin1("No styleHints available"));
+    o->setObjectName(QStringLiteral("No styleHints available"));
     QQmlEngine::setObjectOwnership(o, QQmlEngine::JavaScriptOwnership);
     return o;
 }
index c0e3b4129b17c6cdfecd4bf8d2de292da0b25b1f..ce86fd3923806220f16a9ffb7aa28a929cceb9f8 100644 (file)
@@ -1355,12 +1355,12 @@ static QString jsStack(QV4::ExecutionEngine *engine) {
 
         QString stackFrame;
         if (frame.column >= 0)
-            stackFrame = QString::fromLatin1("%1 (%2:%3:%4)").arg(frame.function,
+            stackFrame = QStringLiteral("%1 (%2:%3:%4)").arg(frame.function,
                                                              frame.source,
                                                              QString::number(frame.line),
                                                              QString::number(frame.column));
         else
-            stackFrame = QString::fromLatin1("%1 (%2:%3)").arg(frame.function,
+            stackFrame = QStringLiteral("%1 (%2:%3)").arg(frame.function,
                                                              frame.source,
                                                              QString::number(frame.line));
 
index 6fc0e4a9d8325a4a1b1f85cc20a027706af4426f..de46020ad8a92a90b1e581ad22cbd57017de464c 100644 (file)
@@ -88,7 +88,7 @@ const ListLayout::Role &ListLayout::getRoleOrCreate(const QString &key, Role::Da
     if (node) {
         const Role &r = *node->value;
         if (type != r.type)
-            qmlInfo(0) << QString::fromLatin1("Can't assign to existing role '%1' of different type [%2 -> %3]").arg(r.name).arg(roleTypeName(type)).arg(roleTypeName(r.type));
+            qmlInfo(0) << QStringLiteral("Can't assign to existing role '%1' of different type [%2 -> %3]").arg(r.name).arg(roleTypeName(type)).arg(roleTypeName(r.type));
         return r;
     }
 
@@ -101,7 +101,7 @@ const ListLayout::Role &ListLayout::getRoleOrCreate(QV4::String *key, Role::Data
     if (node) {
         const Role &r = *node->value;
         if (type != r.type)
-            qmlInfo(0) << QString::fromLatin1("Can't assign to existing role '%1' of different type [%2 -> %3]").arg(r.name).arg(roleTypeName(type)).arg(roleTypeName(r.type));
+            qmlInfo(0) << QStringLiteral("Can't assign to existing role '%1' of different type [%2 -> %3]").arg(r.name).arg(roleTypeName(type)).arg(roleTypeName(r.type));
         return r;
     }
 
@@ -1185,7 +1185,7 @@ int ListElement::setJsProperty(const ListLayout::Role &role, const QV4::Value &d
             }
             roleIndex = setListProperty(role, subModel);
         } else {
-            qmlInfo(0) << QString::fromLatin1("Can't assign to existing role '%1' of different type [%2 -> %3]").arg(role.name).arg(roleTypeName(role.type)).arg(roleTypeName(ListLayout::Role::List));
+            qmlInfo(0) << QStringLiteral("Can't assign to existing role '%1' of different type [%2 -> %3]").arg(role.name).arg(roleTypeName(role.type)).arg(roleTypeName(ListLayout::Role::List));
         }
     } else if (d.isBoolean()) {
         roleIndex = setBoolProperty(role, d.booleanValue());