From 2d2d3c4a83788f222386e9ef3a7b48075f558edf Mon Sep 17 00:00:00 2001 From: Andrew Gazelka Date: Tue, 22 Apr 2025 13:49:05 -0700 Subject: [PATCH] feat: use single threaded flecs --- crates/hyperion/src/egress/mod.rs | 1 - crates/hyperion/src/egress/sync_chunks.rs | 3 +- .../hyperion/src/egress/sync_entity_state.rs | 6 -- crates/hyperion/src/ingress/mod.rs | 2 - crates/hyperion/src/lib.rs | 2 +- crates/hyperion/src/simulation/inventory.rs | 2 +- .../hyperion/src/simulation/metadata/mod.rs | 1 - events/tag/src/module/attack.rs | 3 +- events/tag/src/module/block.rs | 6 +- events/tag/src/module/bow.rs | 3 - events/tag/src/module/chat.rs | 2 +- events/tag/src/module/regeneration.rs | 1 - events/tag/src/module/stats.rs | 80 +++++++++---------- events/tag/src/module/vanish.rs | 1 - 14 files changed, 47 insertions(+), 66 deletions(-) diff --git a/crates/hyperion/src/egress/mod.rs b/crates/hyperion/src/egress/mod.rs index c27cc4d3..0179f981 100644 --- a/crates/hyperion/src/egress/mod.rs +++ b/crates/hyperion/src/egress/mod.rs @@ -60,7 +60,6 @@ impl Module for EgressModule { &Compose($), &mut Blocks($), ) - .multi_threaded() .kind::() .each_iter(move |it: TableIter<'_, false>, _, (compose, mc)| { let span = info_span!("broadcast_chunk_deltas"); diff --git a/crates/hyperion/src/egress/sync_chunks.rs b/crates/hyperion/src/egress/sync_chunks.rs index 1d920018..b046ac68 100644 --- a/crates/hyperion/src/egress/sync_chunks.rs +++ b/crates/hyperion/src/egress/sync_chunks.rs @@ -45,7 +45,6 @@ impl Module for SyncChunksModule { ) .with_enum(PacketState::Play) .kind::() - .multi_threaded() .each_iter( move |it, _, (compose, last_sent, pose, &stream_id, chunk_changes)| { let system = it.system(); @@ -161,7 +160,7 @@ impl Module for SyncChunksModule { system!("send_full_loaded_chunks", world, &Blocks($), &Compose($), &ConnectionId, &mut ChunkSendQueue) .with_enum(PacketState::Play) .kind::() - .multi_threaded() + .each_iter( move |it, _, (chunks, compose, &stream_id, queue)| { const MAX_CHUNKS_PER_TICK: usize = 16; diff --git a/crates/hyperion/src/egress/sync_entity_state.rs b/crates/hyperion/src/egress/sync_entity_state.rs index 53b001dc..312e66de 100644 --- a/crates/hyperion/src/egress/sync_entity_state.rs +++ b/crates/hyperion/src/egress/sync_entity_state.rs @@ -53,7 +53,6 @@ fn track_previous(world: &World) { world .system_named::<(&mut (Prev, T), &T)>(system_name.as_str()) - .multi_threaded() .kind_id(post_store) .each(|(prev, value)| { *prev = *value; @@ -71,7 +70,6 @@ impl Module for EntityStateSyncModule { )>("entity_xp_sync") .term_at(0u32) .singleton() - .multi_threaded() .kind::() .each_iter(|table, idx, (compose, net, prev_xp, current)| { const { @@ -99,7 +97,6 @@ impl Module for EntityStateSyncModule { }); system!("entity_metadata_sync", world, &Compose($), &mut MetadataChanges) - .multi_threaded() .kind::() .each_iter(move |it, row, (compose, metadata_changes)| { let system = it.system(); @@ -135,7 +132,6 @@ impl Module for EntityStateSyncModule { ?&ConnectionId, &mut ActiveAnimation, ) - .multi_threaded() .kind::() .each_iter( move |it, row, (position, compose, connection_id, animation)| { @@ -177,7 +173,6 @@ impl Module for EntityStateSyncModule { &mut MovementTracking, &Flight, ) - .multi_threaded() .kind::() .each_iter( |it, @@ -368,7 +363,6 @@ impl Module for EntityStateSyncModule { &Owner, ?&ConnectionId ) - .multi_threaded() .kind::() .with_enum_wildcard::() .each_iter(|it, row, (position, velocity, owner, connection_id)| { diff --git a/crates/hyperion/src/ingress/mod.rs b/crates/hyperion/src/ingress/mod.rs index 3544eece..0d4c09b5 100644 --- a/crates/hyperion/src/ingress/mod.rs +++ b/crates/hyperion/src/ingress/mod.rs @@ -345,7 +345,6 @@ impl Module for IngressModule { .system_named::<(&ReceiveState, &ConnectionId, &mut PacketDecoder)>("ingress_to_ecs") .term_at(0u32) .singleton() // StreamLookup - // .multi_threaded() .immediate(true) .kind::() .each(move |(receive, connection_id, decoder)| { @@ -445,7 +444,6 @@ impl Module for IngressModule { &IgnMap($), ) .kind::() - .multi_threaded() .each_iter( move |it, row, diff --git a/crates/hyperion/src/lib.rs b/crates/hyperion/src/lib.rs index d59ecf05..ba856c32 100644 --- a/crates/hyperion/src/lib.rs +++ b/crates/hyperion/src/lib.rs @@ -25,7 +25,7 @@ #![feature(pointer_is_aligned_to)] #![feature(thread_local)] -pub const NUM_THREADS: usize = 8; +pub const NUM_THREADS: usize = 1; pub const CHUNK_HEIGHT_SPAN: u32 = 384; // 512; // usually 384 use std::{ diff --git a/crates/hyperion/src/simulation/inventory.rs b/crates/hyperion/src/simulation/inventory.rs index 78eb409a..cc55032d 100644 --- a/crates/hyperion/src/simulation/inventory.rs +++ b/crates/hyperion/src/simulation/inventory.rs @@ -123,7 +123,7 @@ impl Module for InventoryModule { ?&OpenInventory, &ConnectionId, ) - .multi_threaded() + .kind::() .each_iter( | diff --git a/crates/hyperion/src/simulation/metadata/mod.rs b/crates/hyperion/src/simulation/metadata/mod.rs index a8fe8980..b739c30a 100644 --- a/crates/hyperion/src/simulation/metadata/mod.rs +++ b/crates/hyperion/src/simulation/metadata/mod.rs @@ -45,7 +45,6 @@ where &mut T, // (1) &mut MetadataChanges, // (2) )>(system_name) - .multi_threaded() .kind::() .each(|(prev, current, metadata_changes)| { if prev != current { diff --git a/events/tag/src/module/attack.rs b/events/tag/src/module/attack.rs index 4f248a16..ddd262ee 100644 --- a/events/tag/src/module/attack.rs +++ b/events/tag/src/module/attack.rs @@ -104,7 +104,6 @@ impl Module for AttackModule { &ConnectionId, ) .with_enum(PacketState::Play) - .multi_threaded() .kind::() .each_iter(move |it, _, (compose, kill_count, stream)| { const MAX_KILLS: usize = 10; @@ -132,7 +131,7 @@ impl Module for AttackModule { // TODO: This code should be split between melee attacks and bow attacks system!("handle_attacks", world, &mut EventQueue($), &Compose($)) - .multi_threaded() + .each_iter( move |it: TableIter<'_, false>, _, diff --git a/events/tag/src/module/block.rs b/events/tag/src/module/block.rs index f7645f0d..28e2dbe2 100644 --- a/events/tag/src/module/block.rs +++ b/events/tag/src/module/block.rs @@ -73,7 +73,7 @@ impl Module for BlockModule { system!("handle_pending_air", world, &mut PendingDestruction($), &mut Blocks($), &Compose($)) .write::() - .multi_threaded() + .each_iter( move |it: TableIter<'_, false>, _, @@ -155,7 +155,7 @@ impl Module for BlockModule { ); system!("handle_destroyed_blocks", world, &mut Blocks($), &mut EventQueue($), &Compose($), &OreVeins($)) - .multi_threaded() + .each_iter(move |it: TableIter<'_, false>, _, (blocks, event_queue, compose, ore_veins): (&mut Blocks, &mut EventQueue, &Compose, &OreVeins)| { let span = info_span!("handle_blocks"); let _enter = span.enter(); @@ -303,7 +303,7 @@ impl Module for BlockModule { }); system!("handle_toggled_doors", world, &mut Blocks($), &mut EventQueue($)) - .multi_threaded() + .each_iter(move |_it: TableIter<'_, false>, _, (mc, event_queue): (&mut Blocks, &mut EventQueue)| { let span = info_span!("handle_toggled_doors"); let _enter = span.enter(); diff --git a/events/tag/src/module/bow.rs b/events/tag/src/module/bow.rs index f8774205..fe2b7834 100644 --- a/events/tag/src/module/bow.rs +++ b/events/tag/src/module/bow.rs @@ -85,7 +85,6 @@ impl Module for BowModule { &mut EventQueue, ) .singleton() - .multi_threaded() .kind::() .each_iter(move |it, _, event_queue| { let _system = it.system(); @@ -203,7 +202,6 @@ impl Module for BowModule { &Compose($), &mut EventQueue, ) - .multi_threaded() .singleton() .kind::() .each_iter(move |it, _, (compose, event_queue)| { @@ -263,7 +261,6 @@ impl Module for BowModule { world, &mut EventQueue, ) - .multi_threaded() .kind::() .each_iter(move |it, _, event_queue| { let _system = it.system(); diff --git a/events/tag/src/module/chat.rs b/events/tag/src/module/chat.rs index aea77baa..b10c33da 100644 --- a/events/tag/src/module/chat.rs +++ b/events/tag/src/module/chat.rs @@ -36,7 +36,7 @@ impl Module for ChatModule { .add_trait::<(flecs::With, ChatCooldown)>(); system!("handle_chat_messages", world, &mut EventQueue($), &hyperion::net::Compose($)) - .multi_threaded() + .each_iter(move |it: TableIter<'_, false>, _: usize, (event_queue, compose): (&mut EventQueue, &hyperion::net::Compose)| { let world = it.world(); let span = info_span!("handle_chat_messages"); diff --git a/events/tag/src/module/regeneration.rs b/events/tag/src/module/regeneration.rs index c76a11e7..62ff55d5 100644 --- a/events/tag/src/module/regeneration.rs +++ b/events/tag/src/module/regeneration.rs @@ -39,7 +39,6 @@ impl Module for RegenerationModule { &mut Health, &Compose($) ) - .multi_threaded() .tracing_each( info_span!("regenerate"), |(last_damaged, prev_health, health, compose)| { diff --git a/events/tag/src/module/stats.rs b/events/tag/src/module/stats.rs index 2639736e..ee2eb849 100644 --- a/events/tag/src/module/stats.rs +++ b/events/tag/src/module/stats.rs @@ -20,46 +20,44 @@ impl Module for StatsModule { let mut tick_times = Vec::with_capacity(20 * 60); // 20 ticks per second, 60 seconds let mut last_frame_time_total = 0.0; - system!("stats", world, &Compose($)) - .multi_threaded() - .each_iter(move |it, _, compose| { - let span = info_span!("stats"); - let _enter = span.enter(); - let system = it.system(); - let world = it.world(); - let player_count = compose - .global() - .player_count - .load(std::sync::atomic::Ordering::Relaxed); - - let info = world.info(); - let current_frame_time_total = info.frame_time_total; - - let ms_per_tick = (current_frame_time_total - last_frame_time_total) * 1000.0; - last_frame_time_total = current_frame_time_total; - - tick_times.push(ms_per_tick); - if tick_times.len() > 20 * 60 { - tick_times.remove(0); - } - - let avg_s05 = tick_times.iter().rev().take(20 * 5).sum::() / (20.0 * 5.0); - let avg_s15 = tick_times.iter().rev().take(20 * 15).sum::() / (20.0 * 15.0); - let avg_s60 = tick_times.iter().sum::() / tick_times.len() as f32; - - let title = format!( - "§b{mode}§r\n§aµ/5s: {avg_s05:.2} ms §r| §eµ/15s: {avg_s15:.2} ms §r| §cµ/1m: \ - {avg_s60:.2} ms" - ); - - let footer = format!("§d§l{player_count} players online"); - - let pkt = play::PlayerListHeaderS2c { - header: title.into_cow_text(), - footer: footer.into_cow_text(), - }; - - compose.broadcast(&pkt, system).send().unwrap(); - }); + system!("stats", world, &Compose($)).each_iter(move |it, _, compose| { + let span = info_span!("stats"); + let _enter = span.enter(); + let system = it.system(); + let world = it.world(); + let player_count = compose + .global() + .player_count + .load(std::sync::atomic::Ordering::Relaxed); + + let info = world.info(); + let current_frame_time_total = info.frame_time_total; + + let ms_per_tick = (current_frame_time_total - last_frame_time_total) * 1000.0; + last_frame_time_total = current_frame_time_total; + + tick_times.push(ms_per_tick); + if tick_times.len() > 20 * 60 { + tick_times.remove(0); + } + + let avg_s05 = tick_times.iter().rev().take(20 * 5).sum::() / (20.0 * 5.0); + let avg_s15 = tick_times.iter().rev().take(20 * 15).sum::() / (20.0 * 15.0); + let avg_s60 = tick_times.iter().sum::() / tick_times.len() as f32; + + let title = format!( + "§b{mode}§r\n§aµ/5s: {avg_s05:.2} ms §r| §eµ/15s: {avg_s15:.2} ms §r| §cµ/1m: \ + {avg_s60:.2} ms" + ); + + let footer = format!("§d§l{player_count} players online"); + + let pkt = play::PlayerListHeaderS2c { + header: title.into_cow_text(), + footer: footer.into_cow_text(), + }; + + compose.broadcast(&pkt, system).send().unwrap(); + }); } } diff --git a/events/tag/src/module/vanish.rs b/events/tag/src/module/vanish.rs index 43f8c153..bd18a614 100644 --- a/events/tag/src/module/vanish.rs +++ b/events/tag/src/module/vanish.rs @@ -36,7 +36,6 @@ impl Module for VanishModule { &Vanished, &Uuid, ) - .multi_threaded() .kind::() .each_iter(move |it, row, (compose, _connection_id, vanished, uuid)| { let entity = it.entity(row);