Skip to content

Commit

Permalink
perf: reorder systems (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka authored Jul 1, 2024
1 parent cb51657 commit a7f2e1b
Show file tree
Hide file tree
Showing 16 changed files with 358 additions and 290 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ tar = "0.4.41"
thiserror = "1.0.61"
tokio = "1.38.0"
toml = "0.8.14"
tracing = { version = "0.1.40", features = ["release_max_level_info"] }
tracing = { version = "0.1.40" }
tracing-subscriber = "0.3.18"
tracing-tracy = "0.11.0"
uuid = "1.8.0"
Expand Down
33 changes: 27 additions & 6 deletions crates/hyperion/src/component/blocks/loaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,24 @@ impl OnChange {
const _: () = assert!(size_of::<OnChange>() == 3);

#[derive(Debug)]
pub struct ThreadList<T> {
pub struct ThreadHeaplessVec<T> {
inner: ThreadLocal<SyncUnsafeCell<heapless::Vec<T, 32>>>,
}

impl<T> Default for ThreadList<T> {
#[derive(Debug)]
pub struct ThreadLocalVec<T> {
inner: ThreadLocal<SyncUnsafeCell<Vec<T>>>,
}

impl<T> Default for ThreadHeaplessVec<T> {
fn default() -> Self {
Self {
inner: ThreadLocal::new_defaults(),
}
}
}

impl<T> Default for ThreadLocalVec<T> {
fn default() -> Self {
Self {
inner: ThreadLocal::new_defaults(),
Expand All @@ -104,19 +117,27 @@ impl<T> Default for ThreadList<T> {
}

#[derive(Debug, Default, Deref, DerefMut, Component)]
pub struct PendingChanges(ThreadList<Delta>);
pub struct PendingChanges(ThreadHeaplessVec<Delta>);

#[derive(Debug, Default, Deref, DerefMut, Component)]
pub struct NeighborNotify(ThreadList<OnChange>);
pub struct NeighborNotify(ThreadHeaplessVec<OnChange>);

impl<T: Debug> ThreadList<T> {
impl<T: Debug> ThreadHeaplessVec<T> {
pub fn push(&self, element: T, world: &World) {
let inner = self.inner.get(world);
let inner = unsafe { &mut *inner.get() };
assert!(inner.push(element).is_ok(), "ThreadList {inner:?} is full");
}
}

impl<T: Debug> ThreadLocalVec<T> {
pub fn push(&self, element: T, world: &World) {
let inner = self.inner.get(world);
let inner = unsafe { &mut *inner.get() };
inner.push(element);
}
}

struct Drain<'a, T, const N: usize> {
inner: &'a mut heapless::Vec<T, N>,
idx: usize,
Expand Down Expand Up @@ -151,7 +172,7 @@ impl<'a, T, const N: usize> Drain<'a, T, N> {
}
}

impl<T> ThreadList<T> {
impl<T> ThreadHeaplessVec<T> {
pub fn drain(&mut self) -> impl Iterator<Item = T> + '_ {
self.inner
.iter_mut()
Expand Down
18 changes: 8 additions & 10 deletions crates/hyperion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::{
global::Global,
net::{proxy::init_proxy_comms, Compose, Compressors, IoBuf, MAX_PACKET_SIZE},
runtime::AsyncRuntime,
singleton::{fd_lookup::StreamLookup, player_id_lookup::EntityIdLookup},
singleton::fd_lookup::StreamLookup,
system::{chunks::ChunkChanges, player_join_world::generate_biome_registry},
thread_local::ThreadLocal,
util::{db, db::Db},
Expand All @@ -70,6 +70,7 @@ pub mod net;

mod packets;
mod system;
mod tracing_ext;

mod bits;

Expand Down Expand Up @@ -247,27 +248,26 @@ impl Hyperion {

world.set(Comms::default());

world.set(EntityIdLookup::default());

world.set(egress_comm);
world.set(tasks);

system::ingress::player_connect_disconnect(world, receive_state.0.clone());
system::ingress::ingress_to_ecs(world, receive_state.0);
system::ingress::remove_player(world);
system::stats::stats(world);
system::joins::joins(world);

system::chunks::load_pending(world);
system::chunks::generate_chunk_changes(world);
system::chunks::send_updates(world);

system::stats::stats(world);
let biome_registry =
generate_biome_registry().context("failed to generate biome registry")?;
world.set(MinecraftWorld::new(&biome_registry)?);

world.set(StreamLookup::default());

system::ingress::player_connect_disconnect(world, receive_state.0.clone());
system::ingress::ingress_to_ecs(world, receive_state.0);

system::ingress::remove_player_from_visibility(world);
system::ingress::remove_player(world);

system::ingress::recv_data(world);

Expand All @@ -278,8 +278,6 @@ impl Hyperion {
system::event_handler::reset_event_queue(world);
system::event_handler::reset_allocators(world);

system::joins::joins(world);

system::egress::egress(world);

app.run();
Expand Down
27 changes: 5 additions & 22 deletions crates/hyperion/src/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use crate::{
event,
event::{EventQueue, Posture, ThreadLocalBump},
net::{Compose, NetworkStreamRef},
singleton::player_id_lookup::EntityIdLookup,
};

pub mod vanilla;
Expand Down Expand Up @@ -62,7 +61,7 @@ fn full(query: &mut PacketSwitchQuery, mut data: &[u8]) -> anyhow::Result<()> {
Ok(())
}

#[instrument(skip_all)]
// #[instrument(skip_all)]
fn change_position_or_correct_client(query: &mut PacketSwitchQuery, proposed: Vec3) {
let pose = &mut *query.pose;

Expand Down Expand Up @@ -206,11 +205,7 @@ fn hand_swing(mut data: &[u8], query: &PacketSwitchQuery) -> anyhow::Result<()>
}

#[instrument(skip_all)]
fn player_interact_entity(
mut data: &[u8],
query: &PacketSwitchQuery,
id_lookup: &EntityIdLookup,
) -> anyhow::Result<()> {
fn player_interact_entity(mut data: &[u8], query: &PacketSwitchQuery) -> anyhow::Result<()> {
let packet = play::PlayerInteractEntityC2s::decode(&mut data)?;

let from_pos = query.pose.position;
Expand All @@ -221,15 +216,7 @@ fn player_interact_entity(
}

let target = packet.entity_id.0;

let Some(target) = id_lookup.get(&target).as_deref().copied() else {
// no target
warn!(
"no target for {target}.. id_lookup haas {} entries",
id_lookup.len()
);
return Ok(());
};
let target = u64::try_from(target).unwrap();

info!("enqueue attack");
let target = query.world.entity_from_id(target);
Expand Down Expand Up @@ -481,11 +468,7 @@ pub fn player_interact_block(mut data: &[u8], query: &mut PacketSwitchQuery) ->
Ok(())
}

pub fn packet_switch(
raw: &PacketFrame,
id_lookup: &EntityIdLookup,
query: &mut PacketSwitchQuery,
) -> anyhow::Result<()> {
pub fn packet_switch(raw: &PacketFrame, query: &mut PacketSwitchQuery) -> anyhow::Result<()> {
let packet_id = raw.id;
let data = raw.body.as_ref();

Expand All @@ -502,7 +485,7 @@ pub fn packet_switch(
// play::UpdatePlayerAbilitiesC2s::ID => update_player_abilities(data)?,
// play::UpdateSelectedSlotC2s::ID => update_selected_slot(data, world, query.id)?,
play::PlayerInteractEntityC2s::ID => {
player_interact_entity(data, query, id_lookup)?;
player_interact_entity(data, query)?;
}
// play::PlayerInteractItemC2s::ID => player_interact_item(data, query, world)?,
// play::KeepAliveC2s::ID => keep_alive(query.keep_alive)?,
Expand Down
1 change: 0 additions & 1 deletion crates/hyperion/src/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
pub mod buf;
pub mod fd_lookup;
pub mod player_aabb_lookup;
pub mod player_id_lookup;
pub mod player_uuid_lookup;
4 changes: 2 additions & 2 deletions crates/hyperion/src/singleton/fd_lookup.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Lookup players by their UUID
use dashmap::DashMap;
use derive_more::{Deref, DerefMut};
use flecs_ecs::{core::Entity, macros::Component};
use fxhash::FxHashMap;

/// See [`crate::singleton::player_uuid_lookup`].
#[derive(Component, Default, Debug, Deref, DerefMut)]
pub struct StreamLookup {
/// The UUID of all players
inner: DashMap<u64, Entity>,
inner: FxHashMap<u64, Entity>,
}
12 changes: 0 additions & 12 deletions crates/hyperion/src/singleton/player_id_lookup.rs

This file was deleted.

25 changes: 13 additions & 12 deletions crates/hyperion/src/system/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::collections::HashSet;

use derive_more::{Deref, DerefMut};
use flecs_ecs::{
core::{QueryBuilderImpl, SystemAPI, TermBuilderImpl, World},
core::{IntoWorld, QueryBuilderImpl, SystemAPI, TermBuilderImpl, World},
macros::{system, Component},
};
use glam::I16Vec2;
use tracing::{debug, error, info_span, instrument};
use tracing::{debug, error, instrument, trace_span};
use valence_protocol::packets::play;

use crate::{
Expand All @@ -17,6 +17,7 @@ use crate::{
config::CONFIG,
net::{Compose, NetworkStreamRef},
runtime::AsyncRuntime,
tracing_ext::TracingExt,
};

#[derive(Component, Deref, DerefMut, Default)]
Expand All @@ -31,7 +32,7 @@ pub fn load_pending(world: &World) {
&mut MinecraftWorld($),
)
.each_iter(|iter, _, blocks| {
let span = tracing::trace_span!("load_pending");
let span = trace_span!("load_pending");
let _enter = span.enter();
blocks.load_pending(&iter.world());
});
Expand All @@ -51,9 +52,10 @@ pub fn generate_chunk_changes(world: &World) {
&mut ChunkChanges,
)
.multi_threaded()
.each_iter(
move |it, _, (compose, last_sent, pose, stream_id, chunk_changes)| {
let world = it.world();
.tracing_each_entity(
trace_span!("generate_chunk_changes"),
move |entity, (compose, last_sent, pose, stream_id, chunk_changes)| {
let world = entity.world();

let last_sent_chunk = last_sent.0;

Expand Down Expand Up @@ -105,14 +107,13 @@ pub fn send_updates(world: &World) {
&mut ChunkChanges,
)
.with::<&Play>()
.each_iter(
|iter, _, (chunks, tasks, compose, stream_id, chunk_changes)| {
let span = info_span!("send_updates");
let _enter = span.enter();

.multi_threaded()
.tracing_each_entity(
trace_span!("send_updates"),
|entity, (chunks, tasks, compose, stream_id, chunk_changes)| {
let mut left_over = Vec::new();

let world = iter.world();
let world = entity.world();

for &elem in &chunk_changes.changes {
match chunks.get_cached_or_load(elem, tasks, &world) {
Expand Down
41 changes: 21 additions & 20 deletions crates/hyperion/src/system/egress.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use flecs_ecs::core::{
flecs::pipeline::OnUpdate, QueryBuilderImpl, SystemAPI, TermBuilderImpl, World,
use flecs_ecs::{
core::{flecs::pipeline, QueryBuilderImpl, SystemAPI, TermBuilderImpl, World},
macros::system,
};
use hyperion_proto::Flush;
use prost::Message;
Expand Down Expand Up @@ -33,24 +34,24 @@ pub fn egress(world: &World) {
bytes::Bytes::from_static(data)
});

world
.system_named::<(&mut Compose, &mut EgressComm)>("egress")
.kind::<OnUpdate>()
.term_at(0)
.singleton()
.term_at(1)
.singleton()
.each(|(compose, egress)| {
let span = tracing::trace_span!("egress");
let _enter = span.enter();
let io = compose.io_buf_mut();
for bytes in io.split() {
if bytes.is_empty() {
continue;
}
egress.send(bytes.freeze()).unwrap();
system!(
"egress",
world,
&mut Compose($),
&mut EgressComm($),
)
.kind::<pipeline::PostUpdate>()
.each(|(compose, egress)| {
let span = tracing::trace_span!("egress");
let _enter = span.enter();
let io = compose.io_buf_mut();
for bytes in io.split() {
if bytes.is_empty() {
continue;
}
egress.send(bytes.freeze()).unwrap();
}

egress.send(FLUSH.clone()).unwrap();
});
egress.send(FLUSH.clone()).unwrap();
});
}
Loading

0 comments on commit a7f2e1b

Please sign in to comment.