mirror of
https://github.com/NotAShelf/microfetch.git
synced 2025-11-03 07:26:35 +00:00
perf: pre-allocate strings when reading files
This commit is contained in:
parent
50827c1847
commit
407d98f3fa
2 changed files with 16 additions and 6 deletions
|
|
@ -1,6 +1,8 @@
|
|||
use color_eyre::Result;
|
||||
use std::fs::read_to_string;
|
||||
use std::io;
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{self, Read},
|
||||
};
|
||||
|
||||
pub fn get_system_info() -> nix::Result<String> {
|
||||
let utsname = nix::sys::utsname::uname()?;
|
||||
|
|
@ -13,7 +15,9 @@ pub fn get_system_info() -> nix::Result<String> {
|
|||
}
|
||||
|
||||
pub fn get_os_pretty_name() -> Result<String, io::Error> {
|
||||
let os_release_content = read_to_string("/etc/os-release")?;
|
||||
let mut os_release_content = String::with_capacity(1024);
|
||||
File::open("/etc/os-release")?.read_to_string(&mut os_release_content)?;
|
||||
|
||||
let pretty_name = os_release_content
|
||||
.lines()
|
||||
.find(|line| line.starts_with("PRETTY_NAME="))
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
use color_eyre::Result;
|
||||
use nix::sys::statvfs::statvfs;
|
||||
|
||||
use std::env;
|
||||
use std::io::{self};
|
||||
use std::{
|
||||
env,
|
||||
fs::File,
|
||||
io::{self, Read},
|
||||
};
|
||||
|
||||
use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW};
|
||||
|
||||
|
|
@ -45,7 +48,10 @@ 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("");
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue