From d672e75109a2265b97d89b23c6aee47addffc2a8 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Sat, 3 May 2014 16:46:17 -0700 Subject: [PATCH] Filter out redundant progress updates sooner Sending events from background threads to the main thread is somewhat expensive, so filter out progress updates which don't actually change the progress on the background thread. --- src/dialog_progress.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/dialog_progress.cpp b/src/dialog_progress.cpp index 669b47d5b..50172c86b 100644 --- a/src/dialog_progress.cpp +++ b/src/dialog_progress.cpp @@ -72,14 +72,11 @@ namespace { class DialogProgressSink final : public agi::ProgressSink { DialogProgress *dialog; - std::atomic cancelled; + std::atomic cancelled{false}; + int progress = 0; public: - DialogProgressSink(DialogProgress *dialog) - : dialog(dialog) - , cancelled(false) - { - } + DialogProgressSink(DialogProgress *dialog) : dialog(dialog) { } void SetTitle(std::string const& title) override { Main().Async([=]{ dialog->title->SetLabelText(to_wx(title)); }); @@ -90,7 +87,11 @@ public: } void SetProgress(int64_t cur, int64_t max) override { - Main().Async([=]{ dialog->SetProgress(mid(0, double(cur) / max * 300, 300)); }); + int new_progress = mid(0, double(cur) / max * 300, 300); + if (new_progress != progress) { + progress = new_progress; + Main().Async([=]{ dialog->SetProgress(new_progress); }); + } } void Log(std::string const& str) override {