Skip to content

Commit

Permalink
Eliminate some vec clones
Browse files Browse the repository at this point in the history
  • Loading branch information
QuarticCat committed Sep 30, 2022
1 parent 69b9214 commit 827e26d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
32 changes: 19 additions & 13 deletions src/diff/dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{cmp::Reverse, env};

use crate::{
diff::changes::ChangeMap,
diff::graph::{get_set_neighbours, populate_change_map, Edge, Vertex},
diff::graph::{populate_change_map, set_neighbours, Edge, Vertex},
parse::syntax::Syntax,
};
use bumpalo::Bump;
Expand Down Expand Up @@ -40,18 +40,22 @@ fn shortest_vertex_path<'a, 'b>(
break current;
}

for neighbour in &get_set_neighbours(current, vertex_arena, &mut seen) {
let (edge, next) = neighbour;
let distance_to_next = distance + edge.cost();
set_neighbours(current, vertex_arena, &mut seen);

let found_shorter_route = match next.predecessor.get() {
Some((prev_shortest, _)) => distance_to_next < prev_shortest,
None => true,
};
if let Some(neighbors) = &*current.neighbours.borrow() {
for neighbour in neighbors {
let (edge, next) = neighbour;
let distance_to_next = distance + edge.cost();

if found_shorter_route {
next.predecessor.replace(Some((distance_to_next, current)));
heap.push(Reverse(distance_to_next), next);
let found_shorter_route = match next.predecessor.get() {
Some((prev_shortest, _)) => distance_to_next < prev_shortest,
None => true,
};

if found_shorter_route {
next.predecessor.replace(Some((distance_to_next, current)));
heap.push(Reverse(distance_to_next), next);
}
}
}

Expand Down Expand Up @@ -219,9 +223,11 @@ pub fn mark_syntax<'a>(
.map(|x| {
format!(
"{:20} {:20} --- {:3} {:?}",
x.1.lhs_syntax.get_ref()
x.1.lhs_syntax
.get_ref()
.map_or_else(|| "None".into(), Syntax::dbg_content),
x.1.rhs_syntax.get_ref()
x.1.rhs_syntax
.get_ref()
.map_or_else(|| "None".into(), Syntax::dbg_content),
x.0.cost(),
x.0,
Expand Down
12 changes: 5 additions & 7 deletions src/diff/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,13 @@ fn looks_like_punctuation(content: &str) -> bool {

/// Compute the neighbours of `v` if we haven't previously done so,
/// write them to the .neighbours cell inside `v`, and return them.
pub fn get_set_neighbours<'syn, 'b>(
pub fn set_neighbours<'syn, 'b>(
v: &Vertex<'syn, 'b>,
alloc: &'b Bump,
seen: &mut FxHashMap<&Vertex<'syn, 'b>, Vec<&'b Vertex<'syn, 'b>>>,
) -> Vec<(Edge, &'b Vertex<'syn, 'b>)> {
match &*v.neighbours.borrow() {
Some(neighbours) => return neighbours.clone(),
None => {}
) {
if v.neighbours.borrow().is_some() {
return;
}

let mut res: Vec<(Edge, &Vertex)> = vec![];
Expand Down Expand Up @@ -734,8 +733,7 @@ pub fn get_set_neighbours<'syn, 'b>(
"Must always find some next steps if node is not the end"
);

v.neighbours.replace(Some(res.clone()));
res
v.neighbours.replace(Some(res));
}

pub fn populate_change_map<'a, 'b>(
Expand Down

0 comments on commit 827e26d

Please sign in to comment.