Skip to content

Commit 1722228

Browse files
committed
Simplify set_neighbours by extracting a lambda
1 parent cb1c3e0 commit 1722228

File tree

1 file changed

+88
-117
lines changed

1 file changed

+88
-117
lines changed

src/diff/graph.rs

Lines changed: 88 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ impl Edge {
407407
type SeenMap<'syn, 'b> =
408408
FxHashMap<&'b Vertex<'syn, 'b>, (Option<&'b Vertex<'syn, 'b>>, Option<&'b Vertex<'syn, 'b>>)>;
409409

410+
#[inline(never)]
410411
fn allocate_if_new<'syn, 'b>(
411412
v: Vertex<'syn, 'b>,
412413
alloc: &'b Bump,
@@ -455,6 +456,11 @@ pub fn set_neighbours<'syn, 'b>(
455456

456457
let mut res: Vec<(Edge, &Vertex)> = vec![];
457458

459+
let mut add_neighbor = std::convert::identity(
460+
#[inline(always)]
461+
|edge, vertex| res.push((edge, allocate_if_new(vertex, alloc, seen))),
462+
);
463+
458464
if v.lhs_syntax.is_id() && v.rhs_syntax.is_id() {
459465
if let Some((lhs_parent, rhs_parent, lhs_parents_next, rhs_parents_next)) =
460466
try_pop_both(&v.lhs_parents, &v.rhs_parents)
@@ -463,20 +469,16 @@ pub fn set_neighbours<'syn, 'b>(
463469
// move up to the parent node.
464470

465471
// Continue from sibling of parent.
466-
res.push((
472+
add_neighbor(
467473
ExitDelimiterBoth,
468-
allocate_if_new(
469-
Vertex {
470-
lhs_syntax: next_sibling_syntax(lhs_parent),
471-
rhs_syntax: next_sibling_syntax(rhs_parent),
472-
lhs_parents: lhs_parents_next,
473-
rhs_parents: rhs_parents_next,
474-
..Vertex::default()
475-
},
476-
alloc,
477-
seen,
478-
),
479-
));
474+
Vertex {
475+
lhs_syntax: next_sibling_syntax(lhs_parent),
476+
rhs_syntax: next_sibling_syntax(rhs_parent),
477+
lhs_parents: lhs_parents_next,
478+
rhs_parents: rhs_parents_next,
479+
..Vertex::default()
480+
},
481+
);
480482
}
481483
}
482484

@@ -485,20 +487,16 @@ pub fn set_neighbours<'syn, 'b>(
485487
// Move to next after LHS parent.
486488

487489
// Continue from sibling of parent.
488-
res.push((
490+
add_neighbor(
489491
ExitDelimiterLHS,
490-
allocate_if_new(
491-
Vertex {
492-
lhs_syntax: next_sibling_syntax(lhs_parent),
493-
rhs_syntax: v.rhs_syntax,
494-
lhs_parents: lhs_parents_next,
495-
rhs_parents: v.rhs_parents.clone(),
496-
..Vertex::default()
497-
},
498-
alloc,
499-
seen,
500-
),
501-
));
492+
Vertex {
493+
lhs_syntax: next_sibling_syntax(lhs_parent),
494+
rhs_syntax: v.rhs_syntax,
495+
lhs_parents: lhs_parents_next,
496+
rhs_parents: v.rhs_parents.clone(),
497+
..Vertex::default()
498+
},
499+
);
502500
}
503501
}
504502

@@ -507,20 +505,16 @@ pub fn set_neighbours<'syn, 'b>(
507505
// Move to next after RHS parent.
508506

509507
// Continue from sibling of parent.
510-
res.push((
508+
add_neighbor(
511509
ExitDelimiterRHS,
512-
allocate_if_new(
513-
Vertex {
514-
lhs_syntax: v.lhs_syntax,
515-
rhs_syntax: next_sibling_syntax(rhs_parent),
516-
lhs_parents: v.lhs_parents.clone(),
517-
rhs_parents: rhs_parents_next,
518-
..Vertex::default()
519-
},
520-
alloc,
521-
seen,
522-
),
523-
));
510+
Vertex {
511+
lhs_syntax: v.lhs_syntax,
512+
rhs_syntax: next_sibling_syntax(rhs_parent),
513+
lhs_parents: v.lhs_parents.clone(),
514+
rhs_parents: rhs_parents_next,
515+
..Vertex::default()
516+
},
517+
);
524518
}
525519
}
526520

@@ -531,20 +525,16 @@ pub fn set_neighbours<'syn, 'b>(
531525
.unsigned_abs();
532526

533527
// Both nodes are equal, the happy case.
534-
res.push((
528+
add_neighbor(
535529
UnchangedNode { depth_difference },
536-
allocate_if_new(
537-
Vertex {
538-
lhs_syntax: next_sibling_syntax(lhs_syntax),
539-
rhs_syntax: next_sibling_syntax(rhs_syntax),
540-
lhs_parents: v.lhs_parents.clone(),
541-
rhs_parents: v.rhs_parents.clone(),
542-
..Vertex::default()
543-
},
544-
alloc,
545-
seen,
546-
),
547-
));
530+
Vertex {
531+
lhs_syntax: next_sibling_syntax(lhs_syntax),
532+
rhs_syntax: next_sibling_syntax(rhs_syntax),
533+
lhs_parents: v.lhs_parents.clone(),
534+
rhs_parents: v.rhs_parents.clone(),
535+
..Vertex::default()
536+
},
537+
);
548538
}
549539

550540
if let (
@@ -572,20 +562,16 @@ pub fn set_neighbours<'syn, 'b>(
572562
- rhs_syntax.num_ancestors() as i32)
573563
.unsigned_abs();
574564

575-
res.push((
565+
add_neighbor(
576566
EnterUnchangedDelimiter { depth_difference },
577-
allocate_if_new(
578-
Vertex {
579-
lhs_syntax: next_child_syntax(lhs_syntax, lhs_children),
580-
rhs_syntax: next_child_syntax(rhs_syntax, rhs_children),
581-
lhs_parents: lhs_parents_next,
582-
rhs_parents: rhs_parents_next,
583-
..Vertex::default()
584-
},
585-
alloc,
586-
seen,
587-
),
588-
));
567+
Vertex {
568+
lhs_syntax: next_child_syntax(lhs_syntax, lhs_children),
569+
rhs_syntax: next_child_syntax(rhs_syntax, rhs_children),
570+
lhs_parents: lhs_parents_next,
571+
rhs_parents: rhs_parents_next,
572+
..Vertex::default()
573+
},
574+
);
589575
}
590576
}
591577

@@ -607,20 +593,17 @@ pub fn set_neighbours<'syn, 'b>(
607593
if lhs_content != rhs_content {
608594
let levenshtein_pct =
609595
(normalized_levenshtein(lhs_content, rhs_content) * 100.0).round() as u8;
610-
res.push((
596+
597+
add_neighbor(
611598
ReplacedComment { levenshtein_pct },
612-
allocate_if_new(
613-
Vertex {
614-
lhs_syntax: next_sibling_syntax(lhs_syntax),
615-
rhs_syntax: next_sibling_syntax(rhs_syntax),
616-
lhs_parents: v.lhs_parents.clone(),
617-
rhs_parents: v.rhs_parents.clone(),
618-
..Vertex::default()
619-
},
620-
alloc,
621-
seen,
622-
),
623-
));
599+
Vertex {
600+
lhs_syntax: next_sibling_syntax(lhs_syntax),
601+
rhs_syntax: next_sibling_syntax(rhs_syntax),
602+
lhs_parents: v.lhs_parents.clone(),
603+
rhs_parents: v.rhs_parents.clone(),
604+
..Vertex::default()
605+
},
606+
);
624607
}
625608
}
626609
}
@@ -629,44 +612,36 @@ pub fn set_neighbours<'syn, 'b>(
629612
match lhs_syntax {
630613
// Step over this novel atom.
631614
Syntax::Atom { content, .. } => {
632-
res.push((
615+
add_neighbor(
633616
NovelAtomLHS {
634617
// TODO: should this apply if prev is a parent
635618
// node rather than a sibling?
636619
contiguous: lhs_syntax.prev_is_contiguous(),
637620
probably_punctuation: looks_like_punctuation(content),
638621
},
639-
allocate_if_new(
640-
Vertex {
641-
lhs_syntax: next_sibling_syntax(lhs_syntax),
642-
rhs_syntax: v.rhs_syntax,
643-
lhs_parents: v.lhs_parents.clone(),
644-
rhs_parents: v.rhs_parents.clone(),
645-
..Vertex::default()
646-
},
647-
alloc,
648-
seen,
649-
),
650-
));
622+
Vertex {
623+
lhs_syntax: next_sibling_syntax(lhs_syntax),
624+
rhs_syntax: v.rhs_syntax,
625+
lhs_parents: v.lhs_parents.clone(),
626+
rhs_parents: v.rhs_parents.clone(),
627+
..Vertex::default()
628+
},
629+
);
651630
}
652631
// Step into this partially/fully novel list.
653632
Syntax::List { children, .. } => {
654-
res.push((
633+
add_neighbor(
655634
EnterNovelDelimiterLHS {
656635
contiguous: lhs_syntax.prev_is_contiguous(),
657636
},
658-
allocate_if_new(
659-
Vertex {
660-
lhs_syntax: next_child_syntax(lhs_syntax, children),
661-
rhs_syntax: v.rhs_syntax,
662-
lhs_parents: push_either(&v.lhs_parents, lhs_syntax),
663-
rhs_parents: v.rhs_parents.clone(),
664-
..Vertex::default()
665-
},
666-
alloc,
667-
seen,
668-
),
669-
));
637+
Vertex {
638+
lhs_syntax: next_child_syntax(lhs_syntax, children),
639+
rhs_syntax: v.rhs_syntax,
640+
lhs_parents: push_either(&v.lhs_parents, lhs_syntax),
641+
rhs_parents: v.rhs_parents.clone(),
642+
..Vertex::default()
643+
},
644+
);
670645
}
671646
}
672647
}
@@ -695,22 +670,18 @@ pub fn set_neighbours<'syn, 'b>(
695670
}
696671
// Step into this partially/fully novel list.
697672
Syntax::List { children, .. } => {
698-
res.push((
673+
add_neighbor(
699674
EnterNovelDelimiterRHS {
700675
contiguous: rhs_syntax.prev_is_contiguous(),
701676
},
702-
allocate_if_new(
703-
Vertex {
704-
lhs_syntax: v.lhs_syntax,
705-
rhs_syntax: next_child_syntax(rhs_syntax, children),
706-
lhs_parents: v.lhs_parents.clone(),
707-
rhs_parents: push_either(&v.rhs_parents, rhs_syntax),
708-
..Vertex::default()
709-
},
710-
alloc,
711-
seen,
712-
),
713-
));
677+
Vertex {
678+
lhs_syntax: v.lhs_syntax,
679+
rhs_syntax: next_child_syntax(rhs_syntax, children),
680+
lhs_parents: v.lhs_parents.clone(),
681+
rhs_parents: push_either(&v.rhs_parents, rhs_syntax),
682+
..Vertex::default()
683+
},
684+
);
714685
}
715686
}
716687
}

0 commit comments

Comments
 (0)