Skip to content

Commit 827e26d

Browse files
committed
Eliminate some vec clones
1 parent 69b9214 commit 827e26d

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/diff/dijkstra.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{cmp::Reverse, env};
55

66
use crate::{
77
diff::changes::ChangeMap,
8-
diff::graph::{get_set_neighbours, populate_change_map, Edge, Vertex},
8+
diff::graph::{populate_change_map, set_neighbours, Edge, Vertex},
99
parse::syntax::Syntax,
1010
};
1111
use bumpalo::Bump;
@@ -40,18 +40,22 @@ fn shortest_vertex_path<'a, 'b>(
4040
break current;
4141
}
4242

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

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

52-
if found_shorter_route {
53-
next.predecessor.replace(Some((distance_to_next, current)));
54-
heap.push(Reverse(distance_to_next), next);
50+
let found_shorter_route = match next.predecessor.get() {
51+
Some((prev_shortest, _)) => distance_to_next < prev_shortest,
52+
None => true,
53+
};
54+
55+
if found_shorter_route {
56+
next.predecessor.replace(Some((distance_to_next, current)));
57+
heap.push(Reverse(distance_to_next), next);
58+
}
5559
}
5660
}
5761

@@ -219,9 +223,11 @@ pub fn mark_syntax<'a>(
219223
.map(|x| {
220224
format!(
221225
"{:20} {:20} --- {:3} {:?}",
222-
x.1.lhs_syntax.get_ref()
226+
x.1.lhs_syntax
227+
.get_ref()
223228
.map_or_else(|| "None".into(), Syntax::dbg_content),
224-
x.1.rhs_syntax.get_ref()
229+
x.1.rhs_syntax
230+
.get_ref()
225231
.map_or_else(|| "None".into(), Syntax::dbg_content),
226232
x.0.cost(),
227233
x.0,

src/diff/graph.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,14 +467,13 @@ fn looks_like_punctuation(content: &str) -> bool {
467467

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

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

737-
v.neighbours.replace(Some(res.clone()));
738-
res
736+
v.neighbours.replace(Some(res));
739737
}
740738

741739
pub fn populate_change_map<'a, 'b>(

0 commit comments

Comments
 (0)