Skip to content

Commit

Permalink
[general] remove par_iter which slows down
Browse files Browse the repository at this point in the history
  • Loading branch information
Stunkymonkey committed May 16, 2021
1 parent 8e49ace commit 6ed11cd
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
15 changes: 5 additions & 10 deletions pre/src/dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Dijkstra {
self.heap.push(MinHeapItem::new(start, 0));
}
if self.visited.is_visited(end) {
return self.resolve_path(end, &edges, with_path);
return Some(self.resolve_path(end, &edges, with_path));
}
self.dist[start] = (0, None);
self.reachable.set_visited(start);
Expand Down Expand Up @@ -82,22 +82,17 @@ impl Dijkstra {
self.visited.set_visited(node);
// found end
if node == end {
return self.resolve_path(end, &edges, with_path);
return Some(self.resolve_path(end, &edges, with_path));
}
}
None
}

/// recreate path, of already visited
fn resolve_path(
&self,
end: NodeId,
edges: &[Way],
with_path: bool,
) -> Option<(Vec<NodeId>, usize)> {
fn resolve_path(&self, end: NodeId, edges: &[Way], with_path: bool) -> (Vec<NodeId>, usize) {
let weight = self.dist[end].0;
if !with_path {
return Some((Vec::new(), weight));
return (Vec::new(), weight);
}
let mut path = Vec::with_capacity(self.dist.len() / 2);
let mut current_dist = self.dist[end];
Expand All @@ -106,7 +101,7 @@ impl Dijkstra {
current_dist = self.dist[edges[prev].source];
}
path.reverse();
Some((path, weight))
(path, weight)
}
}

Expand Down
8 changes: 4 additions & 4 deletions pre/src/graph_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::*;
/// get all up edges from one node
#[allow(dead_code)]
pub fn get_edges_from_id(ids: Vec<EdgeId>, edges: &[Way]) -> Vec<Way> {
return ids.par_iter().map(|x| edges[*x]).collect();
return ids.iter().map(|x| edges[*x]).collect();
}

/// get all up edge-ids from one node
Expand All @@ -20,7 +20,7 @@ pub fn get_down_edge_ids(
down_index: &[EdgeId],
) -> Vec<EdgeId> {
let prev: Vec<EdgeId> = (down_offset[node]..down_offset[node + 1]).collect();
return prev.par_iter().map(|x| down_index[*x]).collect();
return prev.iter().map(|x| down_index[*x]).collect();
}

/// get all down edge-ids from one node
Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn get_all_edge_ids(
#[allow(dead_code)]
pub fn get_up_neighbors(node: NodeId, edges: &[Way], up_offset: &[EdgeId]) -> Vec<EdgeId> {
let next = get_up_edge_ids(node, &up_offset);
let mut tmp: Vec<EdgeId> = next.par_iter().map(|x| edges[*x].target).collect();
let mut tmp: Vec<EdgeId> = next.iter().map(|x| edges[*x].target).collect();
tmp.dedup();
tmp
}
Expand All @@ -68,7 +68,7 @@ pub fn get_down_neighbors(
down_index: &[EdgeId],
) -> Vec<EdgeId> {
let prev = get_down_edge_ids(node, &down_offset, &down_index);
let mut tmp: Vec<EdgeId> = prev.par_iter().map(|x| edges[*x].source).collect();
let mut tmp: Vec<EdgeId> = prev.iter().map(|x| edges[*x].source).collect();
tmp.par_sort_unstable();
tmp.dedup();
tmp
Expand Down
2 changes: 2 additions & 0 deletions pre/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ fn get_min_max(nodes: &[Node]) -> GridBounds {
}
}

#[allow(clippy::suspicious_operation_groupings)]
fn get_grid_lat(node: &Node, grid_bounds: &GridBounds) -> usize {
let lat_percent =
(node.latitude - grid_bounds.lat_min) / (grid_bounds.lat_max - grid_bounds.lat_min);
(lat_percent * (LAT_GRID_AMOUNT - 1) as f32) as usize
}

#[allow(clippy::suspicious_operation_groupings)]
fn get_grid_lng(node: &Node, grid_bounds: &GridBounds) -> usize {
let lng_percent =
(node.longitude - grid_bounds.lng_min) / (grid_bounds.lng_max - grid_bounds.lng_min);
Expand Down
4 changes: 2 additions & 2 deletions pre/src/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ pub fn generate_offsets_unstable(
down_offset.resize(amount_nodes + 1, 0);

// generate up edges
let sources: Vec<EdgeId> = edges.par_iter().map(|x| x.source).rev().collect();
let sources: Vec<EdgeId> = edges.iter().map(|x| x.source).rev().collect();
fill_offset(sources, &mut up_offset);

// generate down edges, but without sorting edges
// first collect offsets
let targets: Vec<EdgeId> = edges.par_iter().map(|x| x.target).rev().collect();
let targets: Vec<EdgeId> = edges.iter().map(|x| x.target).rev().collect();
fill_offset(targets, &mut down_offset);
let mut down_index = vec![INVALID_EDGE; edges.len()];
// fill offsets, where not already filled
Expand Down
10 changes: 5 additions & 5 deletions web/src/bidijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use visited_list::*;

#[derive(Clone)]
pub struct Dijkstra {
dist_up: Vec<(NodeId, Option<Weight>)>,
dist_down: Vec<(NodeId, Option<Weight>)>,
dist_up: Vec<(Weight, Option<EdgeId>)>,
dist_down: Vec<(Weight, Option<EdgeId>)>,
visited_up: VisitedList,
visited_down: VisitedList,
heap_up: BinaryHeap<MinHeapItem>,
Expand Down Expand Up @@ -145,7 +145,7 @@ impl Dijkstra {
if meeting_node == INVALID_NODE {
None
} else {
self.resolve_path(meeting_node, best_weight, nodes[meeting_node].rank, &edges)
Some(self.resolve_path(meeting_node, best_weight, nodes[meeting_node].rank, &edges))
}
}

Expand All @@ -156,7 +156,7 @@ impl Dijkstra {
weight: Weight,
meeting_rank: Rank,
edges: &[Way],
) -> Option<(Vec<NodeId>, f32)> {
) -> (Vec<NodeId>, f32) {
assert!(self.visited_up.is_visited(meeting_node));
assert!(self.visited_down.is_visited(meeting_node));

Expand All @@ -174,7 +174,7 @@ impl Dijkstra {
self.walk_down(down_edge.1.unwrap(), false, &mut path, &edges);
}

Some((path, weight as f32 / DIST_MULTIPLICATOR as f32))
(path, weight as f32 / DIST_MULTIPLICATOR as f32)
}

// walk shortcuts from meeting point to end
Expand Down
8 changes: 4 additions & 4 deletions web/src/graph_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::*;
/// get all up edges from one node
#[allow(dead_code)]
pub fn get_edges_from_id(ids: Vec<EdgeId>, edges: &[Way]) -> Vec<Way> {
return ids.par_iter().map(|x| edges[*x]).collect();
return ids.iter().map(|x| edges[*x]).collect();
}

/// get all up edge-ids from one node
Expand All @@ -20,7 +20,7 @@ pub fn get_down_edge_ids(
down_index: &[EdgeId],
) -> Vec<EdgeId> {
let prev: Vec<EdgeId> = (down_offset[node]..down_offset[node + 1]).collect();
return prev.par_iter().map(|x| down_index[*x]).collect();
return prev.iter().map(|x| down_index[*x]).collect();
}

/// get all down edge-ids from one node
Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn get_all_edge_ids(
#[allow(dead_code)]
pub fn get_up_neighbors(node: NodeId, edges: &[Way], up_offset: &[EdgeId]) -> Vec<EdgeId> {
let next = get_up_edge_ids(node, &up_offset);
let mut tmp: Vec<EdgeId> = next.par_iter().map(|x| edges[*x].target).collect();
let mut tmp: Vec<EdgeId> = next.iter().map(|x| edges[*x].target).collect();
tmp.dedup();
tmp
}
Expand All @@ -68,7 +68,7 @@ pub fn get_down_neighbors(
down_index: &[EdgeId],
) -> Vec<EdgeId> {
let prev = get_down_edge_ids(node, &down_offset, &down_index);
let mut tmp: Vec<EdgeId> = prev.par_iter().map(|x| edges[*x].source).collect();
let mut tmp: Vec<EdgeId> = prev.iter().map(|x| edges[*x].source).collect();
tmp.par_sort_unstable();
tmp.dedup();
tmp
Expand Down
2 changes: 2 additions & 0 deletions web/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ fn get_points_from_cells(
result
}

#[allow(clippy::suspicious_operation_groupings)]
fn get_grid_lat(node: &Node, grid_bounds: &GridBounds) -> usize {
let lat_percent =
(node.latitude - grid_bounds.lat_min) / (grid_bounds.lat_max - grid_bounds.lat_min);
(lat_percent * (LAT_GRID_AMOUNT - 1) as f32) as usize
}

#[allow(clippy::suspicious_operation_groupings)]
fn get_grid_lng(node: &Node, grid_bounds: &GridBounds) -> usize {
let lng_percent =
(node.longitude - grid_bounds.lng_min) / (grid_bounds.lng_max - grid_bounds.lng_min);
Expand Down

0 comments on commit 6ed11cd

Please # to comment.