From 319f6074761421b797db71bf10f6171516e3d92a Mon Sep 17 00:00:00 2001 From: Yiyu Lin Date: Wed, 4 Jan 2023 20:20:36 +0800 Subject: [PATCH] chore(cli,ext,rt): remove some unnecessary `clone` or `malloc` (#17261) --- cli/build.rs | 2 +- cli/file_fetcher.rs | 6 +++--- cli/tools/check.rs | 18 +++++++++++++----- cli/tsc/diagnostics.rs | 4 ++-- ext/web/blob.rs | 9 +++------ runtime/ops/web_worker/sync_fetch.rs | 2 +- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/cli/build.rs b/cli/build.rs index 5ef2296e82..faeaed0732 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -1,11 +1,11 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use deno_core::Extension; use std::env; use std::path::Path; use std::path::PathBuf; use deno_core::snapshot_util::*; +use deno_core::Extension; use deno_runtime::deno_cache::SqliteBackedCache; use deno_runtime::permissions::Permissions; use deno_runtime::*; diff --git a/cli/file_fetcher.rs b/cli/file_fetcher.rs index b8ee82a80b..cc83e6f5f9 100644 --- a/cli/file_fetcher.rs +++ b/cli/file_fetcher.rs @@ -90,7 +90,7 @@ fn fetch_local(specifier: &ModuleSpecifier) -> Result { let local = specifier.to_file_path().map_err(|_| { uri_error(format!("Invalid file path.\n Specifier: {}", specifier)) })?; - let bytes = fs::read(local.clone())?; + let bytes = fs::read(&local)?; let charset = text_encoding::detect_charset(&bytes).to_string(); let source = get_source_from_bytes(bytes, Some(charset))?; let media_type = MediaType::from(specifier); @@ -359,7 +359,7 @@ impl FileFetcher { let blob = { let blob_store = self.blob_store.borrow(); blob_store - .get_object_url(specifier.clone())? + .get_object_url(specifier.clone()) .ok_or_else(|| { custom_error( "NotFound", @@ -525,7 +525,7 @@ impl FileFetcher { CacheSetting::ReloadSome(list) => { let mut url = specifier.clone(); url.set_fragment(None); - if list.contains(&url.as_str().to_string()) { + if list.iter().any(|x| x == url.as_str()) { return false; } url.set_query(None); diff --git a/cli/tools/check.rs b/cli/tools/check.rs index e3fb664ad5..0c57af0b0a 100644 --- a/cli/tools/check.rs +++ b/cli/tools/check.rs @@ -79,10 +79,10 @@ pub fn check( let root_names = get_tsc_roots(&segment_graph_data, check_js); if options.log_checks { for (root, _) in roots { - let root_str = root.to_string(); + let root_str = root.as_str(); // `$deno` specifiers are internal, don't print them. if !root_str.contains("$deno") { - log::info!("{} {}", colors::green("Check"), root); + log::info!("{} {}", colors::green("Check"), root_str); } } } @@ -116,12 +116,20 @@ pub fn check( let diagnostics = if options.type_check_mode == TypeCheckMode::Local { response.diagnostics.filter(|d| { if let Some(file_name) = &d.file_name { - !file_name.starts_with("http") - && ModuleSpecifier::parse(file_name) + if !file_name.starts_with("http") { + if ModuleSpecifier::parse(file_name) .map(|specifier| !npm_resolver.in_npm_package(&specifier)) .unwrap_or(true) + { + Some(d.clone()) + } else { + None + } + } else { + None + } } else { - true + Some(d.clone()) } }) } else { diff --git a/cli/tsc/diagnostics.rs b/cli/tsc/diagnostics.rs index 4cd6564cb8..fb7304c9be 100644 --- a/cli/tsc/diagnostics.rs +++ b/cli/tsc/diagnostics.rs @@ -340,9 +340,9 @@ impl Diagnostics { /// returns `true` are included. pub fn filter

(&self, predicate: P) -> Self where - P: FnMut(&Diagnostic) -> bool, + P: FnMut(&Diagnostic) -> Option, { - let diagnostics = self.0.clone().into_iter().filter(predicate).collect(); + let diagnostics = self.0.iter().filter_map(predicate).collect(); Self(diagnostics) } diff --git a/ext/web/blob.rs b/ext/web/blob.rs index 7da42e178c..24cd13454e 100644 --- a/ext/web/blob.rs +++ b/ext/web/blob.rs @@ -47,13 +47,10 @@ impl BlobStore { parts.remove(id) } - pub fn get_object_url( - &self, - mut url: Url, - ) -> Result>, AnyError> { + pub fn get_object_url(&self, mut url: Url) -> Option> { let blob_store = self.object_urls.lock(); url.set_fragment(None); - Ok(blob_store.get(&url).cloned()) + blob_store.get(&url).cloned() } pub fn insert_object_url( @@ -285,7 +282,7 @@ pub fn op_blob_from_object_url( let blob_store = state.try_borrow::().ok_or_else(|| { type_error("Blob URLs are not supported in this context.") })?; - if let Some(blob) = blob_store.get_object_url(url)? { + if let Some(blob) = blob_store.get_object_url(url) { let parts = blob .parts .iter() diff --git a/runtime/ops/web_worker/sync_fetch.rs b/runtime/ops/web_worker/sync_fetch.rs index f96a01a5a1..9d356f5ee5 100644 --- a/runtime/ops/web_worker/sync_fetch.rs +++ b/runtime/ops/web_worker/sync_fetch.rs @@ -107,7 +107,7 @@ pub fn op_worker_sync_fetch( } "blob" => { let blob = - blob_store.get_object_url(script_url)?.ok_or_else(|| { + blob_store.get_object_url(script_url).ok_or_else(|| { type_error("Blob for the given URL not found.") })?;