From 2be0d3088fc5bc52ece6b28157b28d16699ccb9b Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sun, 18 Jan 2015 14:56:21 +0100 Subject: qFormatLogMessage: optimize %{backtrace} backtrace_symbols is very slow because it tries to find the function name from the address. In order to do that it needs to do a linear search over all symbols. (Because the hash table goes the other way to find the address from the symbol name) The code is going to skip a few frames from QtCore. Since we cannot know how many, we take a few more than necessary. This patch changes the additional number of frames from 15 to 7 (Usually, there are about 5 suppressed frames). We call backtrace_symbols several times for only one frame at the time. So we are not looking up addresses we don't need after we printed the right number of frames. Calling many times backtrace_symbols means we do more malloc, but that's negligible compared to the time we save. We anyway do a lot of other allocations because of the regexp operations and such So this patch is then saving about 10 frames lookups which allow to print about 6 qDebug per miliseconds instead of only 2 when using %{backtrace depth=2} Change-Id: Ic6ece2145d53dc570c80fcb0e4455dcef6bc40cb Reviewed-by: Kai Koehne --- src/corelib/global/qlogging.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 18b672d7ee..5f11334a82 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -1154,13 +1154,13 @@ QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, con message.append(QString::number(qlonglong(QThread::currentThread()->currentThread()), 16)); #ifdef QLOGGING_HAVE_BACKTRACE } else if (token == backtraceTokenC) { - QVarLengthArray buffer(15 + pattern->backtraceDepth); + QVarLengthArray buffer(7 + pattern->backtraceDepth); int n = backtrace(buffer.data(), buffer.size()); if (n > 0) { - QScopedPointer strings(backtrace_symbols(buffer.data(), n)); int numberPrinted = 0; for (int i = 0; i < n && numberPrinted < pattern->backtraceDepth; ++i) { - QString trace = QString::fromLatin1(strings.data()[i]); + QScopedPointer strings(backtrace_symbols(buffer.data() + i, 1)); + QString trace = QString::fromLatin1(strings.data()[0]); // The results of backtrace_symbols looks like this: // /lib/libc.so.6(__libc_start_main+0xf3) [0x4a937413] // The offset and function name are optional. -- cgit v1.2.3