Skip to content

Commit

Permalink
Squeeze some more performance out.
Browse files Browse the repository at this point in the history
Get rid of the windowing vector, map to the array of surrounding y-
coordinates.
  • Loading branch information
khoover committed Sep 9, 2024
1 parent d96a1e6 commit ea510a3
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/algorithms/distance_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub fn chebyshev_distance_transform_from_cost_matrix(mut cm: LocalCostMatrix) ->
let one = RoomCoordinate::new(1).unwrap();
let forty_eight = RoomCoordinate::new(ROOM_SIZE - 2).unwrap();
let forty_nine = RoomCoordinate::new(ROOM_SIZE - 1).unwrap();
let windowing_vec: Vec<_> = range_inclusive(zero, forty_nine).collect();
// Pass 1: Top-to-Bottom, Left-to-Right

// Phase A: first column
Expand Down Expand Up @@ -69,12 +68,18 @@ pub fn chebyshev_distance_transform_from_cost_matrix(mut cm: LocalCostMatrix) ->
initial_top,
);
let final_top = range_exclusive(zero, forty_nine)
.map(|y| RoomXY { x: current_x, y })
.zip(windowing_vec.windows(3))
.map(|y| {
(RoomXY { x: current_x, y }, unsafe {
[
RoomCoordinate::unchecked_new(y.u8() - 1),
y,
RoomCoordinate::unchecked_new(y.u8() + 1),
]
})
})
.fold(initial_top, |top, (current_xy, lefts)| {
let val = lefts
.iter()
.copied()
.into_iter()
.map(|y| RoomXY { x: left_x, y })
.map(|xy| cm.get(xy))
.min()
Expand Down Expand Up @@ -155,13 +160,18 @@ pub fn chebyshev_distance_transform_from_cost_matrix(mut cm: LocalCostMatrix) ->
initial_bottom,
);
let final_bottom = range_exclusive(zero, forty_nine)
.rev()
.map(|y| RoomXY { x: current_x, y })
.zip(windowing_vec.windows(3).rev())
.fold(initial_bottom, |bottom, (current_xy, rights)| {
.map(|y| {
(RoomXY { x: current_x, y }, unsafe {
[
RoomCoordinate::unchecked_new(y.u8() - 1),
y,
RoomCoordinate::unchecked_new(y.u8() + 1),
]
})
})
.rfold(initial_bottom, |bottom, (current_xy, rights)| {
let val = rights
.iter()
.copied()
.into_iter()
.map(|y| RoomXY { x: right_x, y })
.map(|xy| cm.get(xy))
.min()
Expand Down

0 comments on commit ea510a3

Please sign in to comment.