QStringLiteral vs QLatin1String



  • The guidance on this is unclear in Qt documentation. You have Qt docs which says:

    To avoid this one can use the QStringLiteral macro to directly create the required data at compile time. Constructing a QString out of the literal does then not cause any overhead at runtime. A slightly less efficient way is to use QLatin1String.

    So it says QLatin1String is less efficient than QStringLiteral.

    Then Qt blogpost

    Use QStringLiteral only if a QString needs to be created. This sounds trivial, but it’s not necessarily so. Some methods in the Qt API have overloads for either taking a QString, or a QLatin1String object. This is because Latin1 is simpler to parse than UTF-16, and therefore the QLatin1String version can be faster, and use less memory.

    So it says QLatin1String is more efficient than QStringLiteral.

    The differences are in their application and what are you doing, I think - could anyone help spell out the different usecases?


  • Moderators

    • QLatin1String is a thin wrapper for plain, 8-bit C string.
    • QString is a Unicode-aware string. Under the hood, it stores data as 16-bit characters.

    QString is a bit more expensive to construct than QLatin1String because the data is larger ("Hello" takes up 5 bytes in QLatin1String, but 10 bytes in QString). However, QString is much more powerful so it is used everywhere in the Qt API.

    @Vadi2 said in QStringLiteral vs QLatin1String:

    You have Qt docs which says:

    To avoid this one can use the QStringLiteral macro to directly create the required data at compile time. Constructing a QString out of the literal does then not cause any overhead at runtime. A slightly less efficient way is to use QLatin1String.

    So it says QLatin1String is less efficient than QStringLiteral.

    Here, QStringLiteral provides efficiency by constructing the QString at compile-time, instead of run-time.

    If a function takes a QString as an input parameter,

    • Passing in a QStringLiteral eliminates the need for any memory allocations or conversions at runtime.
    • Passing in a QLatin1String requires memory allocation and conversion, to turn your QLatin1String into a QString.

    Then Qt blogpost

    Use QStringLiteral only if a QString needs to be created. This sounds trivial, but it’s not necessarily so. Some methods in the Qt API have overloads for either taking a QString, or a QLatin1String object. This is because Latin1 is simpler to parse than UTF-16, and therefore the QLatin1String version can be faster, and use less memory.

    So it says QLatin1String is more efficient than QStringLiteral.

    Here, QLatin1String provides efficiency because its data is "simpler"

    If you perform string comparison on some English text, you need to compare twice the number of bytes if your string is stored as a QString/QStringLiteral, than if it was stored as a QLatin1String.


    For a super detailed comparison, see https://meetingcpp.com/tl_files/mcpp/2015/talks/Marc-Mutz-MC++15-Effective-Qt.pdf


  • Qt Champions 2016

    There's also QLatin1Literal, which is the same as QStringLiteral but for QLatin1String.



  • @kshegunov said in QStringLiteral vs QLatin1String:

    There's also QLatin1Literal, which is the same as QStringLiteral but for QLatin1String.

    not really, QLatin1Literal still has the same costs as QLatin1String("some text"); as it's the exact same under the hood.

    You can see it right in qstring.h at line 141/142

    // Qt 4.x compatibility
    typedef QLatin1String QLatin1Literal;
    

  • Qt Champions 2016

    Indeed, but it has the same cost as QStringLiteral as data is not copied.


Log in to reply
 

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