From dde75d283b7a3d208eac8c74924f7ed822aa0385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sat, 7 Oct 2023 11:52:21 +0200 Subject: [PATCH] Add a pending frame list to `GlobalFrameView` --- puffin/src/profile_view.rs | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/puffin/src/profile_view.rs b/puffin/src/profile_view.rs index a5e0adc3..915110bf 100644 --- a/puffin/src/profile_view.rs +++ b/puffin/src/profile_view.rs @@ -230,17 +230,41 @@ impl Ord for OrderedByDuration { /// Automatically connects to [`crate::GlobalProfiler`]. pub struct GlobalFrameView { sink_id: FrameSinkId, - view: Arc>, + inner: Arc, +} + +struct GlobalFrameViewInner { + view: Mutex, + pending: Mutex>>, +} + +impl GlobalFrameViewInner { + fn process_pending(&self, view: &mut FrameView) { + let mut guard = self.pending.lock().unwrap(); + for frame in guard.drain(..) { + view.add_frame(frame); + } + } } impl Default for GlobalFrameView { fn default() -> Self { - let view = Arc::new(Mutex::new(FrameView::default())); - let view_clone = view.clone(); + let inner = Arc::new(GlobalFrameViewInner { + view: Mutex::new(FrameView::default()), + pending: Mutex::new(Vec::new()), + }); + let inner_clone = inner.clone(); let sink_id = crate::GlobalProfiler::lock().add_sink(Box::new(move |frame| { - view_clone.lock().unwrap().add_frame(frame); + if let Ok(mut view) = inner_clone.view.try_lock() { + inner_clone.process_pending(&mut *view); + view.add_frame(frame); + } else { + // Something is currently looking at the frame view, add the + // frame to the pending list. + inner_clone.pending.lock().unwrap().push(frame); + } })); - Self { sink_id, view } + Self { sink_id, inner } } } @@ -253,6 +277,8 @@ impl Drop for GlobalFrameView { impl GlobalFrameView { /// View the latest profiling data. pub fn lock(&self) -> std::sync::MutexGuard<'_, FrameView> { - self.view.lock().unwrap() + let mut guard = self.inner.view.lock().unwrap(); + self.inner.process_pending(&mut *guard); + guard } }