Core::OutputWindow: Prevent UI thread overload

... by splitting up huge amounts of output and feeding it to the
formatter in more digestable chunks, with event processing in between.
This prevents a UI freeze in the case that a single file emits a large
amount of diagnostics and the build tool buffers the output (as ninja
and qbs do), which therefore comes in all at once.
Apart from keeping the UI responsive, this also speeds up execution of
the build step itself, as the remaining output can now be displayed
after it has finished. If another build step is started and there is too
much output pending to flush all at once, we discard the pending output,
in order to prevent the delay to accumulate.

Fixes: QTCREATORBUG-23944
Task-number: QTCREATORBUG-22914
Change-Id: I7cfef939a85bbd13730f607b0f83c36473b0e550
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2020-04-29 13:37:36 +02:00
parent 4b0f326bd9
commit 37ce45c21e
5 changed files with 84 additions and 10 deletions

View File

@@ -32,6 +32,7 @@
#include "icore.h"
#include <utils/outputformatter.h>
#include <utils/qtcassert.h>
#include <QAction>
#include <QCursor>
@@ -45,6 +46,10 @@
#include <QtTest>
#endif
#include <numeric>
const int chunkSize = 10000;
using namespace Utils;
namespace Core {
@@ -68,7 +73,10 @@ public:
IContext *outputWindowContext = nullptr;
QString settingsKey;
OutputFormatter formatter;
QList<QPair<QString, OutputFormat>> queuedOutput;
QTimer queueTimer;
bool flushRequested = false;
bool scrollToBottom = true;
bool linksActive = true;
bool zoomEnabled = false;
@@ -98,6 +106,10 @@ OutputWindow::OutputWindow(Context context, const QString &settingsKey, QWidget
setUndoRedoEnabled(false);
d->formatter.setPlainTextEdit(this);
d->queueTimer.setSingleShot(true);
d->queueTimer.setInterval(10);
connect(&d->queueTimer, &QTimer::timeout, this, &OutputWindow::handleNextOutputChunk);
d->settingsKey = settingsKey;
d->outputWindowContext = new IContext;
@@ -224,6 +236,7 @@ void OutputWindow::keyPressEvent(QKeyEvent *ev)
void OutputWindow::setLineParsers(const QList<OutputLineParser *> &parsers)
{
reset();
d->formatter.setLineParsers(parsers);
}
@@ -370,18 +383,26 @@ void OutputWindow::filterNewContent()
scrollToBottom();
}
void OutputWindow::setMaxCharCount(int count)
void OutputWindow::handleNextOutputChunk()
{
d->maxCharCount = count;
setMaximumBlockCount(count / 100);
QTC_ASSERT(!d->queuedOutput.isEmpty(), return);
auto &chunk = d->queuedOutput.first();
if (chunk.first.size() <= chunkSize) {
handleOutputChunk(chunk.first, chunk.second);
d->queuedOutput.removeFirst();
} else {
handleOutputChunk(chunk.first.left(chunkSize), chunk.second);
chunk.first.remove(0, chunkSize);
}
if (!d->queuedOutput.isEmpty())
d->queueTimer.start();
else if (d->flushRequested) {
d->formatter.flush();
d->flushRequested = false;
}
}
int OutputWindow::maxCharCount() const
{
return d->maxCharCount;
}
void OutputWindow::appendMessage(const QString &output, OutputFormat format)
void OutputWindow::handleOutputChunk(const QString &output, OutputFormat format)
{
QString out = output;
if (out.size() > d->maxCharCount) {
@@ -422,6 +443,27 @@ void OutputWindow::appendMessage(const QString &output, OutputFormat format)
enableUndoRedo();
}
void OutputWindow::setMaxCharCount(int count)
{
d->maxCharCount = count;
setMaximumBlockCount(count / 100);
}
int OutputWindow::maxCharCount() const
{
return d->maxCharCount;
}
void OutputWindow::appendMessage(const QString &output, OutputFormat format)
{
if (d->queuedOutput.isEmpty() || d->queuedOutput.last().second != format)
d->queuedOutput << qMakePair(output, format);
else
d->queuedOutput.last().first.append(output);
if (!d->queueTimer.isActive())
d->queueTimer.start();
}
bool OutputWindow::isScrollbarAtBottom() const
{
return verticalScrollBar()->value() == verticalScrollBar()->maximum();
@@ -461,9 +503,32 @@ void OutputWindow::clear()
void OutputWindow::flush()
{
const int totalQueuedSize = std::accumulate(d->queuedOutput.cbegin(), d->queuedOutput.cend(), 0,
[](int val, const QPair<QString, OutputFormat> &c) { return val + c.first.size(); });
if (totalQueuedSize > 5 * chunkSize) {
d->flushRequested = true;
return;
}
d->queueTimer.stop();
for (const auto &chunk : d->queuedOutput)
handleOutputChunk(chunk.first, chunk.second);
d->queuedOutput.clear();
d->formatter.flush();
}
void OutputWindow::reset()
{
flush();
d->queueTimer.stop();
d->formatter.reset();
if (!d->queuedOutput.isEmpty()) {
d->queuedOutput.clear();
d->formatter.appendMessage(tr("[Discarding excessive amount of pending output.]\n"),
ErrorMessageFormat);
}
d->flushRequested = false;
}
void OutputWindow::scrollToBottom()
{
verticalScrollBar()->setValue(verticalScrollBar()->maximum());

View File

@@ -67,6 +67,7 @@ public:
void grayOutOldContent();
void clear();
void flush();
void reset();
void scrollToBottom();
@@ -109,6 +110,8 @@ private:
QElapsedTimer m_lastMessage;
void enableUndoRedo();
void filterNewContent();
void handleNextOutputChunk();
void handleOutputChunk(const QString &output, Utils::OutputFormat format);
Internal::OutputWindowPrivate *d = nullptr;
};

View File

@@ -703,7 +703,7 @@ void BuildManager::nextStep()
connect(d->m_currentBuildStep, &BuildStep::finished, instance(), finishedHandler);
connect(d->m_currentBuildStep, &BuildStep::progress,
instance(), &BuildManager::progressChanged);
d->m_outputWindow->outputFormatter()->reset();
d->m_outputWindow->reset();
d->m_currentBuildStep->setupOutputFormatter(d->m_outputWindow->outputFormatter());
d->m_currentBuildStep->run();
} else {

View File

@@ -285,6 +285,11 @@ void CompileOutputWindow::flush()
m_outputWindow->flush();
}
void CompileOutputWindow::reset()
{
m_outputWindow->reset();
}
void CompileOutputWindow::setSettings(const CompileOutputSettings &settings)
{
m_settings = settings;

View File

@@ -78,6 +78,7 @@ public:
void showPositionOf(const Task &task);
void flush();
void reset();
const CompileOutputSettings &settings() const { return m_settings; }
void setSettings(const CompileOutputSettings &settings);