[Development] A faster qUtf8Printable for static trace points?

Milian Wolff milian.wolff at kdab.com
Mon Sep 5 21:20:12 CEST 2016


Hey Thiago, others.

I plan to work on adding static trace points to Qt in the next months, i.e. 
find an abstraction that works with DTrace/SystemTap UDST/xperf as well as 
custom trace point APIs that are sometimes used in the automotive sector e.g.

One thing that I noticed already is that often the existing consumers for 
trace files expect UTF8 string parameters for tracepoints. Now in Qt we 
usually deal with stuff like:

bool QLibraryPrivate::load()
{
    ...
    TraceScope<...> ts(qUtf8Printable(fileName))
    ...
}

Trace points have nearly zero runtime overhead when not used [1]. But the 
above has a super high cost due to calling QString::toUtf8, which constructs a 
temporary QByteArray and thus incurs a temporary runtime allocation. An 
alternative would be to use qUtf16Printable, which would be fast, but I fear 
it makes interoperability with existing trace point consumers hard.

Can someone with experience on trace points confirm this assertion? If that is 
untrue, simply using qUtf16Printable above would make everything work, and be 
fast.

One way to handle the above case would be only doing the qUtf8Printable call 
when the trace point is enabled, but afaik that checking also is not done for 
free, and also makes the consumer code harder to write.

I thought, what about improving qUtf8Printable instead. I.e. something along 
the lines of:

https://github.com/milianw/bench_qt/blob/master/bench_qstring/
bench_qstring.cpp#L118

Would something like this be acceptable for upstream? E.g. change 
qUtf8Printable to not allocate memory for up to PATH_MAX chars or similar, and 
otherwise fallback to the slow QByteArray (or simply truncate, which would be 
an option for trace points actually).

From my POV, the way qPrintable & friends are used right now, the above would 
be "safe". And it's much faster:

RESULT : BenchQString::benchQUtf8Printable():
     0.00014 msecs per iteration (total: 74, iterations: 524288)
RESULT : BenchQString::benchQFastUtf8Printable_QUtf8Functions():
     0.000049 msecs per iteration (total: 52, iterations: 1048576)

RESULT : BenchQString::benchQUtf8Printable():
     450.764625 CPU cycles per iteration (total: 450,764,625, iterations: 
1000000)
RESULT : BenchQString::benchQFastUtf8Printable_QUtf8Functions():
     161.244798 CPU cycles per iteration (total: 161,244,798, iterations: 
1000000)

RESULT : BenchQString::benchQUtf8Printable():
     1,023.400176 instructions per iteration (total: 1,023,400,177, 
iterations: 1000000)
RESULT : BenchQString::benchQFastUtf8Printable_QUtf8Functions():
     432.060962 instructions per iteration (total: 432,060,962, iterations: 
1000000)

Thanks, any input welcome.

[1]: one nop - see http://tromey.com/blog/?p=687

-- 
Milian Wolff | milian.wolff at kdab.com | Software Engineer
KDAB (Deutschland) GmbH&Co KG, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt Experts
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5903 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20160905/bba8e06d/attachment.bin>


More information about the Development mailing list