Skip to content

Commit

Permalink
refactor: remove expensive call to find_matching_entry
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Jul 27, 2024
1 parent c495920 commit 7d4ba15
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
18 changes: 14 additions & 4 deletions bin/src/control.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
error::Error,
fmt::{self, Display}, time::Instant,
fmt::{self, Display},
time::Instant,
};

use matching::MatchingEntry;
Expand Down Expand Up @@ -79,17 +80,26 @@ pub fn run_tool_on_merge_scenario(
let start = Instant::now();
log::info!("Started calculation of matchings between left and base");
let matchings_left_base = matching::calculate_matchings(&left_tree, &base_tree);
log::info!("Finished calculation of matchings between left and base in {:?}", start.elapsed());
log::info!(
"Finished calculation of matchings between left and base in {:?}",
start.elapsed()
);

let start = Instant::now();
log::info!("Started calculation of matchings between right and base");
let matchings_right_base = matching::calculate_matchings(&right_tree, &base_tree);
log::info!("Finished calculation of matchings between right and base in {:?}", start.elapsed());
log::info!(
"Finished calculation of matchings between right and base in {:?}",
start.elapsed()
);

let start = Instant::now();
log::info!("Started calculation of matchings between left and right");
let matchings_left_right = matching::calculate_matchings(&left_tree, &right_tree);
log::info!("Finished calculation of matchings between left and right in {:?}", start.elapsed());
log::info!(
"Finished calculation of matchings between left and right in {:?}",
start.elapsed()
);

let start = Instant::now();
log::info!("Starting merge of the trees");
Expand Down
14 changes: 3 additions & 11 deletions matching/src/matchings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ impl<'a> Matchings<'a> {
}

pub fn from_single(key: UnorderedPair<&'a CSTNode>, value: MatchingEntry) -> Self {
let mut matching_entries = HashMap::new();
matching_entries.insert(key, value);
Matchings { matching_entries }
Matchings {
matching_entries: HashMap::from([(key, value)]),
}
}

pub fn new(matching_entries: HashMap<UnorderedPair<&'a CSTNode<'a>>, MatchingEntry>) -> Self {
Expand Down Expand Up @@ -56,14 +56,6 @@ impl<'a> Matchings<'a> {
self.matching_entries.get(&UnorderedPair(left, right))
}

pub fn has_bidirectional_matching(
&'a self,
left: &'a CSTNode<'a>,
right: &'a CSTNode<'a>,
) -> bool {
self.find_matching_for(left).is_some() && self.find_matching_for(right).is_some()
}

pub fn extend(&mut self, matchings: Matchings<'a>) {
self.matching_entries.extend(matchings);
}
Expand Down
2 changes: 1 addition & 1 deletion merge/src/merge_terminals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn merge_terminals<'a>(
right: &'a Terminal<'a>,
) -> Result<MergedCSTNode<'a>, MergeError> {
log::trace!("Calling merge terminal");

// Nodes of different kind, early return
if left.kind != right.kind {
return Err(MergeError::NodesWithDifferentKinds(
Expand Down
12 changes: 9 additions & 3 deletions merge/src/ordered_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ pub fn ordered_merge<'a>(
while let (Some(cur_left), Some(cur_right)) = (cur_left_option, cur_right_option) {
let matching_base_left = base_left_matchings.find_matching_for(cur_left);
let matching_base_right = base_right_matchings.find_matching_for(cur_right);
let left_matching_in_right = left_right_matchings.find_matching_for(cur_left);
let right_matching_in_left = left_right_matchings.find_matching_for(cur_right);
let left_matching_in_right = right
.get_children()
.iter()
.find_map(|right_child| left_right_matchings.get_matching_entry(cur_left, right_child));
let right_matching_in_left = left
.get_children()
.iter()
.find_map(|left_child| left_right_matchings.get_matching_entry(left_child, cur_right));
let has_bidirectional_matching_left_right =
left_right_matchings.has_bidirectional_matching(cur_left, cur_right);
left_matching_in_right.is_some() && right_matching_in_left.is_some();

match (
has_bidirectional_matching_left_right,
Expand Down

0 comments on commit 7d4ba15

Please sign in to comment.