From 55264459ce4a818a7a88b42582c59e057c3a90b1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:47:22 +0200 Subject: [PATCH] In-frame ticks stop at a tick's length per frame: an overloaded world slows down instead of drawing at 2 FPS Single player's world ticks in the frame by default (#1428). When its ticks take longer than 50 ms, every frame fell behind and ran the client timer's ten, so the picture stalled at 1-2 FPS (a player's report on an AMD 680M, in-frame tick at 36.7 ms). Vanilla's world ticks on the integrated server's thread, which falls behind without costing frames and logs "Can't keep up!". A frame now stops running world ticks once they took a tick's length and drops the rest, logging vanilla's overload warning (1 s + 20 ticks behind, at most every 10 s + 100 ticks). Ticks of a few milliseconds still get the full ten per frame; sprints and server clients are unchanged. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/clock.rs | 72 ++++++++++++++++++++++++++++++ crates/horizon-client/src/game.rs | 13 +++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/clock.rs b/crates/horizon-client/src/clock.rs index 3240944ba..5239db143 100644 --- a/crates/horizon-client/src/clock.rs +++ b/crates/horizon-client/src/clock.rs @@ -304,6 +304,10 @@ pub struct FixedStep { /// How many times `/tick step` started or stopped a step /// (`ServerTickRateManager.updateStepTicks` broadcasts each one). step_changes: u32, + /// In-frame ticks dropped since the last overload warning (`drop_ticks`). + dropped: u32, + /// When the last overload warning was due, on the frame clock. + last_overload_warning: std::time::Duration, } impl Default for FixedStep { @@ -320,6 +324,8 @@ impl Default for FixedStep { tick_count: 0, client: false, step_changes: 0, + dropped: 0, + last_overload_warning: std::time::Duration::ZERO, } } } @@ -434,6 +440,41 @@ impl FixedStep { pub fn sprinting(&self) -> bool { self.manager.is_sprinting() } + /// Whether a frame that ran its world in the frame, and whose ticks so + /// far took `spent`, may run another one. Vanilla's world ticks on the + /// integrated server's thread, so a slow tick costs its frames nothing: + /// the server falls behind and says so (`MinecraftServer.runServer`'s + /// "Can't keep up!"). In the frame, a world whose ticks take longer than + /// their length falls behind every frame, so every frame ran the client + /// timer's ten: at 57 ms a tick, 1.7 FPS. So a frame stops once its + /// ticks have taken a tick's length: ticks of a few milliseconds keep the + /// full ten, and a world that cannot keep up runs one tick a frame, + /// slower but drawn. A sprint (`/tick sprint`) runs its own frame's worth. + pub fn tick_fits(&self, spent: std::time::Duration) -> bool { + self.sprinting() || spent < self.tick_duration() + } + /// The frame skips its remaining `ticks` (`tick_fits`): the world runs + /// slower than its tick rate. Returns vanilla's overload warning when one + /// is due: a second's worth of ticks behind, at most every ten seconds + /// (`OVERLOADED_THRESHOLD_NANOS`, `OVERLOADED_WARNING_INTERVAL_NANOS`). + pub fn drop_ticks(&mut self, ticks: u32) -> Option { + self.dropped = self.dropped.saturating_add(ticks); + let tick = self.tick_duration(); + let behind = tick * self.dropped; + let threshold = std::time::Duration::from_secs(1) + tick * 20; + let interval = std::time::Duration::from_secs(10) + tick * 100; + if behind <= threshold || self.now < self.last_overload_warning + interval { + return None; + } + let warning = format!( + "Can't keep up! Is the server overloaded? Running {}ms or {} ticks behind", + behind.as_millis(), + self.dropped + ); + self.dropped = 0; + self.last_overload_warning = self.now; + Some(warning) + } /// `requestGameToSprint`: true when a sprint was already running. pub fn start_sprint(&mut self, ticks: u32) -> bool { self.manager @@ -494,6 +535,37 @@ impl FixedStep { mod tests { use super::*; + #[test] + fn a_frame_stops_ticking_after_a_ticks_length() { + let clock = FixedStep::default(); + let ms = std::time::Duration::from_millis; + // Ticks of 4 ms: the ten the timer allows fit in 50 ms. + assert!(clock.tick_fits(ms(4 * 9))); + // Ticks of 40 ms: the second one runs, not a third. + assert!(clock.tick_fits(ms(40))); + assert!(!clock.tick_fits(ms(80))); + // One tick longer than its length still runs, once a frame. + assert!(clock.tick_fits(ms(0))); + assert!(!clock.tick_fits(ms(120))); + } + #[test] + fn dropped_ticks_warn_once_a_second_behind_and_not_again_for_ten() { + let mut clock = FixedStep::default(); + let s = std::time::Duration::from_secs; + clock.now = s(20); + assert_eq!( + clock.drop_ticks(40), + None, + "two seconds: not past 1 s + 20 ticks" + ); + let warning = clock.drop_ticks(1).expect("past the threshold"); + assert!(warning.contains("2050ms or 41 ticks behind"), "{warning}"); + clock.now = s(25); + assert_eq!(clock.drop_ticks(100), None, "within ten seconds"); + clock.now = s(36); + assert!(clock.drop_ticks(0).unwrap().contains("100 ticks behind")); + } + #[test] fn timer_carries_fractions_and_caps_nothing_itself() { let mut t = Timer::new(20.0, 0); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index dc6be85b7..2eb9d1fb2 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -11274,7 +11274,17 @@ impl ApplicationHandler for Game<'_> { // For the "I'm playing" ping: an atomic, sent on when it changes. horizon_client::heartbeat::set_lan(self.is_published()); let steps = self.clock.advance(elapsed); - for _ in 0..steps { + // The world ticking in the frame: once its ticks took a + // tick's length, the rest are dropped (`FixedStep::tick_fits`). + let in_frame = self.remote.is_none(); + let mut spent = Duration::ZERO; + for step in 0..steps { + if in_frame && !self.clock.tick_fits(spent) { + if let Some(warning) = self.clock.drop_ticks(steps - step) { + log::warn!("{warning}"); + } + break; + } let started = Instant::now(); if self.title.is_none() && !self.pause { let tick = Instant::now(); @@ -11291,6 +11301,7 @@ impl ApplicationHandler for Game<'_> { self.tick_music(); self.tick_sound_engine(); self.clock.record(started.elapsed()); + spent += started.elapsed(); if let Some(report) = self.clock.take_sprint_report() { self.sprint_report(&report); }