Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Stop sealing empty intervals on idle frame skips (#65057)

Anthony Eid36b6d0951fde
diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs
index 2416dee27fb..8a103008253 100644
--- a/crates/gpui/src/platform.rs
+++ b/crates/gpui/src/platform.rs
@@ -800,7 +800,6 @@ pub struct A11yCallbacks {
/// The source of a platform frame request's timestamp.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
-#[repr(u8)]
pub enum FrameRequestSource {
/// An OS callback or compositor-paced frame request.
#[default]
@@ -872,6 +871,7 @@ impl PlatformFrameSignal {
/// Records a platform frame request, retaining the first undrained timestamp.
#[inline]
+ #[cfg_attr(not(feature = "profiler"), expect(unused_variables))]
pub fn record(&self, at: Instant, source: FrameRequestSource) {
#[cfg(feature = "profiler")]
{
@@ -888,8 +888,6 @@ impl PlatformFrameSignal {
std::sync::atomic::Ordering::Relaxed,
);
}
- #[cfg(not(feature = "profiler"))]
- let _ = (at, source);
}
/// Drains the first platform frame request time and source, leaving the accumulator empty.
diff --git a/crates/gpui/src/profiler/hang.rs b/crates/gpui/src/profiler/hang.rs
index c63b31b917f..fbd52341e30 100644
--- a/crates/gpui/src/profiler/hang.rs
+++ b/crates/gpui/src/profiler/hang.rs
@@ -576,11 +576,10 @@ mod tests {
reason: FrameSkipReason::NoRenderNeeded,
})),
]);
- let [snapshot] = snapshots.as_slice() else {
- panic!("expected one snapshot, got {snapshots:?}");
- };
- assert_eq!(snapshot.occupancy(), Duration::ZERO);
- assert!(HangIncident::detect(snapshot.clone(), HANG_THRESHOLD, FRAME_BUDGET).is_none());
+ assert!(
+ snapshots.is_empty(),
+ "a late signal without work seals nothing, got {snapshots:?}"
+ );
}
/// A hang that outlives the frame deadline stays in one incident with
diff --git a/crates/gpui/src/profiler/journal.rs b/crates/gpui/src/profiler/journal.rs
index f8ae07c3138..fba75ae77ca 100644
--- a/crates/gpui/src/profiler/journal.rs
+++ b/crates/gpui/src/profiler/journal.rs
@@ -638,15 +638,29 @@ impl ForegroundJournalWriter {
}
}
+ /// Publishes a point record that is neither work nor a boundary. Folded
+ /// polls stay folded: publishing them here would let them reach the
+ /// sealer even when [`Self::end_turn`] later discards folded-only work.
+ fn record_marker(&mut self, entry: ForegroundJournalEntry) {
+ self.publisher.publish([entry]);
+ }
+
fn record_frame_state(&mut self, change: FrameStateChange) {
self.record_entry(ForegroundJournalEntry::FrameState(change));
}
fn record_platform_signal(&mut self, signal: PlatformSignal) {
- self.record_entry(ForegroundJournalEntry::PlatformSignal(signal));
+ self.record_marker(ForegroundJournalEntry::PlatformSignal(signal));
}
+ /// Seals retained work at a frame request that didn't draw. Like idle
+ /// boundaries, skips require a retained event since the last boundary:
+ /// platforms that request a frame every refresh would otherwise seal an
+ /// empty interval, and flush folded polls, on every idle refresh.
fn record_frame_skipped(&mut self, skipped: FrameSkipped) {
+ if !self.retained_since_boundary {
+ return;
+ }
// Even a no-render callback can schedule or invalidate the next frame.
// Skipping does not establish that the window's pending demand is met.
self.record_entry(ForegroundJournalEntry::Boundary(
@@ -1038,7 +1052,7 @@ pub(crate) fn record_platform_signal(signal: PlatformSignal) {
pub(crate) fn record_reentrant_frame_skipped(window_id: WindowId, at: Instant) {
with_journal(|journal| {
- journal.record_entry(ForegroundJournalEntry::ReentrantFrameSkipped { window_id, at });
+ journal.record_marker(ForegroundJournalEntry::ReentrantFrameSkipped { window_id, at });
});
}
@@ -1424,8 +1438,7 @@ impl IntervalSealer {
}
if self.is_empty() {
self.interval_start = self.interval_start.max(boundary.end_time());
- }
- if !self.is_empty() || matches!(boundary, IntervalBoundary::FrameSkipped(_)) {
+ } else {
snapshots.push(self.seal(boundary));
}
}
@@ -1551,6 +1564,12 @@ mod tests {
let (mut writer, mut collector) = test_journal(ForegroundRunnableCounter::new());
writer.begin_turn();
writer.record_frame_pending(window_id, start);
+ writer.record_event(ForegroundEvent::Input(InputTiming {
+ kind: "test",
+ start,
+ end: start + Duration::from_millis(2),
+ caused_invalidation: false,
+ }));
writer.record_frame_skipped(FrameSkipped {
window_id,
at,
@@ -1568,7 +1587,7 @@ mod tests {
);
assert_eq!(snapshot.interval_end(), at);
assert_eq!(snapshot.boundary.dirty_at(), None);
- assert_eq!(snapshot.occupancy(), Duration::ZERO);
+ assert_eq!(snapshot.occupancy(), Duration::from_millis(2));
writer.record_event(ForegroundEvent::Input(InputTiming {
kind: "test",
@@ -1588,6 +1607,42 @@ mod tests {
}
}
+ /// Platforms that request a frame every refresh skip most of them while
+ /// idle. Those skips seal nothing and leave folded polls folded, so
+ /// end-of-turn idle handling can still discard folded-only work.
+ #[test]
+ fn idle_refreshes_seal_nothing_and_keep_polls_folded() {
+ let start = Instant::now();
+ let window_id = WindowId::from(1);
+ let (mut writer, mut collector) = test_journal(ForegroundRunnableCounter::new());
+ for refresh in 0..3u32 {
+ let at = start + Duration::from_millis(16) * refresh;
+ writer.begin_turn();
+ writer.fold_small_poll(task_timing(at, at + Duration::from_micros(50)));
+ writer.record_platform_signal(PlatformSignal {
+ window_id,
+ signal_at: Some(at),
+ handled_at: at,
+ source: crate::FrameRequestSource::NativeCallback,
+ });
+ writer.record_frame_skipped(FrameSkipped {
+ window_id,
+ at,
+ reason: FrameSkipReason::NoRenderNeeded,
+ });
+ writer.end_turn(at + Duration::from_micros(60));
+ }
+
+ let entries = collector.collect_unseen().entries;
+ assert!(
+ entries
+ .iter()
+ .all(|entry| matches!(entry, ForegroundJournalEntry::PlatformSignal(_))),
+ "expected only platform signals, got {entries:?}"
+ );
+ assert!(IntervalSealer::new(start).push_entries(entries).is_empty());
+ }
+
#[test]
fn reentrant_skip_does_not_seal_the_enclosing_draw() {
let (journal, _guard) = install_test_foreground_journal(32, 4);
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index a12cd5d6814..259a451ab16 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -142,6 +142,15 @@ struct FrameDirtyAccumulator {
invalidations: u64,
}
+/// Why GPUI capped a window's frame rate.
+#[derive(Clone, Copy)]
+enum FrameRateLimit {
+ /// The window isn't focused.
+ Inactive,
+ /// The system is under thermal pressure.
+ Thermal,
+}
+
#[derive(Clone)]
pub(crate) struct WindowInvalidator {
inner: Rc<RefCell<WindowInvalidatorInner>>,
@@ -1759,15 +1768,16 @@ impl Window {
{
None
} else if !active.get() && !input_rate_tracker.borrow_mut().is_high_rate() {
- inactive_frame_interval.map(|interval| (interval, true))
+ inactive_frame_interval.map(|interval| (interval, FrameRateLimit::Inactive))
} else if let Some(ThermalState::Critical | ThermalState::Serious) = thermal_state {
- Some((Duration::from_micros(16667), false))
+ Some((Duration::from_micros(16667), FrameRateLimit::Thermal))
} else {
None
};
let now = Instant::now();
- if let Some((min_interval, _inactive_throttle)) = min_frame_interval {
+ #[cfg_attr(not(feature = "profiler"), expect(unused_variables))]
+ if let Some((min_interval, limit)) = min_frame_interval {
if let Some(last_frame) = last_frame_time.get()
&& now.duration_since(last_frame) < min_interval
{
@@ -1785,18 +1795,18 @@ impl Window {
// idle windows need a wakeup to deliver the retry.
invalidator.wake_platform();
#[cfg(feature = "profiler")]
- {
- let reason = if _inactive_throttle {
- profiler::journal::FrameSkipReason::InactiveFrameRateLimit
- } else {
- profiler::journal::FrameSkipReason::ThermalFrameRateLimit
- };
- profiler::journal::record_frame_skipped(
- window_id,
- Instant::now(),
- reason,
- );
- }
+ profiler::journal::record_frame_skipped(
+ window_id,
+ Instant::now(),
+ match limit {
+ FrameRateLimit::Inactive => {
+ profiler::journal::FrameSkipReason::InactiveFrameRateLimit
+ }
+ FrameRateLimit::Thermal => {
+ profiler::journal::FrameSkipReason::ThermalFrameRateLimit
+ }
+ },
+ );
return;
}
}
@@ -7845,6 +7855,14 @@ mod tests {
draws_before
})
.expect("schedule throttled frame");
+ // Skips only seal retained work, so give the throttled skip some.
+ let input_at = Instant::now();
+ journal::record_input(journal::InputTiming {
+ kind: "test",
+ start: input_at,
+ end: input_at,
+ caused_invalidation: false,
+ });
collector.collect_unseen();
platform_window.simulate_frame_request(RequestFrameOptions {
signal_at: Some(Instant::now()),