From 8c937f45f77149cc5e88d64886546818d8eaf500 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Thu, 23 Sep 2021 22:14:25 -0700 Subject: [PATCH] UI: Cache save size on save buttons. It was calculating the size and date for each permutation, which became monstrously slow on Android with scoped storage. --- UI/SavedataScreen.cpp | 88 +++++++++++++++++++++++++------------------ UI/SavedataScreen.h | 6 +-- 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/UI/SavedataScreen.cpp b/UI/SavedataScreen.cpp index 2e698c8774..be06271797 100644 --- a/UI/SavedataScreen.cpp +++ b/UI/SavedataScreen.cpp @@ -138,7 +138,7 @@ private: class SortedLinearLayout : public UI::LinearLayoutList { public: - typedef std::function CompareFunc; + typedef std::function CompareFunc; typedef std::function DoneFunc; SortedLinearLayout(UI::Orientation orientation, UI::LayoutParams *layoutParams = nullptr) @@ -184,14 +184,52 @@ public: const Path &GamePath() const { return savePath_; } + uint64_t GetTotalSize(); + int64_t GetDateSeconds(); + private: void UpdateText(const std::shared_ptr &ginfo); Path savePath_; std::string title_; std::string subtitle_; + uint64_t totalSize_ = 0; + int64_t dateSeconds_ = 0; + bool hasTotalSize_ = false; + bool hasDateSeconds_ = false; }; +uint64_t SavedataButton::GetTotalSize() { + if (hasTotalSize_) + return totalSize_; + + File::FileInfo info; + if (File::GetFileInfo(savePath_, &info)) { + totalSize_ = info.size; + if (info.isDirectory) + totalSize_ = File::ComputeRecursiveDirectorySize(savePath_); + } + + hasTotalSize_ = true; + return totalSize_; +} + +int64_t SavedataButton::GetDateSeconds() { + if (hasDateSeconds_) + return dateSeconds_; + + File::FileInfo info; + if (File::GetFileInfo(savePath_, &info)) { + dateSeconds_ = info.mtime; + if (info.isDirectory && File::GetFileInfo(savePath_ / "PARAM.SFO", &info)) { + dateSeconds_ = info.mtime; + } + } + + hasDateSeconds_ = true; + return dateSeconds_; +} + UI::EventReturn SavedataPopupScreen::OnDeleteButtonClick(UI::EventParams &e) { std::shared_ptr ginfo = g_gameInfoCache->GetInfo(nullptr, savePath_, GAMEINFO_WANTSIZE); ginfo->Delete(); @@ -417,28 +455,18 @@ void SavedataBrowser::SetSortOption(SavedataSortOption opt) { } } -bool SavedataBrowser::ByFilename(const UI::View *v1, const UI::View *v2) { - const SavedataButton *b1 = static_cast(v1); - const SavedataButton *b2 = static_cast(v2); +bool SavedataBrowser::ByFilename(UI::View *v1, UI::View *v2) { + SavedataButton *b1 = static_cast(v1); + SavedataButton *b2 = static_cast(v2); return strcmp(b1->GamePath().c_str(), b2->GamePath().c_str()) < 0; } -static uint64_t GetTotalSize(const SavedataButton *b) { - File::FileInfo info; - if (File::GetFileInfo(b->GamePath(), &info)) { - if (info.isDirectory) - return File::ComputeRecursiveDirectorySize(b->GamePath()); - return info.size; - } - return 0; -} - -bool SavedataBrowser::BySize(const UI::View *v1, const UI::View *v2) { - const SavedataButton *b1 = static_cast(v1); - const SavedataButton *b2 = static_cast(v2); - const uint64_t size1 = GetTotalSize(b1); - const uint64_t size2 = GetTotalSize(b2); +bool SavedataBrowser::BySize(UI::View *v1, UI::View *v2) { + SavedataButton *b1 = static_cast(v1); + SavedataButton *b2 = static_cast(v2); + const uint64_t size1 = b1->GetTotalSize(); + const uint64_t size2 = b2->GetTotalSize(); if (size1 > size2) return true; @@ -447,23 +475,11 @@ bool SavedataBrowser::BySize(const UI::View *v1, const UI::View *v2) { return strcmp(b1->GamePath().c_str(), b2->GamePath().c_str()) < 0; } -static int64_t GetDateSeconds(const SavedataButton *b) { - File::FileInfo info; - if (File::GetFileInfo(b->GamePath(), &info)) { - if (info.isDirectory && File::GetFileInfo(b->GamePath() / "PARAM.SFO", &info)) { - return info.mtime; - } - return info.mtime; - } - - return 0; -} - -bool SavedataBrowser::ByDate(const UI::View *v1, const UI::View *v2) { - const SavedataButton *b1 = static_cast(v1); - const SavedataButton *b2 = static_cast(v2); - const int64_t time1 = GetDateSeconds(b1); - const int64_t time2 = GetDateSeconds(b2); +bool SavedataBrowser::ByDate(UI::View *v1, UI::View *v2) { + SavedataButton *b1 = static_cast(v1); + SavedataButton *b2 = static_cast(v2); + const int64_t time1 = b1->GetDateSeconds(); + const int64_t time2 = b2->GetDateSeconds(); if (time1 > time2) return true; diff --git a/UI/SavedataScreen.h b/UI/SavedataScreen.h index b9d057d025..14ea48a960 100644 --- a/UI/SavedataScreen.h +++ b/UI/SavedataScreen.h @@ -46,9 +46,9 @@ public: UI::Event OnChoice; private: - static bool ByFilename(const UI::View *, const UI::View *); - static bool BySize(const UI::View *, const UI::View *); - static bool ByDate(const UI::View *, const UI::View *); + static bool ByFilename(UI::View *, UI::View *); + static bool BySize(UI::View *, UI::View *); + static bool ByDate(UI::View *, UI::View *); static bool SortDone(); void Refresh();