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
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,15 +1,21 @@
use color_eyre::Result;
use nix::sys::statvfs::statvfs;
use nix::sys::{statvfs::statvfs, utsname::UtsName};
use std::env;
use std::io::{self};
use std::{
env,
fs::File,
io::{self, Read},
};
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 hostname = nix::unistd::gethostname().unwrap_or("unknown_host".to_string().into());
let hostname = hostname.to_string_lossy();
let hostname = utsname
.nodename()
.to_str()
.unwrap_or("unknown_host")
.to_string();
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 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 key = split.next().unwrap_or("");
if key == "MemTotal:" {
total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0);
} else if key == "MemAvailable:" {
available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0);
match split.next().unwrap_or_default() {
"MemTotal:" => total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0),
"MemAvailable:" => {
available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0);
// MemTotal comes before MemAvailable, stop parsing
break;
}
_ => (),
}
}