Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce short-lived allocations in memory maps parsing #331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions procfs-core/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,24 @@ pub struct MemoryMaps(pub Vec<MemoryMap>);

impl crate::FromBufRead for MemoryMaps {
/// The data should be formatted according to procfs /proc/pid/{maps,smaps,smaps_rollup}.
fn from_buf_read<R: BufRead>(reader: R) -> ProcResult<Self> {
let mut memory_maps = Vec::new();

let mut line_iter = reader.lines().map(|r| r.map_err(|_| ProcError::Incomplete(None)));
fn from_buf_read<R: BufRead>(mut reader: R) -> ProcResult<Self> {
let mut memory_maps = Vec::with_capacity(10);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an educate guess that I cross-checked with my dev box:

$ for maps in /proc/*/maps; do sudo cat $maps | wc -l; done 2>/dev/null | grep -v 0 | sort | uniq -c | sort -k2h
      6 23
      1 26
     12 31
      1 33
      2 36
      2 45
      2 57
      1 59
      1 63
      1 78
      1 82
      1 83
[...]

let mut current_memory_map: Option<MemoryMap> = None;
while let Some(line) = line_iter.next().transpose()? {
let mut line = String::with_capacity(100);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on:

len("55fa56b4f000-55fa56b51000 r--p 00000000 00:1f 5749885190                 /usr/bin/cat")
85


loop {
line.clear();
match reader.read_line(&mut line) {
// End of file.
Ok(0) => break,
Ok(_) => {},
Err(_) => return Err(ProcError::Incomplete(None)),
}

if line.is_empty() {
break;
}

// Assumes all extension fields (in `/proc/<pid>/smaps`) start with a capital letter,
// which seems to be the case.
if line.starts_with(|c: char| c.is_ascii_uppercase()) {
Expand Down
Loading