micro optimizations (#8)

* perf: break early after parsing required meminfo

Also a match statement for compiler magic.

* perf: pre-allocate strings when reading files

* refactor: remove duplicate .to_string()

* perf: try to print everything in one syscall

println! sends a syscall for each line.

* perf: get rid of duplicate uname syscall

* perf: simplify first letter capitalization

* refactor: directly use key in match statement
This commit is contained in:
Ryan 2024-08-15 18:32:02 +08:00 committed by GitHub
parent cb9703f820
commit 907112f2d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 52 additions and 42 deletions

View file

@ -1,21 +1,13 @@
pub fn get_desktop_info() -> String { pub fn get_desktop_info() -> String {
fn capitalize_first_letter(s: &str) -> String {
if s.is_empty() {
return String::new();
}
let mut chars = s.chars();
let first_char = chars.next().unwrap().to_uppercase().to_string();
let rest: String = chars.collect();
first_char + &rest
}
// Retrieve the environment variables and handle Result types // Retrieve the environment variables and handle Result types
let desktop_env = std::env::var("XDG_CURRENT_DESKTOP"); let desktop_env = std::env::var("XDG_CURRENT_DESKTOP");
let display_backend_result = std::env::var("XDG_SESSION_TYPE"); let display_backend_result = std::env::var("XDG_SESSION_TYPE");
// Capitalize the first letter of the display backend value // Capitalize the first letter of the display backend value
let display_backend = capitalize_first_letter(display_backend_result.as_deref().unwrap_or("")); let mut display_backend = display_backend_result.unwrap_or_default();
if let Some(c) = display_backend.as_mut_str().get_mut(0..1) {
c.make_ascii_uppercase();
}
// Trim "none+" from the start of desktop_env if present // Trim "none+" from the start of desktop_env if present
// Use "Unknown" if desktop_env is empty or has an error // Use "Unknown" if desktop_env is empty or has an error
@ -26,10 +18,11 @@ pub fn get_desktop_info() -> String {
// Handle the case where display_backend might be empty after capitalization // Handle the case where display_backend might be empty after capitalization
let display_backend = if display_backend.is_empty() { let display_backend = if display_backend.is_empty() {
"Unknown".to_string() "Unknown"
} else { } else {
display_backend &display_backend
}; }
.to_string();
format!("{desktop_env} ({display_backend})") format!("{desktop_env} ({display_backend})")
} }

View file

@ -4,6 +4,8 @@ mod release;
mod system; mod system;
mod uptime; mod uptime;
use std::io::Write;
use crate::colors::{print_dots, BLUE, CYAN, RESET}; use crate::colors::{print_dots, BLUE, CYAN, RESET};
use crate::desktop::get_desktop_info; use crate::desktop::get_desktop_info;
use crate::release::{get_os_pretty_name, get_system_info}; use crate::release::{get_os_pretty_name, get_system_info};
@ -15,10 +17,11 @@ use color_eyre::Report;
fn main() -> Result<(), Report> { fn main() -> Result<(), Report> {
color_eyre::install()?; color_eyre::install()?;
let utsname = nix::sys::utsname::uname()?;
let fields = Fields { let fields = Fields {
user_info: get_username_and_hostname(), user_info: get_username_and_hostname(&utsname),
os_name: get_os_pretty_name()?, os_name: get_os_pretty_name()?,
kernel_version: get_system_info()?, kernel_version: get_system_info(&utsname)?,
shell: get_shell(), shell: get_shell(),
desktop: get_desktop_info(), desktop: get_desktop_info(),
uptime: get_current()?, uptime: get_current()?,
@ -61,7 +64,7 @@ fn print_system_info(fields: &Fields) {
colors, colors,
} = fields; } = fields;
println!( let _ = std::io::stdout().write_all(format!(
" "
{CYAN} {BLUE} {user_info} ~{RESET} {CYAN} {BLUE} {user_info} ~{RESET}
{CYAN} {BLUE} {CYAN} {CYAN} {BLUE}System{RESET} {os_name} {CYAN} {BLUE} {CYAN} {CYAN} {BLUE}System{RESET} {os_name}
@ -71,5 +74,6 @@ fn print_system_info(fields: &Fields) {
{BLUE} {CYAN} {CYAN} {CYAN} {BLUE}Desktop{RESET} {desktop} {BLUE} {CYAN} {CYAN} {CYAN} {BLUE}Desktop{RESET} {desktop}
{BLUE} {CYAN}{BLUE} {CYAN}󰍛 {BLUE}Memory{RESET} {memory_usage} {BLUE} {CYAN}{BLUE} {CYAN}󰍛 {BLUE}Memory{RESET} {memory_usage}
{BLUE} {CYAN}{BLUE} {CYAN}󱥎 {BLUE}Storage (/){RESET} {storage} {BLUE} {CYAN}{BLUE} {CYAN}󱥎 {BLUE}Storage (/){RESET} {storage}
{CYAN} {BLUE} {CYAN} {BLUE}Colors{RESET} {colors}"); {CYAN} {BLUE} {CYAN} {BLUE}Colors{RESET} {colors}
").as_bytes());
} }

View file

@ -1,9 +1,11 @@
use color_eyre::Result; use color_eyre::Result;
use std::fs::read_to_string; use nix::sys::utsname::UtsName;
use std::io; use std::{
fs::File,
io::{self, Read},
};
pub fn get_system_info() -> nix::Result<String> { pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> {
let utsname = nix::sys::utsname::uname()?;
Ok(format!( Ok(format!(
"{} {} ({})", "{} {} ({})",
utsname.sysname().to_str().unwrap_or("Unknown"), utsname.sysname().to_str().unwrap_or("Unknown"),
@ -13,15 +15,13 @@ pub fn get_system_info() -> nix::Result<String> {
} }
pub fn get_os_pretty_name() -> Result<String, io::Error> { pub fn get_os_pretty_name() -> Result<String, io::Error> {
let os_release_content = read_to_string("/etc/os-release")?; let mut os_release_content = String::with_capacity(1024);
File::open("/etc/os-release")?.read_to_string(&mut os_release_content)?;
let pretty_name = os_release_content let pretty_name = os_release_content
.lines() .lines()
.find(|line| line.starts_with("PRETTY_NAME=")) .find(|line| line.starts_with("PRETTY_NAME="))
.map(|line| { .map(|line| line.trim_start_matches("PRETTY_NAME=").trim_matches('"'));
line.trim_start_matches("PRETTY_NAME=")
.trim_matches('"')
.to_string()
});
Ok(pretty_name.unwrap_or("Unknown".to_string())) Ok(pretty_name.unwrap_or("Unknown").to_string())
} }

View file

@ -1,15 +1,21 @@
use color_eyre::Result; use color_eyre::Result;
use nix::sys::statvfs::statvfs; use nix::sys::{statvfs::statvfs, utsname::UtsName};
use std::env; use std::{
use std::io::{self}; env,
fs::File,
io::{self, Read},
};
use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW}; use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW};
pub fn get_username_and_hostname() -> String { pub fn get_username_and_hostname(utsname: &UtsName) -> String {
let username = env::var("USER").unwrap_or("unknown_user".to_string()); let username = env::var("USER").unwrap_or("unknown_user".to_string());
let hostname = nix::unistd::gethostname().unwrap_or("unknown_host".to_string().into()); let hostname = utsname
let hostname = hostname.to_string_lossy(); .nodename()
.to_str()
.unwrap_or("unknown_host")
.to_string();
format!("{YELLOW}{username}{RED}@{GREEN}{hostname}") format!("{YELLOW}{username}{RED}@{GREEN}{hostname}")
} }
@ -45,13 +51,20 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
let mut total_memory_kb = 0.0; let mut total_memory_kb = 0.0;
let mut available_memory_kb = 0.0; let mut available_memory_kb = 0.0;
for line in std::fs::read_to_string("/proc/meminfo")?.lines() { let mut meminfo = String::with_capacity(2048);
File::open("/proc/meminfo")?.read_to_string(&mut meminfo)?;
for line in meminfo.lines() {
let mut split = line.split_whitespace(); let mut split = line.split_whitespace();
let key = split.next().unwrap_or(""); match split.next().unwrap_or_default() {
if key == "MemTotal:" { "MemTotal:" => total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0),
total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0); "MemAvailable:" => {
} else if key == "MemAvailable:" { available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0);
available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0);
// MemTotal comes before MemAvailable, stop parsing
break;
}
_ => (),
} }
} }