From 08e508e846d04652a9e3bd12872b9fef1dee4712 Mon Sep 17 00:00:00 2001 From: Tobias Date: Sat, 29 Aug 2020 17:29:20 +0200 Subject: [PATCH] Port yuzu-emu/yuzu#4472: "perf_stats: Mark GetMeanFrametime() as const" (#5498) The general pattern is to mark mutexes as mutable when it comes to matters of constness, given the mutex acts as a transient member of a data structure. Co-Authored-By: LC --- src/core/perf_stats.cpp | 9 +++++---- src/core/perf_stats.h | 11 +++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/core/perf_stats.cpp b/src/core/perf_stats.cpp index 107b1c372..e5b01f086 100644 --- a/src/core/perf_stats.cpp +++ b/src/core/perf_stats.cpp @@ -73,15 +73,16 @@ void PerfStats::EndGameFrame() { game_frames += 1; } -double PerfStats::GetMeanFrametime() { +double PerfStats::GetMeanFrametime() const { std::lock_guard lock{object_mutex}; if (current_index <= IgnoreFrames) { return 0; } + const double sum = std::accumulate(perf_history.begin() + IgnoreFrames, - perf_history.begin() + current_index, 0); - return sum / (current_index - IgnoreFrames); + perf_history.begin() + current_index, 0.0); + return sum / static_cast(current_index - IgnoreFrames); } PerfStats::Results PerfStats::GetAndResetStats(microseconds current_system_time_us) { @@ -110,7 +111,7 @@ PerfStats::Results PerfStats::GetAndResetStats(microseconds current_system_time_ return results; } -double PerfStats::GetLastFrameTimeScale() { +double PerfStats::GetLastFrameTimeScale() const { std::lock_guard lock{object_mutex}; constexpr double FRAME_LENGTH = 1.0 / GPU::SCREEN_REFRESH_RATE; diff --git a/src/core/perf_stats.h b/src/core/perf_stats.h index c7d22ef8e..9038e4ca2 100644 --- a/src/core/perf_stats.h +++ b/src/core/perf_stats.h @@ -21,7 +21,6 @@ namespace Core { class PerfStats { public: explicit PerfStats(u64 title_id); - ~PerfStats(); using Clock = std::chrono::high_resolution_clock; @@ -44,18 +43,18 @@ public: Results GetAndResetStats(std::chrono::microseconds current_system_time_us); /** - * Returns the Arthimetic Mean of all frametime values stored in the performance history. + * Returns the arithmetic mean of all frametime values stored in the performance history. */ - double GetMeanFrametime(); + double GetMeanFrametime() const; /** * Gets the ratio between walltime and the emulated time of the previous system frame. This is * useful for scaling inputs or outputs moving between the two time domains. */ - double GetLastFrameTimeScale(); + double GetLastFrameTimeScale() const; private: - std::mutex object_mutex{}; + mutable std::mutex object_mutex; /// Title ID for the game that is running. 0 if there is no game running yet u64 title_id{0}; @@ -63,7 +62,7 @@ private: std::size_t current_index{0}; /// Stores an hour of historical frametime data useful for processing and tracking performance /// regressions with code changes. - std::array perf_history = {}; + std::array perf_history{}; /// Point when the cumulative counters were reset Clock::time_point reset_point = Clock::now();