From 5138c731a92a8e5107e41829573617fc624ea9c7 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 10 Aug 2023 11:58:27 -0400 Subject: [PATCH] util: avoid vector allocation when parsing ksyms --- aya/src/util.rs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/aya/src/util.rs b/aya/src/util.rs index 4db89af8..65bcb415 100644 --- a/aya/src/util.rs +++ b/aya/src/util.rs @@ -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}, }; @@ -210,18 +212,24 @@ pub fn kernel_symbols() -> Result, io::Error> { } fn parse_kernel_symbols(reader: impl BufRead) -> Result, io::Error> { - let mut syms = BTreeMap::new(); - - for line in reader.lines() { - let line = line?; - let parts = line.splitn(4, ' ').collect::>(); - 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.