From d62e4da163dbea6bf6a6066fe00b66041f092dcf Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 4 Dec 2025 06:51:08 +0300 Subject: [PATCH] Added simple cache of recognized text to media view overlay. --- .../media/view/media_view_overlay_widget.cpp | 66 ++++++++++++++----- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index ad5e434836..74212b58ba 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -115,6 +115,24 @@ namespace Media { namespace View { namespace { +struct RecognitionId { + uint64 sessionUniqueId = 0; + PhotoId photoId = 0; + DocumentId documentId = 0; + + friend inline auto operator<=>(RecognitionId, RecognitionId) = default; +}; + +using RecognitionResult = Platform::TextRecognition::Result; +using RecognitionCacheMap = base::flat_map; + +[[nodiscard]] RecognitionCacheMap *RecognitionCache() { + static auto cache = Platform::TextRecognition::IsAvailable() + ? std::make_unique>() + : nullptr; + return cache.get(); +} + constexpr auto kPreloadCount = 3; constexpr auto kMaxZoomLevel = 7; // x8 constexpr auto kZoomToScreenLevel = 1024; @@ -3751,23 +3769,39 @@ void OverlayWidget::displayPhoto( } if (!_stories && Platform::TextRecognition::IsAvailable()) { - const auto weak = base::make_weak(_widget); - const auto photoMedia = _photoMedia; - crl::async([=] { - const auto image = photoMedia->image(Data::PhotoSize::Large); - if (!image) { - return; - } - const auto qimage = image->original(); - if (qimage.isNull()) { - return; - } - auto result = Platform::TextRecognition::RecognizeText(qimage); - crl::on_main(weak, [=, result = std::move(result)]() mutable { - _recognitionResult = std::move(result); - updateControls(); + const auto cache = RecognitionCache(); + const auto id = RecognitionId{ + .sessionUniqueId = _session->uniqueId(), + .photoId = _photo->id, + }; + if (const auto cached = cache->find(id) + ; cached != cache->end()) { + _recognitionResult = cached->second; + updateControls(); + } else { + const auto weak = base::make_weak(_widget); + const auto photoMedia = _photoMedia; + crl::async([=] { + const auto image = photoMedia->image( + Data::PhotoSize::Large); + if (!image) { + return; + } + const auto original = image->original(); + if (original.isNull()) { + return; + } + auto result = Platform::TextRecognition::RecognizeText( + original); + crl::on_main(weak, [=, result = std::move(result)]() mutable { + const auto cache = RecognitionCache(); + _recognitionResult = std::move(result); + auto &cached = (*cache)[id]; + cached = _recognitionResult; + updateControls(); + }); }); - }); + } } initSponsoredButton();