From 8291c8af5b06f73cec102b7c88702ab2cda67ca6 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 09:09:47 +0200 Subject: [PATCH] Chunk packets get 1 ms a frame; the rest waits for the next frame A client of a server (single player's server thread, LAN, multiplayer) handled every received packet in the frame it arrived. Crossing a chunk border at 32 chunks brings 25 to 60 columns at once, 0.1 ms each to decode on an M4 Pro: a 3 to 8 ms frame each time. Once a frame has spent 1 ms on chunk packets, the events from the next chunk packet on wait for the next frame, in order; events that have waited a tick are all handled at once, so a slow machine's backlog never holds packets longer than that. The status's pending_chunks counts the waiting events. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/control.rs | 2 +- crates/horizon-client/src/game/remote.rs | 63 ++++++++++++++++++++--- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index d79f69d3c..a4e70d038 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -886,7 +886,7 @@ impl Game<'_> { "chunks": self.world.chunks.len(), "mobs": self.mobs.len(), "block_entities": self.world.block_entities.len(), - "pending_chunks": self.streaming.pending() + self.pending_uploads.len(), + "pending_chunks": self.streaming.pending() + self.pending_uploads.len() + self.remote_backlog(), "generated_chunks": self.streaming.generated_total, "meshed_sections": self.streaming.meshed_total, "streaming": self.streaming.stages().into_iter().map(|(k, v)| (k.to_string(), v.into())).collect::>(), diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 5b8072d7f..afd13fd03 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -41,7 +41,11 @@ use horizon_protocol::{ text::{Language, format_java, plain}, }; use horizon_world::{nbt::Tag, registry::BlockStateId}; -use std::{collections::HashMap, sync::Arc, time::Instant}; +use std::{ + collections::{HashMap, VecDeque}, + sync::Arc, + time::{Duration, Instant}, +}; use winit::{event::MouseButton, keyboard::KeyCode}; /// A text component the server sent, for the HUD. @@ -158,8 +162,22 @@ pub(super) struct Remote { clock_game_time: i64, /// `ClientChunkCache.Storage`'s view centre (ClientboundSetChunkCacheCenterPacket). chunk_center: (i32, i32), + /// Received events a frame left for the next one, in order, and when + /// the oldest of them first waited (`poll_remote`). + backlog: VecDeque, + backlog_since: Option, } +/// The time a frame spends on chunk packets before it leaves the rest of +/// the received events for the next frame. A batch of 25 to 60 columns +/// (0.1 ms each on an M4 Pro) all in one frame was a 3 to 8 ms frame every +/// time the player crossed a chunk border at 32 chunks. +const CHUNK_PACKETS_PER_FRAME: Duration = Duration::from_millis(1); +/// Events that have waited this long are all handled in the next frame, +/// whatever their cost: the packets after a slow machine's chunks never +/// wait more than a tick. +const BACKLOG_LIMIT: Duration = Duration::from_millis(50); + fn face_direction(face: [i32; 3]) -> u8 { match face { [0, -1, 0] => 0, @@ -350,6 +368,8 @@ impl Game<'_> { clock_partial: 0.0, clock_game_time: 0, chunk_center: (0, 0), + backlog: VecDeque::new(), + backlog_since: None, }); // The level's TickRateManager follows the server's ticking packets. self.clock = horizon_client::clock::FixedStep::client(); @@ -544,12 +564,34 @@ impl Game<'_> { Some(r.chat.iter().rev().take(n).map(|l| l.0.clone()).collect()) } - /// Process everything the network thread received (once per frame, like - /// the client's packet processor). + /// Received events left for the next frame (`poll_remote`): until they + /// are handled, the terrain the server sent is not all there. + pub(super) fn remote_backlog(&self) -> usize { + self.remote.as_ref().map_or(0, |r| r.backlog.len()) + } + /// Process what the network thread received (once per frame, like the + /// client's packet processor), in order. Chunk packets get + /// `CHUNK_PACKETS_PER_FRAME`: once a frame has spent it, the events from + /// the next chunk packet on wait for the next frame, unless they have + /// already waited `BACKLOG_LIMIT`. pub(super) fn poll_remote(&mut self) -> Result<()> { - let Some(r) = &self.remote else { return Ok(()) }; - let events = r.session.poll(); - for event in events { + let Some(r) = self.remote.as_mut() else { + return Ok(()); + }; + let mut events = std::mem::take(&mut r.backlog); + events.extend(r.session.poll()); + let overdue = r + .backlog_since + .is_some_and(|since| since.elapsed() >= BACKLOG_LIMIT); + let mut chunks = Duration::ZERO; + while let Some(event) = events.pop_front() { + let chunk = + matches!(&event, Event::Packet(p) if matches!(**p, C::LevelChunkWithLight(_))); + if chunk && !overdue && chunks >= CHUNK_PACKETS_PER_FRAME { + events.push_front(event); + break; + } + let started = chunk.then(Instant::now); match event { Event::Status(s) => { if let Some(r) = self.remote.as_mut() { @@ -595,10 +637,19 @@ impl Game<'_> { } Event::Packet(packet) => self.handle_packet(*packet)?, } + chunks += started.map_or(Duration::ZERO, |t| t.elapsed()); if self.remote.is_none() { break; } } + if let Some(r) = self.remote.as_mut() { + r.backlog_since = if events.is_empty() { + None + } else { + Some(r.backlog_since.unwrap_or_else(Instant::now)) + }; + r.backlog = events; + } self.send_suggestion_request(); Ok(()) }