Skip to content

Commit

Permalink
feat: use a fixed sized constant array for zone flags in a box
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraviolet-jordan committed Oct 24, 2024
1 parent 61fea33 commit c26eb6b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ AMD Ryzen 9 3900X 12-Core Processor 3.80 GHz
Ran in Release mode on Windows x64 OS.

```
100k paths took: 312.3261ms; time per call: 3.123µs
100k paths took: 313.0271ms; time per call: 3.13µs
100k paths took: 310.554ms; time per call: 3.105µs
100k paths took: 312.095ms; time per call: 3.12µs
100k paths took: 312.5054ms; time per call: 3.125µs
100k paths took: 315.6638ms; time per call: 3.156µs
100k paths took: 279.3573ms; time per call: 2.793µs
100k paths took: 280.2238ms; time per call: 2.802µs
100k paths took: 281.206ms; time per call: 2.812µs
100k paths took: 278.8223ms; time per call: 2.788µs
100k paths took: 277.9747ms; time per call: 2.779µs
100k paths took: 279.5909ms; time per call: 2.795µs
100k paths took: 277.8473ms; time per call: 2.778µs
100k paths took: 281.0326ms; time per call: 2.81µs
```

### Rust (WebAssembly):
Expand All @@ -61,13 +63,14 @@ This typically takes about 850ms on average in a real world scenario tested
on a server also using TypeScript.

```
100k paths took: 711ms; time per call: 7.11µs
100k paths took: 709ms; time per call: 7.09µs
100k paths took: 707ms; time per call: 7.07µs
100k paths took: 714ms; time per call: 7.14µs
100k paths took: 707ms; time per call: 7.07µs
100k paths took: 715ms; time per call: 7.15µs
100k paths took: 712ms; time per call: 7.12µs
100k paths took: 703ms; time per call: 7.03µs
100k paths took: 702ms; time per call: 7.02µs
100k paths took: 701ms; time per call: 7.01µs
100k paths took: 702ms; time per call: 7.02µs
100k paths took: 699ms; time per call: 6.99µs
100k paths took: 705ms; time per call: 7.05µs
100k paths took: 701ms; time per call: 7.01µs
100k paths took: 702ms; time per call: 7.02µs
```

### AssemblyScript (WebAssembly):
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::rsmod::reach_strategy::ReachStrategy;
pub mod rsmod;

// alloc on the heap for the wasm module globally.
static mut COLLISION_FLAGS: Lazy<CollisionFlagMap> = Lazy::new(|| CollisionFlagMap::new());
static mut PATHFINDER: Lazy<PathFinder> = Lazy::new(|| PathFinder::new());
static mut COLLISION_FLAGS: Lazy<CollisionFlagMap> = Lazy::new(CollisionFlagMap::new);
static mut PATHFINDER: Lazy<PathFinder> = Lazy::new(PathFinder::new);

#[wasm_bindgen]
pub unsafe fn findPath(
Expand Down
10 changes: 5 additions & 5 deletions src/rsmod/collision/collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::rsmod::collision_flag::CollisionFlag;

#[derive(Clone)]
pub struct CollisionFlagMap {
pub flags: Vec<Option<Vec<u32>>>,
pub flags: Vec<Option<Box<[u32; 8 * 8]>>>,
}

impl CollisionFlagMap {
Expand Down Expand Up @@ -65,10 +65,10 @@ impl CollisionFlagMap {
}

#[inline(always)]
unsafe fn allocate_if_absent_return(&mut self, zone_idx: usize) -> &mut Vec<u32> {
return (*self.flags.as_mut_ptr().add(zone_idx)).get_or_insert_with(|| {
vec![CollisionFlag::OPEN as u32; CollisionFlagMap::ZONE_TILE_COUNT]
});
unsafe fn allocate_if_absent_return(&mut self, zone_idx: usize) -> &mut [u32; 64] {
return (*self.flags.as_mut_ptr().add(zone_idx)).get_or_insert(Box::new(
[CollisionFlag::OPEN as u32; CollisionFlagMap::ZONE_TILE_COUNT],
));
}

#[rustfmt::skip]
Expand Down

0 comments on commit c26eb6b

Please sign in to comment.