perf: use HashSet to filter out tags which are not in both collections

This commit is contained in:
Nikita Revenco 2024-11-14 16:05:29 +00:00
parent c3a3e4803c
commit dd3251da46

View File

@ -1,4 +1,4 @@
use std::fmt::Display; use std::{collections::HashSet, fmt::Display};
use crate::{ use crate::{
graphemes::next_grapheme_boundary, graphemes::next_grapheme_boundary,
@ -479,23 +479,22 @@ fn find_nth_nearest_tag(
} }
} }
// only consider the tags which are common in both vectors // only consider the tags which are in both collections.
let backward_tags: Vec<(Range, String)> = backward_tags let backward_tag_names: HashSet<_> = backward_tags.iter().map(|(_, tag)| tag.clone()).collect();
.into_iter() let forward_tag_names: HashSet<_> = forward_tags.iter().map(|(_, tag)| tag.clone()).collect();
.filter(|(_, backward_tag_name)| {
forward_tags let common_tags: HashSet<_> = backward_tag_names
.iter() .intersection(&forward_tag_names)
.any(|(_, forward_tag_name)| backward_tag_name == forward_tag_name)
})
.collect(); .collect();
let forward_tags: Vec<(Range, String)> = forward_tags let backward_tags: Vec<_> = backward_tags
.into_iter() .into_iter()
.filter(|(_, forward_tag_name)| { .filter(|(_, tag)| common_tags.contains(tag))
backward_tags .collect();
.iter()
.any(|(_, backward_tag_name)| forward_tag_name == backward_tag_name) let forward_tags: Vec<_> = forward_tags
}) .into_iter()
.filter(|(_, tag)| common_tags.contains(tag))
.collect(); .collect();
// improperly ordered tags such as <div> <span> </div> </span> are ignored completely // improperly ordered tags such as <div> <span> </div> </span> are ignored completely