Skip to content

Commit

Permalink
chore: reduce allocations in a few places (#27288)
Browse files Browse the repository at this point in the history
Probably doesn't have much impact. I didn't measure any of these, but
reducing allocations should always be good.
  • Loading branch information
dsherret authored and bartlomieju committed Dec 11, 2024
1 parent d410358 commit c402448
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 26 deletions.
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ boxed_error = "0.2.2"
brotli = "6.0.0"
bytes = "1.4.0"
cache_control = "=0.2.0"
capacity_builder = "0.1.0"
cbc = { version = "=0.1.2", features = ["alloc"] }
# Note: Do not use the "clock" feature of chrono, as it links us to CoreFoundation on macOS.
# Instead use util::time::utc_now()
Expand Down
5 changes: 3 additions & 2 deletions cli/util/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::borrow::Cow;
use std::fmt::Write;
use std::path::Path;
use std::path::PathBuf;

Expand Down Expand Up @@ -58,8 +59,8 @@ pub fn get_atomic_file_path(file_path: &Path) -> PathBuf {
}

fn gen_rand_path_component() -> String {
(0..4).fold(String::new(), |mut output, _| {
output.push_str(&format!("{:02x}", rand::random::<u8>()));
(0..4).fold(String::with_capacity(8), |mut output, _| {
write!(&mut output, "{:02x}", rand::random::<u8>()).unwrap();
output
})
}
Expand Down
9 changes: 6 additions & 3 deletions cli/util/progress_bar/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::fmt::Write;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::time::Duration;
Expand Down Expand Up @@ -81,12 +82,14 @@ impl ProgressBarRenderer for BarProgressBarRenderer {
let elapsed_text = get_elapsed_text(data.duration);
let mut text = String::new();
if !display_entry.message.is_empty() {
text.push_str(&format!(
"{} {}{}\n",
writeln!(
&mut text,
"{} {}{}",
colors::green("Download"),
display_entry.message,
bytes_text,
));
)
.unwrap();
}
text.push_str(&elapsed_text);
let max_width = (data.terminal_width as i32 - 5).clamp(10, 75) as usize;
Expand Down
5 changes: 3 additions & 2 deletions resolvers/npm_cache/tarball_extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ fn get_atomic_dir_path(file_path: &Path) -> PathBuf {
}

fn gen_rand_path_component() -> String {
(0..4).fold(String::new(), |mut output, _| {
output.push_str(&format!("{:02x}", rand::random::<u8>()));
use std::fmt::Write;
(0..4).fold(String::with_capacity(8), |mut output, _| {
write!(&mut output, "{:02x}", rand::random::<u8>()).unwrap();
output
})
}
Expand Down
1 change: 1 addition & 0 deletions runtime/permissions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ name = "deno_permissions"
path = "lib.rs"

[dependencies]
capacity_builder.workspace = true
deno_core.workspace = true
deno_path_util.workspace = true
deno_terminal.workspace = true
Expand Down
45 changes: 28 additions & 17 deletions runtime/permissions/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use capacity_builder::StringBuilder;
use deno_core::parking_lot::Mutex;
use deno_core::serde::de;
use deno_core::serde::Deserialize;
Expand Down Expand Up @@ -179,13 +180,18 @@ impl PermissionState {
(Ok(()), false, false)
}
PermissionState::Prompt if prompt => {
let msg = format!(
"{} access{}",
name,
info()
.map(|info| { format!(" to {info}") })
.unwrap_or_default(),
);
let msg = {
let info = info();
StringBuilder::build(|builder| {
builder.append(name);
builder.append(" access");
if let Some(info) = &info {
builder.append(" to ");
builder.append(info);
}
})
.unwrap()
};
match permission_prompt(&msg, name, api_name, true) {
PromptResponse::Allow => {
Self::log_perm_access(name, info);
Expand Down Expand Up @@ -344,11 +350,11 @@ pub trait QueryDescriptor: Debug {
fn overlaps_deny(&self, other: &Self::DenyDesc) -> bool;
}

fn format_display_name(display_name: Cow<str>) -> String {
fn format_display_name(display_name: Cow<str>) -> Cow<str> {
if display_name.starts_with('<') && display_name.ends_with('>') {
display_name.into_owned()
display_name
} else {
format!("\"{}\"", display_name)
Cow::Owned(format!("\"{}\"", display_name))
}
}

Expand Down Expand Up @@ -424,7 +430,7 @@ impl<TQuery: QueryDescriptor> UnaryPermission<TQuery> {
.check2(
TQuery::flag_name(),
api_name,
|| desc.map(|d| format_display_name(d.display_name())),
|| desc.map(|d| format_display_name(d.display_name()).into_owned()),
self.prompt,
);
if prompted {
Expand Down Expand Up @@ -487,12 +493,17 @@ impl<TQuery: QueryDescriptor> UnaryPermission<TQuery> {
if !self.prompt {
return PermissionState::Denied;
}
let mut message = String::with_capacity(40);
message.push_str(&format!("{} access", TQuery::flag_name()));
if let Some(desc) = desc {
message
.push_str(&format!(" to {}", format_display_name(desc.display_name())));
}
let maybe_formatted_display_name =
desc.map(|d| format_display_name(d.display_name()));
let message = StringBuilder::build(|builder| {
builder.append(TQuery::flag_name());
builder.append(" access");
if let Some(display_name) = &maybe_formatted_display_name {
builder.append(" to ");
builder.append(display_name)
}
})
.unwrap();
match permission_prompt(
&message,
TQuery::flag_name(),
Expand Down

0 comments on commit c402448

Please sign in to comment.