Merge pull request #22 from NotAShelf/notashelf/push-uysmruuoyvnz

various: reduce allocations where available
This commit is contained in:
raf 2025-11-17 18:42:58 +03:00 committed by GitHub
commit 11a726428b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 270 additions and 67 deletions

57
.github/workflows/hotpath-comment.yml vendored Normal file
View file

@ -0,0 +1,57 @@
name: Hotpath Comment
on:
workflow_run:
workflows: ["Hotpath Profile"]
types:
- completed
permissions:
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Download profiling results
uses: actions/download-artifact@v4
with:
name: hotpath-results
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
- name: Read PR number
id: pr
run: echo "number=$(cat pr_number.txt)" >> $GITHUB_OUTPUT
- name: Setup Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Install hotpath CLI
run: cargo install hotpath
- name: Post timing comparison comment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
hotpath profile-pr \
--repo ${{ github.repository }} \
--pr-number ${{ steps.pr.outputs.number }} \
--head-json head-timing.json \
--base-json base-timing.json \
--mode timing \
--title "⏱️ Hotpath Timing Profile"
- name: Post allocation comparison comment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
hotpath profile-pr \
--repo ${{ github.repository }} \
--pr-number ${{ steps.pr.outputs.number }} \
--head-json head-alloc.json \
--base-json base-alloc.json \
--mode alloc \
--title "📊 Hotpath Allocation Profile"

63
.github/workflows/hotpath-profile.yml vendored Normal file
View file

@ -0,0 +1,63 @@
name: Hotpath Profile
on:
pull_request:
branches: [ "main" ]
env:
CARGO_TERM_COLOR: always
jobs:
profile:
runs-on: ubuntu-latest
steps:
- name: Checkout PR HEAD
uses: actions/checkout@v4
- name: Setup Rust
uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Run timing profiling on HEAD
env:
HOTPATH_JSON: "true"
run: |
cargo run --features='hotpath' 2>&1 | grep '^{"hotpath_profiling_mode"' > head-timing.json
- name: Run allocation profiling on HEAD
env:
HOTPATH_JSON: "true"
run: |
cargo run --features='hotpath,hotpath-alloc-count-total' 2>&1 | grep '^{"hotpath_profiling_mode"' > head-alloc.json
- name: Checkout base branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.sha }}
- name: Run timing profiling on base
env:
HOTPATH_JSON: "true"
run: |
cargo run --features='hotpath' 2>&1 | grep '^{"hotpath_profiling_mode"' > base-timing.json
- name: Run allocation profiling on base
env:
HOTPATH_JSON: "true"
run: |
cargo run --features='hotpath,hotpath-alloc-count-total' 2>&1 | grep '^{"hotpath_profiling_mode"' > base-alloc.json
- name: Save PR number
run: echo "${{ github.event.number }}" > pr_number.txt
- name: Upload profiling results
uses: actions/upload-artifact@v4
with:
name: hotpath-results
path: |
head-timing.json
head-alloc.json
base-timing.json
base-alloc.json
pr_number.txt
retention-days: 1

2
Cargo.lock generated
View file

@ -772,7 +772,7 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]] [[package]]
name = "microfetch" name = "microfetch"
version = "0.4.9" version = "0.4.10"
dependencies = [ dependencies = [
"criterion", "criterion",
"hotpath", "hotpath",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "microfetch" name = "microfetch"
version = "0.4.9" version = "0.4.10"
edition = "2024" edition = "2024"
[lib] [lib]

View file

@ -37,7 +37,7 @@ impl Colors {
} }
pub static COLORS: LazyLock<Colors> = LazyLock::new(|| { pub static COLORS: LazyLock<Colors> = LazyLock::new(|| {
// check for NO_COLOR once at startup // Check for NO_COLOR once at startup
let is_no_color = env::var("NO_COLOR").is_ok(); let is_no_color = env::var("NO_COLOR").is_ok();
Colors::new(is_no_color) Colors::new(is_no_color)
}); });
@ -45,14 +45,37 @@ pub static COLORS: LazyLock<Colors> = LazyLock::new(|| {
#[must_use] #[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn print_dots() -> String { pub fn print_dots() -> String {
format!( // Pre-calculate capacity: 6 color codes + " " (glyph + 2 spaces) per color
"{} {} {} {} {} {} {}", const GLYPH: &str = "";
COLORS.blue, let capacity = COLORS.blue.len()
COLORS.cyan, + COLORS.cyan.len()
COLORS.green, + COLORS.green.len()
COLORS.yellow, + COLORS.yellow.len()
COLORS.red, + COLORS.red.len()
COLORS.magenta, + COLORS.magenta.len()
COLORS.reset, + COLORS.reset.len()
) + (GLYPH.len() + 2) * 6;
let mut result = String::with_capacity(capacity);
result.push_str(COLORS.blue);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.cyan);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.green);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.yellow);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.red);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.magenta);
result.push_str(GLYPH);
result.push_str(" ");
result.push_str(COLORS.reset);
result
} }

View file

@ -1,30 +1,37 @@
use std::fmt::Write;
#[must_use] #[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_desktop_info() -> String { pub fn get_desktop_info() -> String {
// 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 = std::env::var("XDG_SESSION_TYPE");
// Capitalize the first letter of the display backend value let desktop_str = match desktop_env {
let mut display_backend = display_backend_result.unwrap_or_default(); Err(_) => "Unknown",
if let Some(c) = display_backend.as_mut_str().get_mut(0..1) { Ok(ref s) if s.starts_with("none+") => &s[5..],
c.make_ascii_uppercase(); Ok(ref s) => s.as_str(),
}
// Trim "none+" from the start of desktop_env if present
// Use "Unknown" if desktop_env is empty or has an error
let desktop_env = match desktop_env {
Err(_) => "Unknown".to_owned(),
Ok(s) => s.trim_start_matches("none+").to_owned(),
}; };
// Handle the case where display_backend might be empty after capitalization let backend_str = match display_backend {
let display_backend = if display_backend.is_empty() { Err(_) => "Unknown",
"Unknown" Ok(ref s) if s.is_empty() => "Unknown",
} else { Ok(ref s) => s.as_str(),
&display_backend };
}
.to_owned();
format!("{desktop_env} ({display_backend})") // Pre-calculate capacity: desktop_len + " (" + backend_len + ")"
// Capitalize first char needs temporary allocation only if backend exists
let mut result =
String::with_capacity(desktop_str.len() + backend_str.len() + 3);
result.push_str(desktop_str);
result.push_str(" (");
// Capitalize first character of backend
if let Some(first_char) = backend_str.chars().next() {
let _ = write!(result, "{}", first_char.to_ascii_uppercase());
result.push_str(&backend_str[first_char.len_utf8()..]);
}
result.push(')');
result
} }

View file

@ -1,6 +1,7 @@
use std::{ use std::{
fmt::Write as _,
fs::File, fs::File,
io::{self, BufRead, BufReader}, io::{self, Read},
}; };
use nix::sys::utsname::UtsName; use nix::sys::utsname::UtsName;
@ -8,21 +9,31 @@ use nix::sys::utsname::UtsName;
#[must_use] #[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_system_info(utsname: &UtsName) -> String { pub fn get_system_info(utsname: &UtsName) -> String {
format!( let sysname = utsname.sysname().to_str().unwrap_or("Unknown");
"{} {} ({})", let release = utsname.release().to_str().unwrap_or("Unknown");
utsname.sysname().to_str().unwrap_or("Unknown"), let machine = utsname.machine().to_str().unwrap_or("Unknown");
utsname.release().to_str().unwrap_or("Unknown"),
utsname.machine().to_str().unwrap_or("Unknown") // Pre-allocate capacity: sysname + " " + release + " (" + machine + ")"
) let capacity = sysname.len() + 1 + release.len() + 2 + machine.len() + 1;
let mut result = String::with_capacity(capacity);
write!(result, "{sysname} {release} ({machine})").unwrap();
result
} }
/// Gets the pretty name of the OS from `/etc/os-release`.
///
/// # Errors
///
/// Returns an error if `/etc/os-release` cannot be read.
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_os_pretty_name() -> Result<String, io::Error> { pub fn get_os_pretty_name() -> Result<String, io::Error> {
let file = File::open("/etc/os-release")?; // We use a stack-allocated buffer here, which seems to perform MUCH better
let reader = BufReader::new(file); // than `BufReader`. In hindsight, I should've seen this coming.
let mut buffer = String::with_capacity(1024);
File::open("/etc/os-release")?.read_to_string(&mut buffer)?;
for line in reader.lines() { for line in buffer.lines() {
let line = line?;
if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") { if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") {
if let Some(trimmed) = pretty_name if let Some(trimmed) = pretty_name
.strip_prefix('"') .strip_prefix('"')

View file

@ -1,5 +1,6 @@
use std::{ use std::{
env, env,
fmt::Write as _,
fs::File, fs::File,
io::{self, Read}, io::{self, Read},
}; };
@ -12,18 +13,26 @@ use crate::colors::COLORS;
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_username_and_hostname(utsname: &UtsName) -> String { pub fn get_username_and_hostname(utsname: &UtsName) -> String {
let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_owned()); let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_owned());
let hostname = utsname let hostname = utsname.nodename().to_str().unwrap_or("unknown_host");
.nodename()
.to_str() let capacity = COLORS.yellow.len()
.unwrap_or("unknown_host") + username.len()
.to_owned(); + COLORS.red.len()
format!( + 1
"{yellow}{username}{red}@{green}{hostname}{reset}", + COLORS.green.len()
yellow = COLORS.yellow, + hostname.len()
red = COLORS.red, + COLORS.reset.len();
green = COLORS.green, let mut result = String::with_capacity(capacity);
reset = COLORS.reset,
) result.push_str(COLORS.yellow);
result.push_str(&username);
result.push_str(COLORS.red);
result.push('@');
result.push_str(COLORS.green);
result.push_str(hostname);
result.push_str(COLORS.reset);
result
} }
#[must_use] #[must_use]
@ -31,11 +40,22 @@ pub fn get_username_and_hostname(utsname: &UtsName) -> String {
pub fn get_shell() -> String { pub fn get_shell() -> String {
let shell_path = let shell_path =
env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_owned()); env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_owned());
let shell_name = shell_path.rsplit('/').next().unwrap_or("unknown_shell");
shell_name.to_owned() // Find last '/' and get the part after it, avoiding allocation
shell_path
.rsplit('/')
.next()
.unwrap_or("unknown_shell")
.to_owned()
} }
/// Gets the root disk usage information.
///
/// # Errors
///
/// Returns an error if the filesystem information cannot be retrieved.
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
#[allow(clippy::cast_precision_loss)]
pub fn get_root_disk_usage() -> Result<String, io::Error> { pub fn get_root_disk_usage() -> Result<String, io::Error> {
let vfs = statvfs("/")?; let vfs = statvfs("/")?;
let block_size = vfs.block_size() as u64; let block_size = vfs.block_size() as u64;
@ -49,13 +69,23 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
let used_size = used_size as f64 / (1024.0 * 1024.0 * 1024.0); let used_size = used_size as f64 / (1024.0 * 1024.0 * 1024.0);
let usage = (used_size / total_size) * 100.0; let usage = (used_size / total_size) * 100.0;
Ok(format!( let mut result = String::with_capacity(64);
write!(
result,
"{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})", "{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})",
cyan = COLORS.cyan, cyan = COLORS.cyan,
reset = COLORS.reset, reset = COLORS.reset,
)) )
.unwrap();
Ok(result)
} }
/// Gets the system memory usage information.
///
/// # Errors
///
/// Returns an error if `/proc/meminfo` cannot be read.
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_memory_usage() -> Result<String, io::Error> { pub fn get_memory_usage() -> Result<String, io::Error> {
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
@ -70,7 +100,7 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
let mut split = line.split_whitespace(); let mut split = line.split_whitespace();
match split.next().unwrap_or_default() { match split.next().unwrap_or_default() {
"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:" => { "MemAvailable:" => {
available_memory_kb = available_memory_kb =
@ -90,12 +120,18 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
} }
let (used_memory, total_memory) = parse_memory_info()?; let (used_memory, total_memory) = parse_memory_info()?;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let percentage_used = (used_memory / total_memory * 100.0).round() as u64; let percentage_used = (used_memory / total_memory * 100.0).round() as u64;
Ok(format!( let mut result = String::with_capacity(64);
write!(
result,
"{used_memory:.2} GiB / {total_memory:.2} GiB \ "{used_memory:.2} GiB / {total_memory:.2} GiB \
({cyan}{percentage_used}%{reset})", ({cyan}{percentage_used}%{reset})",
cyan = COLORS.cyan, cyan = COLORS.cyan,
reset = COLORS.reset, reset = COLORS.reset,
)) )
.unwrap();
Ok(result)
} }

View file

@ -1,5 +1,10 @@
use std::{io, mem::MaybeUninit}; use std::{fmt::Write, io, mem::MaybeUninit};
/// Gets the current system uptime.
///
/// # Errors
///
/// Returns an error if the system uptime cannot be retrieved.
#[cfg_attr(feature = "hotpath", hotpath::measure)] #[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_current() -> Result<String, io::Error> { pub fn get_current() -> Result<String, io::Error> {
let uptime_seconds = { let uptime_seconds = {
@ -7,6 +12,7 @@ pub fn get_current() -> Result<String, io::Error> {
if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 { if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 {
return Err(io::Error::last_os_error()); return Err(io::Error::last_os_error());
} }
#[allow(clippy::cast_sign_loss)]
unsafe { info.assume_init().uptime as u64 } unsafe { info.assume_init().uptime as u64 }
}; };
@ -16,21 +22,21 @@ pub fn get_current() -> Result<String, io::Error> {
let mut result = String::with_capacity(32); let mut result = String::with_capacity(32);
if days > 0 { if days > 0 {
result.push_str(&days.to_string()); let _ = write!(result, "{days}");
result.push_str(if days == 1 { " day" } else { " days" }); result.push_str(if days == 1 { " day" } else { " days" });
} }
if hours > 0 { if hours > 0 {
if !result.is_empty() { if !result.is_empty() {
result.push_str(", "); result.push_str(", ");
} }
result.push_str(&hours.to_string()); let _ = write!(result, "{hours}");
result.push_str(if hours == 1 { " hour" } else { " hours" }); result.push_str(if hours == 1 { " hour" } else { " hours" });
} }
if minutes > 0 { if minutes > 0 {
if !result.is_empty() { if !result.is_empty() {
result.push_str(", "); result.push_str(", ");
} }
result.push_str(&minutes.to_string()); let _ = write!(result, "{minutes}");
result.push_str(if minutes == 1 { " minute" } else { " minutes" }); result.push_str(if minutes == 1 { " minute" } else { " minutes" });
} }
if result.is_empty() { if result.is_empty() {