From 3aa7f4dd626ebfcd78d2f0432862e0b2352b05b5 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 6 Oct 2022 17:05:50 +0400 Subject: [PATCH] Test custom emoji timer-by-on_main optimization. --- .../data/stickers/data_custom_emoji.cpp | 43 +++++++++++++++++-- .../data/stickers/data_custom_emoji.h | 6 +++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp b/Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp index e5023c4ff5..4dc8fbb6f8 100644 --- a/Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp +++ b/Telegram/SourceFiles/data/stickers/data_custom_emoji.cpp @@ -33,6 +33,9 @@ namespace Data { namespace { constexpr auto kMaxPerRequest = 100; +#if 0 // inject-to-on_main +constexpr auto kUnsubscribeUpdatesDelay = 3 * crl::time(1000); +#endif using SizeTag = CustomEmojiManager::SizeTag; @@ -730,15 +733,40 @@ void CustomEmojiManager::repaintLater( } } bunch.when = request.when; +#if 0 // inject-to-on_main + _repaintsLastAdded = request.when; +#endif } bunch.instances.emplace_back(instance); scheduleRepaintTimer(); } +bool CustomEmojiManager::checkEmptyRepaints() { + if (!_repaints.empty()) { + return false; +#if 0 // inject-to-on_main + } else if (_repaintsLifetime + && crl::now() >= _repaintsLastAdded + kUnsubscribeUpdatesDelay) { + _repaintsLifetime.destroy(); +#endif + } + return true; +} + void CustomEmojiManager::scheduleRepaintTimer() { - if (_repaintTimerScheduled) { + if (checkEmptyRepaints() || _repaintTimerScheduled) { return; } + +#if 0 // inject-to-on_main + if (!_repaintsLifetime) { + crl::on_main_update_requests( + ) | rpl::start_with_next([=] { + invokeRepaints(); + }, _repaintsLifetime); + } +#endif + _repaintTimerScheduled = true; Ui::PostponeCall(this, [=] { _repaintTimerScheduled = false; @@ -765,6 +793,9 @@ void CustomEmojiManager::scheduleRepaintTimer() { void CustomEmojiManager::invokeRepaints() { _repaintNext = 0; + if (checkEmptyRepaints()) { + return; + } const auto now = crl::now(); auto repaint = std::vector>(); for (auto i = begin(_repaints); i != end(_repaints);) { @@ -783,10 +814,14 @@ void CustomEmojiManager::invokeRepaints() { } i = _repaints.erase(i); } - for (const auto &weak : repaint) { - if (const auto strong = weak.get()) { - strong->repaint(); + if (!repaint.empty()) { + for (const auto &weak : repaint) { + if (const auto strong = weak.get()) { + strong->repaint(); + } } + } else if (_repaintTimer.isActive()) { + return; } scheduleRepaintTimer(); } diff --git a/Telegram/SourceFiles/data/stickers/data_custom_emoji.h b/Telegram/SourceFiles/data/stickers/data_custom_emoji.h index a4a973c96d..5cd8de9020 100644 --- a/Telegram/SourceFiles/data/stickers/data_custom_emoji.h +++ b/Telegram/SourceFiles/data/stickers/data_custom_emoji.h @@ -108,6 +108,7 @@ private: not_null instance, Ui::CustomEmoji::RepaintRequest request); void scheduleRepaintTimer(); + bool checkEmptyRepaints(); void invokeRepaints(); void fillColoredFlags(not_null document); void processLoaders(not_null document); @@ -161,6 +162,11 @@ private: bool _repaintTimerScheduled = false; bool _requestSetsScheduled = false; +#if 0 // inject-to-on_main + crl::time _repaintsLastAdded = 0; + rpl::lifetime _repaintsLifetime; +#endif + rpl::lifetime _lifetime; };