|
1 | 1 | use metrics::gauge; |
2 | 2 | use rustc_hash::FxHashMap; |
| 3 | +use std::hash::BuildHasherDefault; |
3 | 4 |
|
4 | 5 | #[derive(thiserror::Error, Debug)] |
5 | 6 | /// Errors produced by functions in this module |
@@ -41,23 +42,29 @@ pub(crate) async fn poll() -> Result<(), Error> { |
41 | 42 | /// |
42 | 43 | /// Function errors if the file is malformed or contains unparseable values. |
43 | 44 | #[inline] |
44 | | -fn proc_vmstat_inner(contents: &str) -> Result<FxHashMap<String, u64>, Error> { |
45 | | - let mut vmstat_data = FxHashMap::default(); |
| 45 | +fn proc_vmstat_inner(contents: &str) -> Result<FxHashMap<&str, u64>, Error> { |
| 46 | + let mut vmstat_data = FxHashMap::with_capacity_and_hasher(128, BuildHasherDefault::default()); |
46 | 47 |
|
47 | 48 | for line in contents.lines() { |
48 | 49 | let line = line.trim(); |
49 | 50 | if line.is_empty() { |
50 | 51 | continue; |
51 | 52 | } |
52 | 53 |
|
53 | | - let parts: Vec<&str> = line.split_whitespace().collect(); |
54 | | - if parts.len() != 2 { |
55 | | - return Err(Error::Malformed("line does not contain exactly two fields")); |
| 54 | + // Avoid Vec allocation by using iterator directly |
| 55 | + let mut parts = line.split_whitespace(); |
| 56 | + let field_name = parts |
| 57 | + .next() |
| 58 | + .ok_or(Error::Malformed("line does not contain field name"))?; |
| 59 | + let value_str = parts |
| 60 | + .next() |
| 61 | + .ok_or(Error::Malformed("line does not contain value"))?; |
| 62 | + |
| 63 | + if parts.next().is_some() { |
| 64 | + return Err(Error::Malformed("line contains more than two fields")); |
56 | 65 | } |
57 | 66 |
|
58 | | - let field_name = parts[0].to_string(); |
59 | | - let value = parts[1].parse::<u64>()?; |
60 | | - |
| 67 | + let value = value_str.parse::<u64>()?; |
61 | 68 | vmstat_data.insert(field_name, value); |
62 | 69 | } |
63 | 70 |
|
|
0 commit comments