Roll our own qUtf8Printable()?

Lubomir I. Ivanov neolit123 at gmail.com
Sun Feb 25 15:00:38 PST 2018


On 25 February 2018 at 21:39, Berthold Stoeger
<bstoeger at mail.tuwien.ac.at> wrote:
> Dear all,
>
> In many places, QStrings are converted to C-style strings with an expression
> of the kind
>  s.toUf8().data()
>
> I have a patch replacing all of them with
>  qUtf8Printable(s)
>
> The idea is that - according to the documentation - this is equivalent to
>  s.toUtf8().constData()
>
> The latter should produce less machine code, because it doesn't have to check
> whether the data of the temporary QByteArray is shared. Owing to the many
> indirections in Qt's code, the compiler is not smart enough to understand that
> in this case data() = constData().
>
> Much to my surprise, qUtf8Printable() produced larger code. The reason is that
> it is defined as a macro in <qglobal.h>:
>
> #ifndef qUtf8Printable
> #  define qUtf8Printable(string) QString(string).toUtf8().constData()
> #endif
>
> It generates a temporary copy of the string! This looks like a defect to me.
>
> So what do you think about rolling our own helper function:
>
> inline const char *qstring2c(const QString &s)
> {
>         return s.toUtf8().constData();
> }
>
> Some numbers (size of the .text segment):
> const char *test(const QString &s) { return qUtf8Printable(s); } -> 267 bytes
> const char *test(const QString &s) { return s.toUtf8().data(); } -> 236 bytes
> const char *test(const QString &s) { return qstring2c(s); }      -> 118 bytes
>
> That's a 50% size reduction per invocation.
>
> Thank you,
>

hi, Thiago.

if you have a moment, what do you think about the size differences
between qUtf8Printable() and the proposed home brew qstring2c()
replacement.
Berhold is saying that qUtf8Printable() always creates a copy of the
string, does that seem right?

thanks
lubomir
--


More information about the subsurface mailing list