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! :^)
This commit is contained in:
Andreas Kling 2019-12-14 19:25:33 +01:00
parent 3fd2304dad
commit 4d6968f95d
Notes: sideshowbarker 2024-07-19 10:51:30 +09:00
3 changed files with 20 additions and 4 deletions

View file

@ -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<u64>() - m_first_timestamp;
bool in_kernel = sample.as_object().get("frames").as_array().at(1).as_object().get("address").to_number<u32>() < (8 * MB);
m_sample_data.append({ timestamp, in_kernel });
});
}
Profile::~Profile()

View file

@ -90,6 +90,13 @@ public:
});
}
struct SampleData {
u64 timestamp { 0 };
bool in_kernel { false };
};
const Vector<SampleData>& 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<SampleData> m_sample_data;
bool m_has_timestamp_filter_range { false };
u64 m_timestamp_filter_range_start { 0 };
u64 m_timestamp_filter_range_end { 0 };

View file

@ -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<u64>() - 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<u32>() < (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);