[Development] QStringLiteral vs QLatin1String , foreach vs for range

Thiago Macieira thiago.macieira at intel.com
Sun Jan 17 20:41:29 CET 2016

On Sunday 17 January 2016 19:31:54 Olivier Goffart wrote:
> As explained here: https://woboq.com/blog/qstringliteral.html
> QStringLiteral can avoid malloc and conversion to QString. But there is an 
> operator==(const QString&,QLatin1String) which also don't do allocations or 
> conversion.  In that case QLatin1String is slightly better because it takes
> twice as less space in the binary (UTF-16 vs. ASCII)

Technically, it's "less than half the space".

For any string of N Latin-1 characters, QLatin1String occupies N+1 bytes and 
QStringLiteral occupies 2*(N+1)+24. 

And this is assuming the compiler didn't do common tail merging of strings 
(most don't). If that happens, then the size of two QLatin1Strings of M and N 
characters could be less than M+N+2.

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

More information about the Development mailing list