From c13902670424cd5e3e9a664677bdf2f1a28a5aa1 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Tue, 15 Apr 2025 03:19:53 -0400 Subject: [PATCH 1/5] refactor: clean up a few things I've decided to keep lazy_static for now as std::sync::LazyLock seems to have a slightly bigger impact on binary size. --- benches/benchmark.rs | 4 ++-- src/colors.rs | 11 ++++++----- src/main.rs | 11 +++++------ src/release.rs | 6 +++--- src/system.rs | 1 - 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/benches/benchmark.rs b/benches/benchmark.rs index 7706617..03e49f4 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -1,4 +1,4 @@ -use criterion::{criterion_group, criterion_main, Criterion}; +use criterion::{Criterion, criterion_group, criterion_main}; use microfetch_lib::colors::print_dots; use microfetch_lib::desktop::get_desktop_info; use microfetch_lib::release::{get_os_pretty_name, get_system_info}; @@ -10,7 +10,7 @@ use microfetch_lib::uptime::get_current; fn main_benchmark(c: &mut Criterion) { let utsname = nix::sys::utsname::uname().expect("lol"); c.bench_function("user_info", |b| { - b.iter(|| get_username_and_hostname(&utsname)) + b.iter(|| get_username_and_hostname(&utsname)); }); c.bench_function("os_name", |b| b.iter(get_os_pretty_name)); c.bench_function("kernel_version", |b| b.iter(|| get_system_info(&utsname))); diff --git a/src/colors.rs b/src/colors.rs index 061db15..11a4b9f 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -12,8 +12,8 @@ pub struct Colors { impl Colors { const fn new(is_no_color: bool) -> Self { - match is_no_color { - true => Self { + if is_no_color { + Self { reset: "", blue: "", cyan: "", @@ -21,8 +21,9 @@ impl Colors { yellow: "", red: "", magenta: "", - }, - false => Self { + } + } else { + Self { reset: "\x1b[0m", blue: "\x1b[34m", cyan: "\x1b[36m", @@ -30,7 +31,7 @@ impl Colors { yellow: "\x1b[33m", red: "\x1b[31m", magenta: "\x1b[35m", - }, + } } } } diff --git a/src/main.rs b/src/main.rs index 05024df..3297d90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use crate::desktop::get_desktop_info; use crate::release::{get_os_pretty_name, get_system_info}; use crate::system::{get_memory_usage, get_root_disk_usage, get_shell, get_username_and_hostname}; use crate::uptime::get_current; -use std::io::Write; +use std::io::{Write, stdout}; fn main() -> Result<(), Box> { let args: Vec = std::env::args().collect(); @@ -20,7 +20,7 @@ fn main() -> Result<(), Box> { let fields = Fields { user_info: get_username_and_hostname(&utsname), os_name: get_os_pretty_name()?, - kernel_version: get_system_info(&utsname)?, + kernel_version: get_system_info(&utsname), shell: get_shell(), desktop: get_desktop_info(), uptime: get_current()?, @@ -77,10 +77,9 @@ fn print_system_info(fields: &Fields) { {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}"); + {cyan} ▟█▘ ▜█▖ {blue}▝█▛ {cyan} {blue}Colors{reset}  {colors}\n"); - std::io::stdout() - .lock() - .write_all(format!("{}\n", system_info).as_bytes()) + stdout() + .write_all(system_info.as_bytes()) .expect("Failed to write to stdout"); } diff --git a/src/release.rs b/src/release.rs index 08e5454..6d1b20f 100644 --- a/src/release.rs +++ b/src/release.rs @@ -4,13 +4,13 @@ use std::{ io::{self, BufRead, BufReader}, }; -pub fn get_system_info(utsname: &UtsName) -> nix::Result { - Ok(format!( +pub fn get_system_info(utsname: &UtsName) -> String { + format!( "{} {} ({})", utsname.sysname().to_str().unwrap_or("Unknown"), utsname.release().to_str().unwrap_or("Unknown"), utsname.machine().to_str().unwrap_or("Unknown") - )) + ) } pub fn get_os_pretty_name() -> Result { diff --git a/src/system.rs b/src/system.rs index 4cc1342..aed8752 100644 --- a/src/system.rs +++ b/src/system.rs @@ -49,7 +49,6 @@ pub fn get_root_disk_usage() -> Result { } pub fn get_memory_usage() -> Result { - #[inline(always)] fn parse_memory_info() -> Result<(f64, f64), io::Error> { let mut total_memory_kb = 0.0; let mut available_memory_kb = 0.0; From 9713138e9415b5ce4071d39410446ebac3e7b521 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Tue, 15 Apr 2025 03:32:46 -0400 Subject: [PATCH 2/5] perf: pre-allocate strings --- src/uptime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uptime.rs b/src/uptime.rs index d9c53de..8643e24 100644 --- a/src/uptime.rs +++ b/src/uptime.rs @@ -13,7 +13,7 @@ pub fn get_current() -> Result { let hours = (uptime_seconds / 3600) % 24; let minutes = (uptime_seconds / 60) % 60; - let mut result = String::new(); + let mut result = String::with_capacity(32); if days > 0 { result.push_str(&days.to_string()); result.push_str(if days == 1 { " day" } else { " days" }); From 88c9ff5e1311e8e88a1ba67b66aa7ea0cb8d5bcc Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Tue, 15 Apr 2025 03:41:14 -0400 Subject: [PATCH 3/5] perf: don't collect args into a Vec --- src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3297d90..47364a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,7 @@ use crate::uptime::get_current; use std::io::{Write, stdout}; fn main() -> Result<(), Box> { - let args: Vec = std::env::args().collect(); - if args.len() > 1 && args[1] == "--version" { + if Some("--version") == std::env::args().nth(1).as_deref() { println!("Microfetch {}", env!("CARGO_PKG_VERSION")); } else { let utsname = nix::sys::utsname::uname()?; From 0233cdc0fc722f1cfaf99898cb13a28502622db6 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Tue, 15 Apr 2025 03:41:38 -0400 Subject: [PATCH 4/5] perf: use MaybeUninit for libc buffers --- src/uptime.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/uptime.rs b/src/uptime.rs index 8643e24..496052a 100644 --- a/src/uptime.rs +++ b/src/uptime.rs @@ -1,12 +1,12 @@ -use std::io; +use std::{io, mem::MaybeUninit}; pub fn get_current() -> Result { - let uptime_seconds = unsafe { - let mut info: libc::sysinfo = std::mem::zeroed(); - if libc::sysinfo(&mut info) != 0 { + let uptime_seconds = { + let mut info = MaybeUninit::uninit(); + if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 { return Err(io::Error::last_os_error()); } - info.uptime as u64 + unsafe { info.assume_init().uptime as u64 } }; let days = uptime_seconds / 86400; From b814b2bacf829bc9877359cd3dc0727d6de90d75 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Tue, 15 Apr 2025 03:42:38 -0400 Subject: [PATCH 5/5] refactor: return write error --- src/main.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 47364a9..41a1818 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ fn main() -> Result<(), Box> { storage: get_root_disk_usage()?, colors: print_dots(), }; - print_system_info(&fields); + print_system_info(&fields)?; } Ok(()) @@ -49,7 +49,7 @@ struct Fields { colors: String, } -fn print_system_info(fields: &Fields) { +fn print_system_info(fields: &Fields) -> Result<(), Box> { use crate::colors::COLORS; let Fields { @@ -78,7 +78,5 @@ fn print_system_info(fields: &Fields) { {blue} ▝ {cyan}▟█▜█▖{blue}▀▀▀▀▀██▛▀▀▘ {cyan}󱥎 {blue}Storage (/){reset}  {storage} {cyan} ▟█▘ ▜█▖ {blue}▝█▛ {cyan} {blue}Colors{reset}  {colors}\n"); - stdout() - .write_all(system_info.as_bytes()) - .expect("Failed to write to stdout"); + Ok(stdout().write_all(system_info.as_bytes())?) }