1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 16:49:18 -05:00

chore(cli,ext,rt): remove some unnecessary clone or malloc (#17261)

This commit is contained in:
Yiyu Lin 2023-01-04 20:20:36 +08:00 committed by GitHub
parent 2da882137e
commit 319f607476
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 18 deletions

View file

@ -1,11 +1,11 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use deno_core::Extension;
use std::env; use std::env;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use deno_core::snapshot_util::*; use deno_core::snapshot_util::*;
use deno_core::Extension;
use deno_runtime::deno_cache::SqliteBackedCache; use deno_runtime::deno_cache::SqliteBackedCache;
use deno_runtime::permissions::Permissions; use deno_runtime::permissions::Permissions;
use deno_runtime::*; use deno_runtime::*;

View file

@ -90,7 +90,7 @@ fn fetch_local(specifier: &ModuleSpecifier) -> Result<File, AnyError> {
let local = specifier.to_file_path().map_err(|_| { let local = specifier.to_file_path().map_err(|_| {
uri_error(format!("Invalid file path.\n Specifier: {}", specifier)) 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 charset = text_encoding::detect_charset(&bytes).to_string();
let source = get_source_from_bytes(bytes, Some(charset))?; let source = get_source_from_bytes(bytes, Some(charset))?;
let media_type = MediaType::from(specifier); let media_type = MediaType::from(specifier);
@ -359,7 +359,7 @@ impl FileFetcher {
let blob = { let blob = {
let blob_store = self.blob_store.borrow(); let blob_store = self.blob_store.borrow();
blob_store blob_store
.get_object_url(specifier.clone())? .get_object_url(specifier.clone())
.ok_or_else(|| { .ok_or_else(|| {
custom_error( custom_error(
"NotFound", "NotFound",
@ -525,7 +525,7 @@ impl FileFetcher {
CacheSetting::ReloadSome(list) => { CacheSetting::ReloadSome(list) => {
let mut url = specifier.clone(); let mut url = specifier.clone();
url.set_fragment(None); url.set_fragment(None);
if list.contains(&url.as_str().to_string()) { if list.iter().any(|x| x == url.as_str()) {
return false; return false;
} }
url.set_query(None); url.set_query(None);

View file

@ -79,10 +79,10 @@ pub fn check(
let root_names = get_tsc_roots(&segment_graph_data, check_js); let root_names = get_tsc_roots(&segment_graph_data, check_js);
if options.log_checks { if options.log_checks {
for (root, _) in roots { for (root, _) in roots {
let root_str = root.to_string(); let root_str = root.as_str();
// `$deno` specifiers are internal, don't print them. // `$deno` specifiers are internal, don't print them.
if !root_str.contains("$deno") { 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 { let diagnostics = if options.type_check_mode == TypeCheckMode::Local {
response.diagnostics.filter(|d| { response.diagnostics.filter(|d| {
if let Some(file_name) = &d.file_name { if let Some(file_name) = &d.file_name {
!file_name.starts_with("http") if !file_name.starts_with("http") {
&& ModuleSpecifier::parse(file_name) if ModuleSpecifier::parse(file_name)
.map(|specifier| !npm_resolver.in_npm_package(&specifier)) .map(|specifier| !npm_resolver.in_npm_package(&specifier))
.unwrap_or(true) .unwrap_or(true)
{
Some(d.clone())
} else {
None
}
} else {
None
}
} else { } else {
true Some(d.clone())
} }
}) })
} else { } else {

View file

@ -340,9 +340,9 @@ impl Diagnostics {
/// returns `true` are included. /// returns `true` are included.
pub fn filter<P>(&self, predicate: P) -> Self pub fn filter<P>(&self, predicate: P) -> Self
where where
P: FnMut(&Diagnostic) -> bool, P: FnMut(&Diagnostic) -> Option<Diagnostic>,
{ {
let diagnostics = self.0.clone().into_iter().filter(predicate).collect(); let diagnostics = self.0.iter().filter_map(predicate).collect();
Self(diagnostics) Self(diagnostics)
} }

View file

@ -47,13 +47,10 @@ impl BlobStore {
parts.remove(id) parts.remove(id)
} }
pub fn get_object_url( pub fn get_object_url(&self, mut url: Url) -> Option<Arc<Blob>> {
&self,
mut url: Url,
) -> Result<Option<Arc<Blob>>, AnyError> {
let blob_store = self.object_urls.lock(); let blob_store = self.object_urls.lock();
url.set_fragment(None); url.set_fragment(None);
Ok(blob_store.get(&url).cloned()) blob_store.get(&url).cloned()
} }
pub fn insert_object_url( pub fn insert_object_url(
@ -285,7 +282,7 @@ pub fn op_blob_from_object_url(
let blob_store = state.try_borrow::<BlobStore>().ok_or_else(|| { let blob_store = state.try_borrow::<BlobStore>().ok_or_else(|| {
type_error("Blob URLs are not supported in this context.") 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 let parts = blob
.parts .parts
.iter() .iter()

View file

@ -107,7 +107,7 @@ pub fn op_worker_sync_fetch(
} }
"blob" => { "blob" => {
let 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.") type_error("Blob for the given URL not found.")
})?; })?;