From f8a0070986dda8c7554dcf769fda99cd129e039f Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 17 Nov 2025 19:19:28 +0300 Subject: [PATCH] uptime: optimize uptime calculation w/ inline assembly and custom itoa lol, lmao even. Signed-off-by: NotAShelf Change-Id: Ie22fbd2e9c2be5740b493bdc81caafb36a6a6964 --- src/uptime.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/src/uptime.rs b/src/uptime.rs index 52188c2..98c9207 100644 --- a/src/uptime.rs +++ b/src/uptime.rs @@ -1,4 +1,66 @@ -use std::{fmt::Write, io, mem::MaybeUninit}; +use std::{io, mem::MaybeUninit}; + +/// Fast integer to string conversion (no formatting overhead) +#[inline] +fn itoa(mut n: u64, buf: &mut [u8]) -> &str { + if n == 0 { + return "0"; + } + + let mut i = buf.len(); + while n > 0 { + i -= 1; + buf[i] = b'0' + (n % 10) as u8; + n /= 10; + } + + unsafe { std::str::from_utf8_unchecked(&buf[i..]) } +} + +/// Direct sysinfo syscall using inline assembly +/// +/// # Safety +/// This function uses inline assembly to make a direct syscall. +/// The caller must ensure the sysinfo pointer is valid. +#[inline] +unsafe fn sys_sysinfo(info: *mut libc::sysinfo) -> i64 { + #[cfg(target_arch = "x86_64")] + { + let ret: i64; + unsafe { + std::arch::asm!( + "syscall", + in("rax") 99_i64, // __NR_sysinfo + in("rdi") info, + out("rcx") _, + out("r11") _, + lateout("rax") ret, + options(nostack) + ); + } + ret + } + + #[cfg(target_arch = "aarch64")] + { + let ret: i64; + unsafe { + std::arch::asm!( + "svc #0", + in("x8") 179_i64, // __NR_sysinfo + in("x0") info, + lateout("x0") ret, + options(nostack) + ); + } + ret + } + + #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))] + { + unsafe { libc::sysinfo(info) as i64 } + } +} /// Gets the current system uptime. /// @@ -9,7 +71,7 @@ use std::{fmt::Write, io, mem::MaybeUninit}; pub fn get_current() -> Result { let uptime_seconds = { let mut info = MaybeUninit::uninit(); - if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 { + if unsafe { sys_sysinfo(info.as_mut_ptr()) } != 0 { return Err(io::Error::last_os_error()); } #[allow(clippy::cast_sign_loss)] @@ -23,22 +85,24 @@ pub fn get_current() -> Result { let minutes = (uptime_seconds / 60) % 60; let mut result = String::with_capacity(32); + let mut buf = [0u8; 20]; // Enough for u64::MAX + if days > 0 { - let _ = write!(result, "{days}"); + result.push_str(itoa(days, &mut buf)); result.push_str(if days == 1 { " day" } else { " days" }); } if hours > 0 { if !result.is_empty() { result.push_str(", "); } - let _ = write!(result, "{hours}"); + result.push_str(itoa(hours, &mut buf)); result.push_str(if hours == 1 { " hour" } else { " hours" }); } if minutes > 0 { if !result.is_empty() { result.push_str(", "); } - let _ = write!(result, "{minutes}"); + result.push_str(itoa(minutes, &mut buf)); result.push_str(if minutes == 1 { " minute" } else { " minutes" }); } if result.is_empty() {