Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/workflows/hotpath-comment.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 63 additions & 0 deletions .github/workflows/hotpath-profile.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "microfetch"
version = "0.4.9"
version = "0.4.10"
edition = "2024"

[lib]
Expand Down
45 changes: 34 additions & 11 deletions src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,45 @@ impl Colors {
}

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();
Colors::new(is_no_color)
});

#[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn print_dots() -> String {
format!(
"{} {} {} {} {} {} {}",
COLORS.blue,
COLORS.cyan,
COLORS.green,
COLORS.yellow,
COLORS.red,
COLORS.magenta,
COLORS.reset,
)
// Pre-calculate capacity: 6 color codes + " " (glyph + 2 spaces) per color
const GLYPH: &str = "";
let capacity = COLORS.blue.len()
+ COLORS.cyan.len()
+ COLORS.green.len()
+ COLORS.yellow.len()
+ COLORS.red.len()
+ COLORS.magenta.len()
+ 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
}
43 changes: 25 additions & 18 deletions src/desktop.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
use std::fmt::Write;

#[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_desktop_info() -> String {
// Retrieve the environment variables and handle Result types
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 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();
}
let desktop_str = match desktop_env {
Err(_) => "Unknown",
Ok(ref s) if s.starts_with("none+") => &s[5..],
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(),
let backend_str = match display_backend {
Err(_) => "Unknown",
Ok(ref s) if s.is_empty() => "Unknown",
Ok(ref s) => s.as_str(),
};

// Handle the case where display_backend might be empty after capitalization
let display_backend = if display_backend.is_empty() {
"Unknown"
} else {
&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()..]);
}
.to_owned();

format!("{desktop_env} ({display_backend})")
result.push(')');
result
}
33 changes: 22 additions & 11 deletions src/release.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
use std::{
fmt::Write as _,
fs::File,
io::{self, BufRead, BufReader},
io::{self, Read},
};

use nix::sys::utsname::UtsName;

#[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)]
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")
)
let sysname = utsname.sysname().to_str().unwrap_or("Unknown");
let release = utsname.release().to_str().unwrap_or("Unknown");
let machine = 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)]
pub fn get_os_pretty_name() -> Result<String, io::Error> {
let file = File::open("/etc/os-release")?;
let reader = BufReader::new(file);
// We use a stack-allocated buffer here, which seems to perform MUCH better
// 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() {
let line = line?;
for line in buffer.lines() {
if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") {
if let Some(trimmed) = pretty_name
.strip_prefix('"')
Expand Down
Loading