perf: use stack buffer and direct write syscall in print_system_info

Eliminates ~1KB stdout buffering allocation by using Cursor<&mut [u8]>
and libc::write instead of format!() + stdout().write_all()
This commit is contained in:
Uzair Aftab 2025-11-30 19:05:05 +01:00
commit 9da87b933d
No known key found for this signature in database

View file

@ -5,7 +5,7 @@ mod syscall;
mod system;
mod uptime;
use std::io::{Write, stdout};
use std::io::{self, Cursor, Write};
pub use microfetch_lib::UtsName;
@ -81,7 +81,13 @@ fn print_system_info(
let cyan = COLORS.cyan;
let blue = COLORS.blue;
let reset = COLORS.reset;
let system_info = format!("
let mut buf = [0u8; 2048];
let mut cursor = Cursor::new(&mut buf[..]);
write!(
cursor,
"
{cyan} {blue} {user_info} ~{reset}
{cyan} {blue} {cyan} {cyan} {blue}System{reset} {os_name}
{cyan} {blue} {cyan} {cyan} {blue}Kernel{reset} {kernel_version}
@ -90,7 +96,16 @@ fn print_system_info(
{blue} {cyan} {cyan} {cyan} {blue}Desktop{reset} {desktop}
{blue} {cyan}{blue} {cyan} {blue}Memory{reset} {memory_usage}
{blue} {cyan}{blue} {cyan}󱥎 {blue}Storage (/){reset} {storage}
{cyan} {blue} {cyan} {blue}Colors{reset} {colors}\n");
{cyan} {blue} {cyan} {blue}Colors{reset} {colors}\n"
)?;
Ok(stdout().write_all(system_info.as_bytes())?)
#[allow(clippy::cast_possible_truncation)]
let len = cursor.position() as usize;
// Direct syscall to avoid stdout buffering allocation
let written = unsafe { libc::write(libc::STDOUT_FILENO, buf.as_ptr().cast(), len) };
if written < 0 {
return Err(io::Error::last_os_error().into());
}
Ok(())
}