Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce some clones and pre-allocate some collections #215

Merged
merged 3 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified lib/deno_graph_wasm_bg.wasm
Binary file not shown.
27 changes: 14 additions & 13 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,15 +769,16 @@ impl ModuleGraph {
/// returning the "final" module.
pub fn resolve(&self, specifier: &ModuleSpecifier) -> ModuleSpecifier {
let mut redirected_specifier = specifier;
let mut seen = HashSet::new();
let max_redirects = 10;
let mut seen = HashSet::with_capacity(max_redirects);
seen.insert(redirected_specifier);
while let Some(specifier) = self.redirects.get(redirected_specifier) {
if !seen.insert(specifier) {
eprintln!("An infinite loop of redirections detected.\n Original specifier: {}", specifier);
break;
}
redirected_specifier = specifier;
if seen.len() > 5 {
if seen.len() >= max_redirects {
eprintln!("An excessive number of redirections detected.\n Original specifier: {}", specifier);
break;
}
Expand Down Expand Up @@ -916,21 +917,20 @@ impl ModuleGraph {
}

fn validate(&self, types_only: bool) -> Result<(), ModuleGraphError> {
fn validate<F>(
specifier: &ModuleSpecifier,
fn validate<'a>(
specifier: &'a ModuleSpecifier,
maybe_range: Option<&Range>,
types_only: bool,
is_type: bool,
seen: &mut HashSet<ModuleSpecifier>,
get_module: &F,
) -> Result<(), ModuleGraphError>
where
F: Fn(&ModuleSpecifier) -> Result<Option<Module>, ModuleGraphError>,
{
seen: &mut HashSet<&'a ModuleSpecifier>,
get_module: &impl Fn(
&ModuleSpecifier,
) -> Result<Option<&'a Module>, ModuleGraphError>,
) -> Result<(), ModuleGraphError> {
if seen.contains(specifier) {
return Ok(());
}
seen.insert(specifier.clone());
seen.insert(specifier);
let should_error = (is_type && types_only) || (!is_type && !types_only);
match get_module(specifier) {
Ok(Some(module)) => {
Expand Down Expand Up @@ -998,10 +998,11 @@ impl ModuleGraph {
}
}

let mut seen = HashSet::new();
let mut seen =
HashSet::with_capacity(self.module_slots.len() + self.redirects.len());
for root in &self.roots {
validate(root, None, types_only, false, &mut seen, &|s| {
self.try_get(s).map(|o| o.cloned())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed heavy.

self.try_get(s)
})?;
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn load_data_url(
let (bytes, _) = url
.decode_to_vec()
.map_err(|_| anyhow!("Unable to decode data url."))?;
let mut headers: HashMap<String, String> = HashMap::new();
let mut headers: HashMap<String, String> = HashMap::with_capacity(1);
headers.insert("content-type".to_string(), url.mime_type().to_string());
let mut content = String::from_utf8(bytes)?;
strip_bom_mut(&mut content);
Expand Down