From 25cd1c0b60604655b70d70f8ec33a54853905eea Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 18 Mar 2026 16:28:46 -1030 Subject: optimize pathfinder swarms and write perf guide --- azalea/src/pathfinder/execute/patching.rs | 33 ++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'azalea/src/pathfinder/execute/patching.rs') diff --git a/azalea/src/pathfinder/execute/patching.rs b/azalea/src/pathfinder/execute/patching.rs index 855aa73d..e1ca2a81 100644 --- a/azalea/src/pathfinder/execute/patching.rs +++ b/azalea/src/pathfinder/execute/patching.rs @@ -1,4 +1,10 @@ -use std::{cmp, collections::VecDeque, ops::RangeInclusive, sync::Arc}; +use std::{ + cmp, + collections::VecDeque, + ops::RangeInclusive, + sync::Arc, + time::{Duration, Instant}, +}; use azalea_core::position::BlockPos; use azalea_entity::inventory::Inventory; @@ -34,11 +40,9 @@ pub fn check_for_path_obstruction( )>, worlds: Res, ) { - for (entity, mut pathfinder, mut executing_path, world_name, inventory, custom_state) in - &mut query - { + query.par_iter_mut().for_each(|(entity, mut pathfinder, mut executing_path, world_name, inventory, custom_state)| { let Some(opts) = pathfinder.opts.clone() else { - continue; + return; }; let world_lock = worlds @@ -65,13 +69,17 @@ pub fn check_for_path_obstruction( ) }; + // don't bother spending more than 10ms per tick on this + let timeout = Duration::from_millis(10); + let Some(obstructed_index) = check_path_obstructed( origin, RelBlockPos::from_origin(origin, executing_path.last_reached_node), &executing_path.path, successors, + timeout ) else { - continue; + return; }; drop(custom_state_ref); @@ -89,12 +97,12 @@ pub fn check_for_path_obstruction( ); executing_path.path.truncate(obstructed_index); executing_path.is_path_partial = true; - continue; + return; } let Some(opts) = pathfinder.opts.clone() else { error!("got PatchExecutingPathEvent but the bot has no pathfinder opts"); - continue; + return; }; let world_lock = worlds @@ -114,7 +122,7 @@ pub fn check_for_path_obstruction( custom_state.clone(), opts, ); - } + }); } /// Update the given [`ExecutingPath`] to recalculate the path of the nodes in @@ -222,11 +230,18 @@ pub fn check_path_obstructed( mut current_position: RelBlockPos, path: &VecDeque>, successors_fn: SuccessorsFn, + timeout: Duration, ) -> Option where SuccessorsFn: Fn(RelBlockPos) -> Vec>, { + let start_time = Instant::now(); + for (i, edge) in path.iter().enumerate() { + if start_time.elapsed() > timeout { + break; + } + let movement_target = RelBlockPos::from_origin(origin, edge.movement.target); let mut found_edge = None; -- cgit v1.2.3