Skip to content

Commit d99b2d6

Browse files
authored
chore: reduce allocations in a few places (#27288)
Probably doesn't have much impact. I didn't measure any of these, but reducing allocations should always be good.
1 parent 1c0f236 commit d99b2d6

File tree

7 files changed

+54
-26
lines changed

7 files changed

+54
-26
lines changed

Cargo.lock

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ boxed_error = "0.2.2"
108108
brotli = "6.0.0"
109109
bytes = "1.4.0"
110110
cache_control = "=0.2.0"
111+
capacity_builder = "0.1.0"
111112
cbc = { version = "=0.1.2", features = ["alloc"] }
112113
# Note: Do not use the "clock" feature of chrono, as it links us to CoreFoundation on macOS.
113114
# Instead use util::time::utc_now()

cli/util/path.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

33
use std::borrow::Cow;
4+
use std::fmt::Write;
45
use std::path::Path;
56
use std::path::PathBuf;
67

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

6061
fn gen_rand_path_component() -> String {
61-
(0..4).fold(String::new(), |mut output, _| {
62-
output.push_str(&format!("{:02x}", rand::random::<u8>()));
62+
(0..4).fold(String::with_capacity(8), |mut output, _| {
63+
write!(&mut output, "{:02x}", rand::random::<u8>()).unwrap();
6364
output
6465
})
6566
}

cli/util/progress_bar/renderer.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

3+
use std::fmt::Write;
34
use std::sync::atomic::AtomicUsize;
45
use std::sync::atomic::Ordering;
56
use std::time::Duration;
@@ -81,12 +82,14 @@ impl ProgressBarRenderer for BarProgressBarRenderer {
8182
let elapsed_text = get_elapsed_text(data.duration);
8283
let mut text = String::new();
8384
if !display_entry.message.is_empty() {
84-
text.push_str(&format!(
85-
"{} {}{}\n",
85+
writeln!(
86+
&mut text,
87+
"{} {}{}",
8688
colors::green("Download"),
8789
display_entry.message,
8890
bytes_text,
89-
));
91+
)
92+
.unwrap();
9093
}
9194
text.push_str(&elapsed_text);
9295
let max_width = (data.terminal_width as i32 - 5).clamp(10, 75) as usize;

resolvers/npm_cache/tarball_extract.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ fn get_atomic_dir_path(file_path: &Path) -> PathBuf {
219219
}
220220

221221
fn gen_rand_path_component() -> String {
222-
(0..4).fold(String::new(), |mut output, _| {
223-
output.push_str(&format!("{:02x}", rand::random::<u8>()));
222+
use std::fmt::Write;
223+
(0..4).fold(String::with_capacity(8), |mut output, _| {
224+
write!(&mut output, "{:02x}", rand::random::<u8>()).unwrap();
224225
output
225226
})
226227
}

runtime/permissions/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ name = "deno_permissions"
1414
path = "lib.rs"
1515

1616
[dependencies]
17+
capacity_builder.workspace = true
1718
deno_core.workspace = true
1819
deno_path_util.workspace = true
1920
deno_terminal.workspace = true

runtime/permissions/lib.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

3+
use capacity_builder::StringBuilder;
34
use deno_core::parking_lot::Mutex;
45
use deno_core::serde::de;
56
use deno_core::serde::Deserialize;
@@ -179,13 +180,18 @@ impl PermissionState {
179180
(Ok(()), false, false)
180181
}
181182
PermissionState::Prompt if prompt => {
182-
let msg = format!(
183-
"{} access{}",
184-
name,
185-
info()
186-
.map(|info| { format!(" to {info}") })
187-
.unwrap_or_default(),
188-
);
183+
let msg = {
184+
let info = info();
185+
StringBuilder::build(|builder| {
186+
builder.append(name);
187+
builder.append(" access");
188+
if let Some(info) = &info {
189+
builder.append(" to ");
190+
builder.append(info);
191+
}
192+
})
193+
.unwrap()
194+
};
189195
match permission_prompt(&msg, name, api_name, true) {
190196
PromptResponse::Allow => {
191197
Self::log_perm_access(name, info);
@@ -344,11 +350,11 @@ pub trait QueryDescriptor: Debug {
344350
fn overlaps_deny(&self, other: &Self::DenyDesc) -> bool;
345351
}
346352

347-
fn format_display_name(display_name: Cow<str>) -> String {
353+
fn format_display_name(display_name: Cow<str>) -> Cow<str> {
348354
if display_name.starts_with('<') && display_name.ends_with('>') {
349-
display_name.into_owned()
355+
display_name
350356
} else {
351-
format!("\"{}\"", display_name)
357+
Cow::Owned(format!("\"{}\"", display_name))
352358
}
353359
}
354360

@@ -424,7 +430,7 @@ impl<TQuery: QueryDescriptor> UnaryPermission<TQuery> {
424430
.check2(
425431
TQuery::flag_name(),
426432
api_name,
427-
|| desc.map(|d| format_display_name(d.display_name())),
433+
|| desc.map(|d| format_display_name(d.display_name()).into_owned()),
428434
self.prompt,
429435
);
430436
if prompted {
@@ -487,12 +493,17 @@ impl<TQuery: QueryDescriptor> UnaryPermission<TQuery> {
487493
if !self.prompt {
488494
return PermissionState::Denied;
489495
}
490-
let mut message = String::with_capacity(40);
491-
message.push_str(&format!("{} access", TQuery::flag_name()));
492-
if let Some(desc) = desc {
493-
message
494-
.push_str(&format!(" to {}", format_display_name(desc.display_name())));
495-
}
496+
let maybe_formatted_display_name =
497+
desc.map(|d| format_display_name(d.display_name()));
498+
let message = StringBuilder::build(|builder| {
499+
builder.append(TQuery::flag_name());
500+
builder.append(" access");
501+
if let Some(display_name) = &maybe_formatted_display_name {
502+
builder.append(" to ");
503+
builder.append(display_name)
504+
}
505+
})
506+
.unwrap();
496507
match permission_prompt(
497508
&message,
498509
TQuery::flag_name(),

0 commit comments

Comments
 (0)