diff options
| author | mat <git@matdoes.dev> | 2023-10-07 14:26:09 -0500 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2023-10-07 14:26:09 -0500 |
| commit | 3831bd6f9cded2831a173c317832dd5f03617b38 (patch) | |
| tree | 7e2616f8f89c055d825986cca485a7095935f32b | |
| parent | 87bfc642dafab835378dac03e3d0c2577c018bdc (diff) | |
| download | azalea-drasl-3831bd6f9cded2831a173c317832dd5f03617b38.tar.xz | |
update chunk batching
| -rw-r--r-- | azalea-client/src/chunk_batching.rs | 45 | ||||
| -rwxr-xr-x | azalea-world/src/chunk_storage.rs | 2 |
2 files changed, 30 insertions, 17 deletions
diff --git a/azalea-client/src/chunk_batching.rs b/azalea-client/src/chunk_batching.rs index c0e8bb34..81fb7550 100644 --- a/azalea-client/src/chunk_batching.rs +++ b/azalea-client/src/chunk_batching.rs @@ -38,7 +38,31 @@ impl Plugin for ChunkBatchingPlugin { #[derive(Component, Clone, Debug)] pub struct ChunkBatchInfo { pub start_time: Instant, - pub accumulator: ChunkReceiveSpeedAccumulator, + pub aggregated_duration_per_chunk: Duration, + pub old_samples_weight: u32, +} + +impl ChunkBatchInfo { + pub fn batch_finished(&mut self, batch_size: u32) { + if batch_size == 0 { + return; + } + let batch_duration = self.start_time.elapsed(); + let duration_per_chunk = batch_duration / batch_size; + let clamped_duration = Duration::clamp( + duration_per_chunk, + self.aggregated_duration_per_chunk / 3, + self.aggregated_duration_per_chunk * 3, + ); + self.aggregated_duration_per_chunk = + ((self.aggregated_duration_per_chunk * self.old_samples_weight) + clamped_duration) + / (self.old_samples_weight + 1); + self.old_samples_weight = u32::min(49, self.old_samples_weight + 1); + } + + pub fn desired_chunks_per_tick(&self) -> f32 { + (7000000. / self.aggregated_duration_per_chunk.as_nanos() as f64) as f32 + } } #[derive(Event)] @@ -69,20 +93,8 @@ pub fn handle_chunk_batch_finished_event( ) { for event in events.iter() { if let Ok(mut chunk_batch_info) = query.get_mut(event.entity) { - let batch_duration = chunk_batch_info.start_time.elapsed(); - if event.batch_size > 0 { - chunk_batch_info - .accumulator - .accumulate(event.batch_size, batch_duration); - } - let millis_per_chunk = - f64::max(0., chunk_batch_info.accumulator.get_millis_per_chunk()); - let desired_chunks_per_tick = if millis_per_chunk == 0. { - // make it the server's problem instead - f32::NAN - } else { - (25. / millis_per_chunk) as f32 - }; + chunk_batch_info.batch_finished(event.batch_size); + let desired_chunks_per_tick = chunk_batch_info.desired_chunks_per_tick(); send_packets.send(SendPacketEvent { entity: event.entity, packet: ServerboundChunkBatchReceivedPacket { @@ -140,7 +152,8 @@ impl Default for ChunkBatchInfo { fn default() -> Self { Self { start_time: Instant::now(), - accumulator: ChunkReceiveSpeedAccumulator::new(50), + aggregated_duration_per_chunk: Duration::from_millis(2), + old_samples_weight: 1, } } } diff --git a/azalea-world/src/chunk_storage.rs b/azalea-world/src/chunk_storage.rs index e7ff40c5..db4dd952 100755 --- a/azalea-world/src/chunk_storage.rs +++ b/azalea-world/src/chunk_storage.rs @@ -145,8 +145,8 @@ impl PartialChunkStorage { heightmaps, )?; - trace!("Loaded chunk {:?}", pos); self.set(pos, Some(chunk), chunk_storage); + trace!("Loaded chunk {pos:?}"); Ok(()) } |
