Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-81231

QJSEngine and QStringList properties handling, a potential performance issue

    XMLWordPrintable

Details

    • c7111871a (dev), e91c9a914 (6.8), 8c97fc274 (6.7), 6238876db (tqtc/lts-6.5)

    Description

      Consider:

      #include <QCoreApplication>
      #include <QDebug>
      #include <QQmlApplicationEngine>
      
      class Object : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QStringList strings READ strings CONSTANT)
      
      public:
          QStringList strings() const
          {
              static unsigned callCounter = 0;
              qDebug() << Q_FUNC_INFO << "called" << ++callCounter << "times";
      
              QStringList ret;
              for (int i = 0; i < 10; ++i)
                  ret.append(QString::number(i));
      
              return ret;
          }
      };
      
      int main(int argc, char *argv[])
      {
          QCoreApplication app(argc, argv);
      
          QJSEngine engine;
          engine.installExtensions(QJSEngine::ConsoleExtension);
      
          qmlRegisterUncreatableType<Object>("App", 1, 0, "Object", QStringLiteral("Use 'object'"));
      
          engine.globalObject().setProperty(QStringLiteral("object"), engine.newQObject(new Object));
      
          // Note how the following snippet causes Object::strings to be called
          // 2 times + as many times as there are items in the list.
          engine.evaluate(QStringLiteral("console.log(object.strings)"));
      }
      
      #include "main.moc"
      

      Running this outputs:

      QStringList Object::strings() const called 1 times
      QStringList Object::strings() const called 2 times
      QStringList Object::strings() const called 3 times
      QStringList Object::strings() const called 4 times
      QStringList Object::strings() const called 5 times
      QStringList Object::strings() const called 6 times
      QStringList Object::strings() const called 7 times
      QStringList Object::strings() const called 8 times
      QStringList Object::strings() const called 9 times
      QStringList Object::strings() const called 10 times
      QStringList Object::strings() const called 11 times
      QStringList Object::strings() const called 12 times
      js: [0,1,2,3,4,5,6,7,8,9]
      

      The list is clearly read more often than it needs to be.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              ulherman Ulf Hermann
              ulherman Ulf Hermann
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes