aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2026-01-18 19:18:58 -0530
committermat <git@matdoes.dev>2026-01-19 12:21:28 +0930
commit544ea6ad46d7f4d621104a9063dbbe1c85284111 (patch)
treee205102e9d1948cbbede21b0058e111aa2d8017a
parenta08af6f2787ce3b04a7b369170254b00ac208375 (diff)
downloadazalea-drasl-544ea6ad46d7f4d621104a9063dbbe1c85284111.tar.xz
delay initialization of cached_mining_costs in pathfinder
-rw-r--r--azalea/src/pathfinder/execute/simulation.rs1
-rw-r--r--azalea/src/pathfinder/simulation.rs2
-rw-r--r--azalea/src/pathfinder/tests.rs8
-rw-r--r--azalea/src/pathfinder/world.rs29
4 files changed, 24 insertions, 16 deletions
diff --git a/azalea/src/pathfinder/execute/simulation.rs b/azalea/src/pathfinder/execute/simulation.rs
index 8d884327..a002df4d 100644
--- a/azalea/src/pathfinder/execute/simulation.rs
+++ b/azalea/src/pathfinder/execute/simulation.rs
@@ -149,6 +149,7 @@ pub fn tick_execute_path(
Cow::Borrowed(simulating_path_state)
} else {
let start = Instant::now();
+
let new_state = run_simulations(
&executing_path,
world_holder,
diff --git a/azalea/src/pathfinder/simulation.rs b/azalea/src/pathfinder/simulation.rs
index 2afbbfad..72e5e048 100644
--- a/azalea/src/pathfinder/simulation.rs
+++ b/azalea/src/pathfinder/simulation.rs
@@ -118,7 +118,7 @@ fn create_simulation_player_complete_bundle(
)
}
-fn create_simulation_player(
+pub fn create_simulation_player(
ecs: &mut bevy_ecs::world::World,
world: Arc<RwLock<World>>,
player: SimulatedPlayerBundle,
diff --git a/azalea/src/pathfinder/tests.rs b/azalea/src/pathfinder/tests.rs
index a428b177..3f4019ed 100644
--- a/azalea/src/pathfinder/tests.rs
+++ b/azalea/src/pathfinder/tests.rs
@@ -6,7 +6,7 @@ use std::{
};
use azalea_block::BlockState;
-use azalea_core::position::{BlockPos, ChunkPos, Vec3};
+use azalea_core::position::{BlockPos, ChunkPos};
use azalea_registry::builtin::BlockKind;
use azalea_world::{Chunk, ChunkStorage, PartialChunkStorage};
@@ -73,11 +73,7 @@ fn setup_simulation_world(
chunks.set_block_state(*block_pos, *block_state);
}
- let player = SimulatedPlayerBundle::new(Vec3::new(
- start_pos.x as f64 + 0.5,
- start_pos.y as f64,
- start_pos.z as f64 + 0.5,
- ));
+ let player = SimulatedPlayerBundle::new(start_pos.center_bottom());
Simulation::new(chunks, player)
}
diff --git a/azalea/src/pathfinder/world.rs b/azalea/src/pathfinder/world.rs
index 4e9952cd..4e55dfe1 100644
--- a/azalea/src/pathfinder/world.rs
+++ b/azalea/src/pathfinder/world.rs
@@ -33,7 +33,7 @@ pub struct CachedWorld {
cached_blocks: UnsafeCell<CachedSections>,
- cached_mining_costs: UnsafeCell<Box<[(RelBlockPos, f32)]>>,
+ cached_mining_costs: UnsafeCell<Option<Box<[(RelBlockPos, f32)]>>>,
}
// we store `PalettedContainer`s instead of `Chunk`s or `Section`s because it
@@ -111,12 +111,7 @@ impl CachedWorld {
cached_chunks: Default::default(),
last_chunk_cache_index: Default::default(),
cached_blocks: Default::default(),
- // this uses about 12mb of memory. it *really* helps though.
- cached_mining_costs: UnsafeCell::new(
- (0..CACHED_MINING_COSTS_SIZE)
- .map(|_| (RelBlockPos::new(i16::MAX, i32::MAX, i16::MAX), 0.))
- .collect(),
- ),
+ cached_mining_costs: UnsafeCell::new(None),
}
}
@@ -316,8 +311,7 @@ impl CachedWorld {
///
/// Returns 0 if the block is already passable.
pub fn cost_for_breaking_block(&self, pos: RelBlockPos, mining_cache: &MiningCache) -> f32 {
- // SAFETY: pathfinding is single-threaded
- let cached_mining_costs = unsafe { &mut *self.cached_mining_costs.get() };
+ let cached_mining_costs = self.cached_mining_costs();
let hash_index = calculate_cached_mining_costs_index(pos);
let &(cached_pos, potential_cost) =
@@ -334,6 +328,23 @@ impl CachedWorld {
cost
}
+ fn cached_mining_costs(&self) -> &mut [(RelBlockPos, f32)] {
+ // SAFETY: pathfinding is single-threaded
+ let cached_mining_costs = unsafe { &mut *self.cached_mining_costs.get() };
+ if let Some(cached_mining_costs) = cached_mining_costs {
+ return cached_mining_costs;
+ }
+ // delay initialization so we don't have to create this if it's unused
+
+ // this uses about 48mb of memory. it *really* helps though.
+ *cached_mining_costs = Some(
+ vec![(RelBlockPos::new(i16::MAX, i32::MAX, i16::MAX), 0.); CACHED_MINING_COSTS_SIZE]
+ .into(),
+ );
+
+ cached_mining_costs.as_mut().unwrap()
+ }
+
fn uncached_cost_for_breaking_block(
&self,
pos: RelBlockPos,