mirror of
https://github.com/NotAShelf/microfetch.git
synced 2025-11-02 23:16:37 +00:00
perf: break early after parsing required meminfo
Also a match statement for compiler magic.
This commit is contained in:
parent
cb9703f820
commit
50827c1847
1 changed files with 10 additions and 4 deletions
|
|
@ -48,10 +48,16 @@ pub fn get_memory_usage() -> Result<String, io::Error> {
|
|||
for line in std::fs::read_to_string("/proc/meminfo")?.lines() {
|
||||
let mut split = line.split_whitespace();
|
||||
let key = split.next().unwrap_or("");
|
||||
if key == "MemTotal:" {
|
||||
total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0);
|
||||
} else if key == "MemAvailable:" {
|
||||
available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0);
|
||||
|
||||
match key {
|
||||
"MemTotal:" => total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0),
|
||||
"MemAvailable:" => {
|
||||
available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0);
|
||||
|
||||
// MemTotal comes before MemAvailable, stop parsing
|
||||
break;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue