From 6368ef41f871ba4f0bc41aa77149b9c1619e5e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Tue, 26 Apr 2022 22:56:57 +0200 Subject: [PATCH] Profiler: Round sample percentages to a constant number of digits This constant is currently 3 but can be changed easily or integrated into a user setting. Note that the results are not ideal because during pretty-printing we're not using any nice rounding rules, so many percentage values will actually appear as 0.399999 even though they were rounded to three digits. --- Userland/DevTools/Profiler/ProfileModel.cpp | 9 +++++++-- Userland/DevTools/Profiler/ProfileModel.h | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Userland/DevTools/Profiler/ProfileModel.cpp b/Userland/DevTools/Profiler/ProfileModel.cpp index 9adf82e1cf3..23cc815fbfb 100644 --- a/Userland/DevTools/Profiler/ProfileModel.cpp +++ b/Userland/DevTools/Profiler/ProfileModel.cpp @@ -111,14 +111,19 @@ GUI::Variant ProfileModel::data(GUI::ModelIndex const& index, GUI::ModelRole rol return {}; } if (role == GUI::ModelRole::Display) { + auto round_percentages = [this](auto percentage) { + return roundf(static_cast(percentage) / static_cast(m_profile.filtered_event_indices().size()) + * percent_digits_rounding_constant) + * 100.0f / percent_digits_rounding_constant; + }; if (index.column() == Column::SampleCount) { if (m_profile.show_percentages()) - return ((float)node->event_count() / (float)m_profile.filtered_event_indices().size()) * 100.0f; + return round_percentages(node->event_count()); return node->event_count(); } if (index.column() == Column::SelfCount) { if (m_profile.show_percentages()) - return ((float)node->self_count() / (float)m_profile.filtered_event_indices().size()) * 100.0f; + return round_percentages(node->self_count()); return node->self_count(); } if (index.column() == Column::ObjectName) diff --git a/Userland/DevTools/Profiler/ProfileModel.h b/Userland/DevTools/Profiler/ProfileModel.h index 11108bdead4..7e785460672 100644 --- a/Userland/DevTools/Profiler/ProfileModel.h +++ b/Userland/DevTools/Profiler/ProfileModel.h @@ -13,6 +13,10 @@ namespace Profiler { class Profile; +// Number of digits after the decimal point for sample percentages. +static constexpr int const number_of_percent_digits = 3; +static constexpr float const percent_digits_rounding_constant = AK::pow(10, number_of_percent_digits); + class ProfileModel final : public GUI::Model { public: static NonnullRefPtr create(Profile& profile)