From 89ef97e8482d1d0c1bb243441d911f688e183315 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 1 Aug 2023 19:10:54 -0400 Subject: [PATCH] aya: preallocate the vector This code badly needs tests :( --- aya/src/programs/uprobe.rs | 55 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/aya/src/programs/uprobe.rs b/aya/src/programs/uprobe.rs index bab82d4b..891e9a07 100644 --- a/aya/src/programs/uprobe.rs +++ b/aya/src/programs/uprobe.rs @@ -337,33 +337,34 @@ impl LdSoCache { 0 }; - let mut entries = Vec::new(); - for _ in 0..num_entries { - let flags = read_i32(&mut cursor)?; - let k_pos = read_u32(&mut cursor)? as usize; - let v_pos = read_u32(&mut cursor)? as usize; - - if new_format { - cursor.consume(12); - } - - let key = unsafe { - CStr::from_ptr(cursor.get_ref()[offset + k_pos..].as_ptr() as *const c_char) - } - .to_string_lossy() - .into_owned(); - let value = unsafe { - CStr::from_ptr(cursor.get_ref()[offset + v_pos..].as_ptr() as *const c_char) - } - .to_string_lossy() - .into_owned(); - - entries.push(CacheEntry { - key, - value, - _flags: flags, - }); - } + let entries = (0..num_entries) + .map(|_: u32| { + let flags = read_i32(&mut cursor)?; + let k_pos = read_u32(&mut cursor)? as usize; + let v_pos = read_u32(&mut cursor)? as usize; + + if new_format { + cursor.consume(12); + } + + let read_str = |pos| { + unsafe { + CStr::from_ptr(cursor.get_ref()[offset + pos..].as_ptr() as *const c_char) + } + .to_string_lossy() + .into_owned() + }; + + let key = read_str(k_pos); + let value = read_str(v_pos); + + Ok::<_, io::Error>(CacheEntry { + key, + value, + _flags: flags, + }) + }) + .collect::>()?; Ok(LdSoCache { entries }) }