mirror of
https://github.com/NotAShelf/microfetch.git
synced 2025-12-13 16:11:01 +00:00
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:
parent
8376e9d323
commit
9da87b933d
1 changed files with 19 additions and 4 deletions
23
src/main.rs
23
src/main.rs
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue