Reduce allocations (#30693)

Removes a unnecessary string conversion and some clones

Release Notes:

- N/A
This commit is contained in:
tidely 2025-05-14 19:29:28 +03:00 committed by GitHub
parent fcfe4e2c14
commit bc99a86bb7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 48 additions and 57 deletions

View file

@ -942,8 +942,8 @@ impl MentionLink {
format!("[@{}]({}:{})", title, Self::THREAD, id)
}
ThreadContextEntry::Context { path, title } => {
let filename = path.file_name().unwrap_or_default();
let escaped_filename = urlencoding::encode(&filename.to_string_lossy()).to_string();
let filename = path.file_name().unwrap_or_default().to_string_lossy();
let escaped_filename = urlencoding::encode(&filename);
format!(
"[@{}]({}:{}{})",
title,

View file

@ -191,7 +191,7 @@ impl TerminalInlineAssistant {
};
self.prompt_history.retain(|prompt| *prompt != user_prompt);
self.prompt_history.push_back(user_prompt.clone());
self.prompt_history.push_back(user_prompt);
if self.prompt_history.len() > PROMPT_HISTORY_MAX_LEN {
self.prompt_history.pop_front();
}

View file

@ -2583,7 +2583,7 @@ impl Thread {
.read(cx)
.current_user()
.map(|user| user.github_login.clone());
let client = self.project.read(cx).client().clone();
let client = self.project.read(cx).client();
let serialize_task = self.serialize(cx);
cx.background_executor()

View file

@ -260,10 +260,7 @@ impl ThreadHistory {
}
});
self.search_state = SearchState::Searching {
query: query.clone(),
_task: task,
};
self.search_state = SearchState::Searching { query, _task: task };
cx.notify();
}

View file

@ -3044,7 +3044,7 @@ fn invoked_slash_command_fold_placeholder(
.gap_2()
.bg(cx.theme().colors().surface_background)
.rounded_sm()
.child(Label::new(format!("/{}", command.name.clone())))
.child(Label::new(format!("/{}", command.name)))
.map(|parent| match &command.status {
InvokedSlashCommandStatus::Running(_) => {
parent.child(Icon::new(IconName::ArrowCircle).with_animation(

View file

@ -137,18 +137,14 @@ pub fn os_version() -> String {
log::error!("Failed to load /etc/os-release, /usr/lib/os-release");
"".to_string()
};
let mut name = "unknown".to_string();
let mut version = "unknown".to_string();
let mut name = "unknown";
let mut version = "unknown";
for line in content.lines() {
if line.starts_with("ID=") {
name = line.trim_start_matches("ID=").trim_matches('"').to_string();
}
if line.starts_with("VERSION_ID=") {
version = line
.trim_start_matches("VERSION_ID=")
.trim_matches('"')
.to_string();
match line.split_once('=') {
Some(("ID", val)) => name = val.trim_matches('"'),
Some(("VERSION_ID", val)) => version = val.trim_matches('"'),
_ => {}
}
}
@ -222,7 +218,7 @@ impl Telemetry {
cx.background_spawn({
let state = state.clone();
let os_version = os_version();
state.lock().os_version = Some(os_version.clone());
state.lock().os_version = Some(os_version);
async move {
if let Some(tempfile) = File::create(Self::log_file_path()).log_err() {
state.lock().log_file = Some(tempfile);

View file

@ -1059,7 +1059,7 @@ impl Render for ChatPanel {
.child(
Label::new(format!(
"@{}",
user_being_replied_to.github_login.clone()
user_being_replied_to.github_login
))
.size(LabelSize::Small)
.weight(FontWeight::BOLD),

View file

@ -365,7 +365,7 @@ impl ConsoleQueryBarCompletionProvider {
new_text: string_match.string.clone(),
label: CodeLabel {
filter_range: 0..string_match.string.len(),
text: format!("{} {}", string_match.string.clone(), variable_value),
text: format!("{} {}", string_match.string, variable_value),
runs: Vec::new(),
},
icon_path: None,

View file

@ -955,7 +955,7 @@ impl ExtensionsPage {
.disabled(true),
configure: is_configurable.then(|| {
Button::new(
SharedString::from(format!("configure-{}", extension.id.clone())),
SharedString::from(format!("configure-{}", extension.id)),
"Configure",
)
.disabled(true)
@ -980,7 +980,7 @@ impl ExtensionsPage {
}),
configure: is_configurable.then(|| {
Button::new(
SharedString::from(format!("configure-{}", extension.id.clone())),
SharedString::from(format!("configure-{}", extension.id)),
"Configure",
)
.on_click({
@ -1049,7 +1049,7 @@ impl ExtensionsPage {
.disabled(true),
configure: is_configurable.then(|| {
Button::new(
SharedString::from(format!("configure-{}", extension.id.clone())),
SharedString::from(format!("configure-{}", extension.id)),
"Configure",
)
.disabled(true)

View file

@ -354,8 +354,9 @@ impl PickerDelegate for NewPathDelegate {
let m = self.matches.get(self.selected_index)?;
if m.is_dir(self.project.read(cx), cx) {
let path = m.relative_path();
self.last_selected_dir = Some(path.clone());
Some(format!("{}/", path))
let result = format!("{}/", path);
self.last_selected_dir = Some(path);
Some(result)
} else {
None
}

View file

@ -2583,19 +2583,18 @@ impl GitPanel {
} else {
workspace.update(cx, |workspace, cx| {
let workspace_weak = cx.weak_entity();
let toast =
StatusToast::new(format!("git {} failed", action.clone()), cx, |this, _cx| {
this.icon(ToastIcon::new(IconName::XCircle).color(Color::Error))
.action("View Log", move |window, cx| {
let message = message.clone();
let action = action.clone();
workspace_weak
.update(cx, move |workspace, cx| {
Self::open_output(action, workspace, &message, window, cx)
})
.ok();
})
});
let toast = StatusToast::new(format!("git {} failed", action), cx, |this, _cx| {
this.icon(ToastIcon::new(IconName::XCircle).color(Color::Error))
.action("View Log", move |window, cx| {
let message = message.clone();
let action = action.clone();
workspace_weak
.update(cx, move |workspace, cx| {
Self::open_output(action, workspace, &message, window, cx)
})
.ok();
})
});
workspace.toggle_status_toast(toast, cx)
});
}

View file

@ -134,7 +134,7 @@ impl From<ErrorCode> for anyhow::Error {
RpcError {
request: None,
code: value,
msg: format!("{:?}", value).to_string(),
msg: format!("{:?}", value),
tags: Default::default(),
}
.into()
@ -241,7 +241,7 @@ impl From<ErrorCode> for RpcError {
RpcError {
request: None,
code,
msg: format!("{:?}", code).to_string(),
msg: format!("{:?}", code),
tags: Default::default(),
}
}

View file

@ -1946,9 +1946,9 @@ impl Render for ProjectSearchBar {
if match_quantity > 0 {
debug_assert!(match_quantity >= index);
if limit_reached {
Some(format!("{index}/{match_quantity}+").to_string())
Some(format!("{index}/{match_quantity}+"))
} else {
Some(format!("{index}/{match_quantity}").to_string())
Some(format!("{index}/{match_quantity}"))
}
} else {
None

View file

@ -269,13 +269,12 @@ impl TerminalError {
Err(s) => s,
}
})
.unwrap_or_else(|| {
let default_dir =
dirs::home_dir().map(|buf| buf.into_os_string().to_string_lossy().to_string());
match default_dir {
Some(dir) => format!("<none specified, using home directory> {}", dir),
None => "<none specified, could not find home directory>".to_string(),
}
.unwrap_or_else(|| match dirs::home_dir() {
Some(dir) => format!(
"<none specified, using home directory> {}",
dir.into_os_string().to_string_lossy()
),
None => "<none specified, could not find home directory>".to_string(),
})
}

View file

@ -475,7 +475,7 @@ impl TitleBar {
.label_size(LabelSize::Small)
.tooltip(Tooltip::text(format!(
"{} is sharing this project. Click to follow.",
host_user.github_login.clone()
host_user.github_login
)))
.on_click({
let host_peer_id = host.peer_id;

View file

@ -20,7 +20,7 @@ impl Modal {
pub fn new(id: impl Into<SharedString>, scroll_handle: Option<ScrollHandle>) -> Self {
let id = id.into();
let container_id = ElementId::Name(format!("{}_container", id.clone()).into());
let container_id = ElementId::Name(format!("{}_container", id).into());
Self {
id: ElementId::Name(id),
header: ModalHeader::new(),

View file

@ -77,7 +77,7 @@ impl QuickActionBar {
let menu_state = session_state(session.clone(), cx);
let id = "repl-menu".to_string();
let id = "repl-menu";
let element_id = |suffix| ElementId::Name(format!("{}-{}", id, suffix).into());
@ -99,8 +99,7 @@ impl QuickActionBar {
.child(
Label::new(format!(
"kernel: {} ({})",
menu_state.kernel_name.clone(),
menu_state.kernel_language.clone()
menu_state.kernel_name, menu_state.kernel_language
))
.size(LabelSize::Small)
.color(Color::Muted),
@ -121,7 +120,7 @@ impl QuickActionBar {
menu.custom_row(move |_window, _cx| {
h_flex()
.child(
Label::new(format!("{}...", status.clone().to_string()))
Label::new(format!("{}...", status.to_string()))
.size(LabelSize::Small)
.color(Color::Muted),
)