From 4d6968f95dc15b81e7a2138283c7fc133e00ed71 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 14 Dec 2019 19:25:33 +0100 Subject: [PATCH] ProfileViewer: Precompute the timestamp and "in kernel?" per sample Instead of fetching these from JSON in every paint event, we now have a separate "SampleData" vector that can be iterated. This optimization was made possible by profiling ProfileViewer and then analyzing the profile with ProfileViewer! :^) --- DevTools/ProfileViewer/Profile.cpp | 7 +++++++ DevTools/ProfileViewer/Profile.h | 9 +++++++++ DevTools/ProfileViewer/ProfileTimelineWidget.cpp | 8 ++++---- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/DevTools/ProfileViewer/Profile.cpp b/DevTools/ProfileViewer/Profile.cpp index cbd0caf683..19d82c12d5 100644 --- a/DevTools/ProfileViewer/Profile.cpp +++ b/DevTools/ProfileViewer/Profile.cpp @@ -12,6 +12,13 @@ Profile::Profile(const JsonArray& json) m_model = ProfileModel::create(*this); rebuild_tree(); + + m_sample_data.ensure_capacity(m_json.size()); + m_json.for_each([&](const JsonValue& sample) { + u64 timestamp = sample.as_object().get("timestamp").to_number() - m_first_timestamp; + bool in_kernel = sample.as_object().get("frames").as_array().at(1).as_object().get("address").to_number() < (8 * MB); + m_sample_data.append({ timestamp, in_kernel }); + }); } Profile::~Profile() diff --git a/DevTools/ProfileViewer/Profile.h b/DevTools/ProfileViewer/Profile.h index 9305fc4d35..8c759f58ab 100644 --- a/DevTools/ProfileViewer/Profile.h +++ b/DevTools/ProfileViewer/Profile.h @@ -90,6 +90,13 @@ public: }); } + struct SampleData { + u64 timestamp { 0 }; + bool in_kernel { false }; + }; + + const Vector& sample_data() const { return m_sample_data; } + u64 length_in_ms() const { return m_last_timestamp - m_first_timestamp; } u64 first_timestamp() const { return m_first_timestamp; } u64 last_timestamp() const { return m_first_timestamp; } @@ -109,6 +116,8 @@ private: u64 m_first_timestamp { 0 }; u64 m_last_timestamp { 0 }; + Vector m_sample_data; + bool m_has_timestamp_filter_range { false }; u64 m_timestamp_filter_range_start { 0 }; u64 m_timestamp_filter_range_end { 0 }; diff --git a/DevTools/ProfileViewer/ProfileTimelineWidget.cpp b/DevTools/ProfileViewer/ProfileTimelineWidget.cpp index e841cb95cc..cd4eb2dab9 100644 --- a/DevTools/ProfileViewer/ProfileTimelineWidget.cpp +++ b/DevTools/ProfileViewer/ProfileTimelineWidget.cpp @@ -28,16 +28,16 @@ void ProfileTimelineWidget::paint_event(GPaintEvent& event) float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms(); - m_profile.for_each_sample([&](const JsonObject& sample) { - u64 t = sample.get("timestamp").to_number() - m_profile.first_timestamp(); + for (auto& sample : m_profile.sample_data()) { + u64 t = sample.timestamp; int x = (int)((float)t * column_width); int cw = max(1, (int)column_width); - bool in_kernel = sample.get("frames").as_array().at(1).as_object().get("address").to_number() < (8 * MB); + bool in_kernel = sample.in_kernel; Color color = in_kernel ? Color::from_rgb(0xc25e5a) : Color::from_rgb(0x5a65c2); for (int i = 0; i < cw; ++i) painter.draw_line({ x + i, frame_thickness() }, { x + i, height() - frame_thickness() * 2 }, color); - }); + } u64 normalized_start_time = min(m_select_start_time, m_select_end_time); u64 normalized_end_time = max(m_select_start_time, m_select_end_time);