QStringLiteral: be or not to be!



  • Hello fellows,

    I was studying about QStringLiteral and started to replace the literals that I have on my project's code by that macro. Until now, I don't see any performance gain... But I was wondering: What are the real benefits in use it? In which cases QStringLiteral performs better than QString or QLatin1String?

    I found the docs for QStringLiteral a bit vague, read a few posts on Google but I'm still full of doubts, so I'm here to ask for help. Well, let's put some examples:

    1. String initialization
      @
      QString foo = "bar";
      QString foo = QString("bar");
      QString foo = QStringLiteral("bar");
      @

    2. String concatenation
      @
      QString dummy = "somestring";
      QString foo;

    foo = "bar" + dummy;
    foo = QString("bar") + dummy;
    foo = QStringLiteral("bar") + dummy;
    @

    1. Function call with QString parameters

    @
    QString foo = "bar";

    //Let's assume that startsWith() accepts only QString as parameter.
    foo.startsWith("bar");
    foo.startsWith(QString("bar"));
    foo.startsWith(QStringLiteral("bar"));
    foo.startsWith(QLatin1String("bar"));
    @

    Based upon the examples above in which cases I can use QStringLiteral in order to see the real advantages of it?

    Thank you!
    P.S.: Sorry about my poor English! :)
    P.S.1: My project's site: http://pgmodeler.com.br



  • Hi,

    You may find the discussion "here":http://comments.gmane.org/gmane.comp.lib.qt.qt5-feedback/1368 useful. You have propably found it but there is also "this":http://blog.qt.digia.com/blog/2014/06/13/qt-weekly-13-qstringliteral/ blog post.

    I haven't done any benchmarks myself so I don't know how you gain or lose in each scenario. But note that QStringLiteral is about creating QString instances, which are UTF-16 encoded, from string literals encoded differently in the source file. So it essentially pushes string creation to compile time.

    Also note that QtCore developers have already spent a great deal of time to optimize string processing/conversions/etc. So QString is already very fast. I don't think you would notice significant performance changes in your applications unless you have tight and long-running string handling loops where using QStringLiteral could avoid string creation at each iteration. (Reading a large file line by line and processing wouldn't count as an example because file IO is much slower than string processing.)



  • Thank you so much for the clarification.
    I have read both links you mentioned before create this thread.

    bq. But note that QStringLiteral is about creating QString instances, which are UTF-16 encoded, from string literals encoded differently in the source file. So it essentially pushes string creation to compile time.

    OK. Got it. But what I can't understand is why even the docs enforces the usage of the macro instead of QLatin1String or QString ctor like in item 2 line 2. In my point of view the QString(const char*) fits well in the examples above. I may be completely wrong but the little overhead (in my case) does not worth the widespread usage of the verbose macro.

    bq.
    Also note that QtCore developers have already spent a great deal of time to optimize string processing/conversions/etc. So QString is already very fast.

    I believe that too. This is why I'm searching for a consistent answer to explain the advantages of the macro.

    bq. I don’t think you would notice significant performance changes in your applications unless you have tight and long-running string handling loops where using QStringLiteral could avoid string creation at each iteration. (Reading a large file line by line and processing wouldn’t count as an example because file IO is much slower than string processing.)

    That's not the case. My software handles a lot of strings for SQL code generation but the majority of them are const/static ones created at startup time. This can explain why I couldn't see any performance changes.

    Again, thank you for the explanation. I think I'll replace literals by QString ("") just for the sake of readability. :)



  • On a related note, you may want to check out "QStringBuilder":http://doc.qt.io/qt-5/qstring.html#more-efficient-string-construction (mentioned towards the end of the linked section) if you concatenate more than 2 strings.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.