App: Use more local methods to trigger memory trim timer

The global event filter function is executed often, for events
originating deep in the widget hierarchy even multiple times.

Triggering the timer only by mouse and key presses on the main
application and on texteditor is less intrusive and still happens
often enough (TM).

Change-Id: I848d55a347bd62d12e8523965d1750c59da33116
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2022-08-02 11:04:10 +02:00
parent 9177649cab
commit df4155f617
6 changed files with 45 additions and 31 deletions

View File

@@ -84,10 +84,6 @@
#include "client/settings.h" #include "client/settings.h"
#endif #endif
#ifdef Q_OS_LINUX
#include <malloc.h>
#endif
using namespace ExtensionSystem; using namespace ExtensionSystem;
enum { OptionIndent = 4, DescriptionIndent = 34 }; enum { OptionIndent = 4, DescriptionIndent = 34 };
@@ -757,32 +753,5 @@ int main(int argc, char **argv)
// shutdown plugin manager on the exit // shutdown plugin manager on the exit
QObject::connect(&app, &QCoreApplication::aboutToQuit, &pluginManager, &PluginManager::shutdown); QObject::connect(&app, &QCoreApplication::aboutToQuit, &pluginManager, &PluginManager::shutdown);
#ifdef Q_OS_LINUX
class MemoryTrimmer : public QObject
{
public:
MemoryTrimmer()
{
m_trimTimer.setSingleShot(true);
m_trimTimer.setInterval(60000);
// glibc may not actually free memory in free().
connect(&m_trimTimer, &QTimer::timeout, this, [] { malloc_trim(0); });
}
bool eventFilter(QObject *, QEvent *e) override
{
if ((e->type() == QEvent::MouseButtonPress || e->type() == QEvent::KeyPress)
&& !m_trimTimer.isActive()) {
m_trimTimer.start();
}
return false;
}
QTimer m_trimTimer;
};
MemoryTrimmer trimmer;
app.installEventFilter(&trimmer);
#endif
return restarter.restartOrExit(app.exec()); return restarter.restartOrExit(app.exec());
} }

View File

@@ -832,6 +832,11 @@ void ICore::registerWindow(QWidget *window, const Context &context)
new WindowSupport(window, context); // deletes itself when widget is destroyed new WindowSupport(window, context); // deletes itself when widget is destroyed
} }
void ICore::restartTrimmer()
{
m_mainwindow->restartTrimmer();
}
/*! /*!
Opens files using \a filePaths and \a flags like it would be Opens files using \a filePaths and \a flags like it would be
done if they were given to \QC on the command line, or done if they were given to \QC on the command line, or

View File

@@ -124,6 +124,7 @@ public:
static void removeContextObject(IContext *context); static void removeContextObject(IContext *context);
static void registerWindow(QWidget *window, const Context &context); static void registerWindow(QWidget *window, const Context &context);
static void restartTrimmer();
enum OpenFilesFlags { enum OpenFilesFlags {
None = 0, None = 0,

View File

@@ -113,6 +113,10 @@
#include <QVersionNumber> #include <QVersionNumber>
#include <QWindow> #include <QWindow>
#ifdef Q_OS_LINUX
#include <malloc.h>
#endif
using namespace ExtensionSystem; using namespace ExtensionSystem;
using namespace Utils; using namespace Utils;
@@ -228,6 +232,13 @@ MainWindow::MainWindow()
}); });
connect(dropSupport, &DropSupport::filesDropped, connect(dropSupport, &DropSupport::filesDropped,
this, &MainWindow::openDroppedFiles); this, &MainWindow::openDroppedFiles);
#ifdef Q_OS_LINUX
m_trimTimer.setSingleShot(true);
m_trimTimer.setInterval(60000);
// glibc may not actually free memory in free().
connect(&m_trimTimer, &QTimer::timeout, this, [] { malloc_trim(0); });
#endif
} }
NavigationWidget *MainWindow::navigationWidget(Side side) const NavigationWidget *MainWindow::navigationWidget(Side side) const
@@ -367,6 +378,12 @@ void MainWindow::restart()
exit(); exit();
} }
void MainWindow::restartTrimmer()
{
if (!m_trimTimer.isActive())
m_trimTimer.start();
}
void MainWindow::closeEvent(QCloseEvent *event) void MainWindow::closeEvent(QCloseEvent *event)
{ {
const auto cancelClose = [event] { const auto cancelClose = [event] {
@@ -419,6 +436,18 @@ void MainWindow::closeEvent(QCloseEvent *event)
alreadyClosed = true; alreadyClosed = true;
} }
void MainWindow::keyPressEvent(QKeyEvent *event)
{
restartTrimmer();
AppMainWindow::keyPressEvent(event);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
restartTrimmer();
AppMainWindow::mousePressEvent(event);
}
void MainWindow::openDroppedFiles(const QList<DropSupport::FileSpec> &files) void MainWindow::openDroppedFiles(const QList<DropSupport::FileSpec> &files)
{ {
raiseWindow(); raiseWindow();

View File

@@ -32,6 +32,7 @@
#include <utils/dropsupport.h> #include <utils/dropsupport.h>
#include <QColor> #include <QColor>
#include <QTimer>
#include <functional> #include <functional>
#include <unordered_map> #include <unordered_map>
@@ -118,12 +119,16 @@ public:
void openFileFromDevice(); void openFileFromDevice();
void restartTrimmer();
public slots: public slots:
static void openFileWith(); static void openFileWith();
void exit(); void exit();
protected: protected:
void closeEvent(QCloseEvent *event) override; void closeEvent(QCloseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
private: private:
static void openFile(); static void openFile();
@@ -153,6 +158,7 @@ private:
void updateModeSelectorStyleMenu(); void updateModeSelectorStyleMenu();
ICore *m_coreImpl = nullptr; ICore *m_coreImpl = nullptr;
QTimer m_trimTimer;
QStringList m_aboutInformation; QStringList m_aboutInformation;
Context m_highPrioAdditionalContexts; Context m_highPrioAdditionalContexts;
Context m_lowPrioAdditionalContexts; Context m_lowPrioAdditionalContexts;

View File

@@ -2372,6 +2372,8 @@ static inline bool isPrintableText(const QString &text)
void TextEditorWidget::keyPressEvent(QKeyEvent *e) void TextEditorWidget::keyPressEvent(QKeyEvent *e)
{ {
ICore::restartTrimmer();
ExecuteOnDestruction eod([&]() { d->clearBlockSelection(); }); ExecuteOnDestruction eod([&]() { d->clearBlockSelection(); });
if (!isModifier(e) && mouseHidingEnabled()) if (!isModifier(e) && mouseHidingEnabled())
@@ -5460,6 +5462,8 @@ static bool handleForwardBackwardMouseButtons(QMouseEvent *e)
void TextEditorWidget::mousePressEvent(QMouseEvent *e) void TextEditorWidget::mousePressEvent(QMouseEvent *e)
{ {
ICore::restartTrimmer();
if (e->button() == Qt::LeftButton) { if (e->button() == Qt::LeftButton) {
MultiTextCursor multiCursor = multiTextCursor(); MultiTextCursor multiCursor = multiTextCursor();
const QTextCursor &cursor = cursorForPosition(e->pos()); const QTextCursor &cursor = cursorForPosition(e->pos());