Skip to content

Commit

Permalink
Merge pull request #743 from aya-rs/avoid-vec-ksyms
Browse files Browse the repository at this point in the history
util: avoid vector allocation when parsing ksyms
  • Loading branch information
tamird committed Aug 10, 2023
2 parents 0c0cf70 + 5138c73 commit 90cf131
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions aya/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::{
ffi::{CStr, CString},
fs::{self, File},
io::{self, BufRead, BufReader},
mem, slice,
mem,
num::ParseIntError,
slice,
str::{FromStr, Utf8Error},
};

Expand Down Expand Up @@ -210,18 +212,24 @@ pub fn kernel_symbols() -> Result<BTreeMap<u64, String>, io::Error> {
}

fn parse_kernel_symbols(reader: impl BufRead) -> Result<BTreeMap<u64, String>, io::Error> {
let mut syms = BTreeMap::new();

for line in reader.lines() {
let line = line?;
let parts = line.splitn(4, ' ').collect::<Vec<_>>();
let addr = u64::from_str_radix(parts[0], 16)
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, line.clone()))?;
let name = parts[2].to_owned();
syms.insert(addr, name);
}

Ok(syms)
reader
.lines()
.map(|line| {
let line = line?;
(|| {
let mut parts = line.splitn(4, ' ');
let addr = parts.next()?;
let _kind = parts.next()?;
let name = parts.next()?;
let addr = match u64::from_str_radix(addr, 16) {
Ok(addr) => Some(addr),
Err(ParseIntError { .. }) => None,
}?;
Some((addr, name.to_owned()))
})()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, line.clone()))
})
.collect()
}

/// Returns the prefix used by syscalls.
Expand Down

0 comments on commit 90cf131

Please sign in to comment.