Implement tiered pathfinding with thread-local scratchpads

- TIER0 (<10 tiles): Vec-based linear search, zero hash overhead
- TIER1 (10-30 tiles): FxHashMap scratchpad with memory reuse
- TIER2 (30-100 tiles): AHashMap scratchpad for better collision resistance
- TIER3 (>100 tiles): Chunk waypoint decomposition
- Added benchmarking infrastructure with F8 report trigger
- Added async infrastructure: PendingPath, CompletedPaths resources
- Lowered MAX_NODES to 5000 to prevent runaway searches

Benchmarks show P50 improved 13%, max improved 53%
This commit is contained in:
2026-03-18 14:37:15 +00:00
parent 49972b9b64
commit 81b70a661c
5 changed files with 1317 additions and 35 deletions
Generated
+8
View File
@@ -2331,10 +2331,12 @@ dependencies = [
name = "dorf" name = "dorf"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"ahash",
"bevy", "bevy",
"bevy_platform", "bevy_platform",
"bevy_rand", "bevy_rand",
"image", "image",
"nohash-hasher",
"noise", "noise",
"rand 0.10.0", "rand 0.10.0",
"rayon", "rayon",
@@ -3567,6 +3569,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "nohash-hasher"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
[[package]] [[package]]
name = "noise" name = "noise"
version = "0.9.0" version = "0.9.0"
+2
View File
@@ -12,6 +12,8 @@ image = "0.25.10"
bevy_platform = "0.18.1" bevy_platform = "0.18.1"
rayon = "1.11.0" rayon = "1.11.0"
rustc-hash = "2.1.1" rustc-hash = "2.1.1"
ahash = "0.8.12"
nohash-hasher = "0.2.0"
# Enable max optimizations for dependencies, but not for our code: # Enable max optimizations for dependencies, but not for our code:
[profile.dev.package."*"] [profile.dev.package."*"]
+10
View File
@@ -3,3 +3,13 @@ pub const TILE_PIXELS: u32 = 16;
pub const TILE_SIZE: f32 = TILE_PIXELS as f32 * PIXEL_RATIO; pub const TILE_SIZE: f32 = TILE_PIXELS as f32 * PIXEL_RATIO;
pub const ITILE_SIZE: i32 = TILE_SIZE as i32; pub const ITILE_SIZE: i32 = TILE_SIZE as i32;
pub const SEED: u32 = 420; pub const SEED: u32 = 420;
// Pathfinding tier thresholds (in tiles)
pub const PATHFINDER_TIER0_MAX_TILES: i32 = 10; // Very short paths: Vec-based
pub const PATHFINDER_TIER1_MAX_TILES: i32 = 30; // Short paths: AHashMap scratchpad
pub const PATHFINDER_TIER2_MAX_TILES: i32 = 100; // Medium paths: AHashMap scratchpad
// TIER3: > 100 tiles → Chunk waypoints + async
// Pathfinding configuration
pub const PATHFINDER_MAX_NODES: usize = 5000; // Max nodes before partial path
pub const PATHFINDER_ASYNC_THRESHOLD_TILES: i32 = 150; // Start async for very long paths
@@ -9,3 +9,21 @@ pub struct Ambulatory {
pub target: Option<Vec3>, pub target: Option<Vec3>,
pub step_recovery: u32, pub step_recovery: u32,
} }
#[derive(Component)]
pub struct PendingPath {
pub start: IVec3,
pub goal: IVec3,
pub waypoint_path: Vec<Vec3>,
pub request_id: u64,
}
#[derive(Resource, Default)]
pub struct PathRequestCounter {
pub next_id: u64,
}
#[derive(Resource, Default)]
pub struct CompletedPaths {
pub paths: Vec<(u64, Vec<Vec3>)>,
}
File diff suppressed because it is too large Load Diff