Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions arceos/modules/axfs/src/fs/procfs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{borrow::ToOwned, collections::BTreeMap, format, string::String, sync::Arc, vec::Vec};
use alloc::{borrow::ToOwned, collections::BTreeMap, string::{String, ToString}, sync::Arc, vec::Vec};
use core::{
any::Any,
borrow::Borrow,
Expand Down Expand Up @@ -475,7 +475,7 @@ impl ProcFilesystem {

if let Some(fds) = provider.process_fds(pid) {
for fd in fds {
let name = format!("{}", fd);
let name = fd.to_string();
let child_ino = PID_INODE_START + (pid << PID_INODE_SHIFT) + SUB_INO_FD_BASE + fd as u64;
entries.insert(name.into(), InodeRef::new(child_ino));
}
Expand Down Expand Up @@ -606,12 +606,11 @@ fn render_meminfo() -> String {
let mem_free = free_bytes.min(total_bytes);
let mem_available = mem_free;

format!(
"MemTotal: {:>8} kB\nMemFree: {:>9} kB\nMemAvailable: {:>4} kB\n",
to_kib(total_bytes),
to_kib(mem_free),
to_kib(mem_available)
)
use core::fmt::Write;
let mut s = String::with_capacity(128);
let _ = write!(&mut s, "MemTotal: {:>8} kB\nMemFree: {:>9} kB\nMemAvailable: {:>4} kB\n",
to_kib(total_bytes), to_kib(mem_free), to_kib(mem_available));
s
}

fn render_mounts() -> String {
Expand Down Expand Up @@ -647,7 +646,7 @@ fn render_proc_file(fs: &ProcFilesystem, kind: ProcLiveFileKind) -> String {
ProcLiveFileKind::SelfSymlink => {
if let Some(provider) = PROCESS_PROVIDER.get() {
if let Some(pid) = provider.current_pid() {
return format!("{}", pid);
return pid.to_string();
}
}
"1".to_owned()
Expand All @@ -656,7 +655,7 @@ fn render_proc_file(fs: &ProcFilesystem, kind: ProcLiveFileKind) -> String {
if let Some(provider) = PROCESS_PROVIDER.get() {
let pids = provider.process_pids();
if let Some(&min_pid) = pids.iter().min() {
return format!("{}", min_pid);
return min_pid.to_string();
}
}
"1".to_owned()
Expand Down Expand Up @@ -695,7 +694,9 @@ fn render_proc_file(fs: &ProcFilesystem, kind: ProcLiveFileKind) -> String {
fs.gid_map_map.lock().get(&pid).cloned().unwrap_or_default()
}
ProcLiveFileKind::PidMax => {
format!("{}\n", PID_MAX.load(core::sync::atomic::Ordering::Acquire))
let mut s = PID_MAX.load(core::sync::atomic::Ordering::Acquire).to_string();
s.push('\n');
s
Comment on lines +697 to +699

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Calling to_string() on an integer typically allocates a String with a capacity exactly matching the length of the formatted number. Consequently, calling s.push('\n') immediately after will trigger a reallocation and copy the string data, which defeats the optimization goal of this PR.

Using write! with a pre-allocated String of capacity 12 (sufficient for any u32 plus a newline) avoids this reallocation entirely.

            use core::fmt::Write;
            let mut s = String::with_capacity(12);
            let _ = write!(&mut s, "{}\n", PID_MAX.load(core::sync::atomic::Ordering::Acquire));
            s

}
ProcLiveFileKind::Filesystems => render_filesystems(),
ProcLiveFileKind::Tainted => {
Expand Down Expand Up @@ -906,7 +907,7 @@ impl DirNodeOps for ProcNode {

if let Some(fds) = provider.process_fds(pid) {
for fd in fds {
let name = format!("{}", fd);
let name = fd.to_string();
let child_ino = PID_INODE_START + (pid << PID_INODE_SHIFT) + SUB_INO_FD_BASE + fd as u64;
all_entries.push((name, child_ino));
}
Expand All @@ -922,7 +923,7 @@ impl DirNodeOps for ProcNode {
if self.ino == ROOT_INO {
if let Some(provider) = PROCESS_PROVIDER.get() {
for pid in provider.process_pids() {
let name = format!("{}", pid);
let name = pid.to_string();
let child_ino = PID_INODE_START + (pid << PID_INODE_SHIFT) + SUB_INO_DIR;
all_entries.push((name, child_ino));
}
Expand Down