1- use std:: {
2- fmt:: Write as _,
3- fs:: File ,
4- io:: { self , Read } ,
5- } ;
1+ use std:: { fmt:: Write as _, io} ;
62
7- use nix :: sys :: utsname :: UtsName ;
3+ use crate :: { UtsName , syscall :: read_file_fast } ;
84
95#[ must_use]
106#[ cfg_attr( feature = "hotpath" , hotpath:: measure) ]
@@ -28,21 +24,46 @@ pub fn get_system_info(utsname: &UtsName) -> String {
2824/// Returns an error if `/etc/os-release` cannot be read.
2925#[ cfg_attr( feature = "hotpath" , hotpath:: measure) ]
3026pub fn get_os_pretty_name ( ) -> Result < String , io:: Error > {
31- // We use a stack-allocated buffer here, which seems to perform MUCH better
32- // than `BufReader`. In hindsight, I should've seen this coming.
33- let mut buffer = String :: with_capacity ( 1024 ) ;
34- File :: open ( "/etc/os-release" ) ?. read_to_string ( & mut buffer) ?;
35-
36- for line in buffer. lines ( ) {
37- if let Some ( pretty_name) = line. strip_prefix ( "PRETTY_NAME=" ) {
38- if let Some ( trimmed) = pretty_name
39- . strip_prefix ( '"' )
40- . and_then ( |s| s. strip_suffix ( '"' ) )
27+ // Fast byte-level scanning for PRETTY_NAME=
28+ const PREFIX : & [ u8 ] = b"PRETTY_NAME=" ;
29+
30+ let mut buffer = [ 0u8 ; 1024 ] ;
31+
32+ // Use fast syscall-based file reading
33+ let bytes_read = read_file_fast ( "/etc/os-release" , & mut buffer) ?;
34+ let content = & buffer[ ..bytes_read] ;
35+
36+ let mut offset = 0 ;
37+
38+ while offset < content. len ( ) {
39+ let remaining = & content[ offset..] ;
40+
41+ // Find newline or end
42+ let line_end = remaining
43+ . iter ( )
44+ . position ( |& b| b == b'\n' )
45+ . unwrap_or ( remaining. len ( ) ) ;
46+ let line = & remaining[ ..line_end] ;
47+
48+ if line. starts_with ( PREFIX ) {
49+ let value = & line[ PREFIX . len ( ) ..] ;
50+
51+ // Strip quotes if present
52+ let trimmed = if value. len ( ) >= 2
53+ && value[ 0 ] == b'"'
54+ && value[ value. len ( ) - 1 ] == b'"'
4155 {
42- return Ok ( trimmed. to_owned ( ) ) ;
43- }
44- return Ok ( pretty_name. to_owned ( ) ) ;
56+ & value[ 1 ..value. len ( ) - 1 ]
57+ } else {
58+ value
59+ } ;
60+
61+ // Convert to String - should be valid UTF-8
62+ return Ok ( String :: from_utf8_lossy ( trimmed) . into_owned ( ) ) ;
4563 }
64+
65+ offset += line_end + 1 ;
4666 }
67+
4768 Ok ( "Unknown" . to_owned ( ) )
4869}
0 commit comments