Skip to content

Commit 637adb0

Browse files
committed
Remove parsing allocations from vmstat observer
1 parent b2aad39 commit 637adb0

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

lading/src/observer/linux/procfs/vmstat.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use metrics::gauge;
22
use rustc_hash::FxHashMap;
3+
use std::hash::BuildHasherDefault;
34

45
#[derive(thiserror::Error, Debug)]
56
/// Errors produced by functions in this module
@@ -41,23 +42,29 @@ pub(crate) async fn poll() -> Result<(), Error> {
4142
///
4243
/// Function errors if the file is malformed or contains unparseable values.
4344
#[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());
4647

4748
for line in contents.lines() {
4849
let line = line.trim();
4950
if line.is_empty() {
5051
continue;
5152
}
5253

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"));
5665
}
5766

58-
let field_name = parts[0].to_string();
59-
let value = parts[1].parse::<u64>()?;
60-
67+
let value = value_str.parse::<u64>()?;
6168
vmstat_data.insert(field_name, value);
6269
}
6370

0 commit comments

Comments
 (0)