From daa7ea6d0dad1895768d3a1cdc62911b15c72a94 Mon Sep 17 00:00:00 2001 From: Thia Wyrod Date: Sun, 21 Nov 2021 16:03:33 -0800 Subject: [PATCH] aya: remove unnecessary usage of &dyn trait in favor of impl trait. This should improve performance in most situations by eliminating unnecessary fat pointer indirection. --- aya/src/maps/array/program_array.rs | 4 ++-- aya/src/maps/hash_map/hash_map.rs | 2 +- aya/src/maps/hash_map/per_cpu_hash_map.rs | 2 +- aya/src/maps/mod.rs | 12 ++++++------ aya/src/maps/sock/sock_hash.rs | 2 +- aya/src/maps/stack_trace.rs | 4 ++-- aya/src/programs/mod.rs | 6 ++++++ aya/src/util.rs | 2 +- 8 files changed, 20 insertions(+), 14 deletions(-) diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index 9b2ae51d..19627f77 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -28,7 +28,7 @@ use crate::{ /// ```no_run /// # let bpf = aya::Bpf::load(&[])?; /// use aya::maps::ProgramArray; -/// use aya::programs::CgroupSkb; +/// use aya::programs::{CgroupSkb, ProgramFd}; /// use std::convert::{TryFrom, TryInto}; /// /// let mut prog_array = ProgramArray::try_from(bpf.map_mut("JUMP_TABLE")?)?; @@ -98,7 +98,7 @@ impl + DerefMut> ProgramArray { /// /// When an eBPF program calls `bpf_tail_call(ctx, prog_array, index)`, control /// flow will jump to `program`. - pub fn set(&mut self, index: u32, program: &dyn ProgramFd, flags: u64) -> Result<(), MapError> { + pub fn set(&mut self, index: u32, program: impl ProgramFd, flags: u64) -> Result<(), MapError> { let fd = self.inner.fd_or_err()?; self.check_bounds(index)?; let prog_fd = program.fd().ok_or(MapError::ProgramNotLoaded)?; diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index fc841f34..170226a7 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -75,7 +75,7 @@ impl, K: Pod, V: Pod> HashMap { /// An iterator visiting all key-value pairs in arbitrary order. The /// iterator item type is `Result<(K, V), MapError>`. - pub unsafe fn iter(&self) -> MapIter<'_, K, V> { + pub unsafe fn iter(&self) -> MapIter<'_, K, V, Self> { MapIter::new(self) } diff --git a/aya/src/maps/hash_map/per_cpu_hash_map.rs b/aya/src/maps/hash_map/per_cpu_hash_map.rs index 34b8453c..77eb76a2 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -87,7 +87,7 @@ impl, K: Pod, V: Pod> PerCpuHashMap { /// An iterator visiting all key-value pairs in arbitrary order. The /// iterator item type is `Result<(K, PerCpuValues), MapError>`. - pub unsafe fn iter(&self) -> MapIter<'_, K, PerCpuValues> { + pub unsafe fn iter(&self) -> MapIter<'_, K, PerCpuValues, Self> { MapIter::new(self) } diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index d99375b7..4b0382d4 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -241,7 +241,7 @@ impl Drop for Map { } } -pub(crate) trait IterableMap { +pub trait IterableMap { fn map(&self) -> ⤅ unsafe fn get(&self, key: &K) -> Result; @@ -302,14 +302,14 @@ impl Iterator for MapKeys<'_, K> { } /// Iterator returned by `map.iter()`. -pub struct MapIter<'coll, K: Pod, V> { +pub struct MapIter<'coll, K: Pod, V, I: IterableMap> { keys: MapKeys<'coll, K>, - map: &'coll dyn IterableMap, + map: &'coll I, _v: PhantomData, } -impl<'coll, K: Pod, V> MapIter<'coll, K, V> { - fn new(map: &'coll dyn IterableMap) -> MapIter<'coll, K, V> { +impl<'coll, K: Pod, V, I: IterableMap> MapIter<'coll, K, V, I> { + fn new(map: &'coll I) -> MapIter<'coll, K, V, I> { MapIter { keys: MapKeys::new(map.map()), map, @@ -318,7 +318,7 @@ impl<'coll, K: Pod, V> MapIter<'coll, K, V> { } } -impl Iterator for MapIter<'_, K, V> { +impl> Iterator for MapIter<'_, K, V, I> { type Item = Result<(K, V), MapError>; fn next(&mut self) -> Option { diff --git a/aya/src/maps/sock/sock_hash.rs b/aya/src/maps/sock/sock_hash.rs index 7e06173c..331cf225 100644 --- a/aya/src/maps/sock/sock_hash.rs +++ b/aya/src/maps/sock/sock_hash.rs @@ -101,7 +101,7 @@ impl, K: Pod> SockHash { /// An iterator visiting all key-value pairs in arbitrary order. The /// iterator item type is `Result<(K, V), MapError>`. - pub unsafe fn iter(&self) -> MapIter<'_, K, RawFd> { + pub unsafe fn iter(&self) -> MapIter<'_, K, RawFd, Self> { MapIter::new(self) } diff --git a/aya/src/maps/stack_trace.rs b/aya/src/maps/stack_trace.rs index aa006b24..6a24b8fc 100644 --- a/aya/src/maps/stack_trace.rs +++ b/aya/src/maps/stack_trace.rs @@ -140,7 +140,7 @@ impl> StackTraceMap { /// An iterator visiting all (`stack_id`, `stack_trace`) pairs in arbitrary order. The /// iterator item type is `Result<(u32, StackTrace), MapError>`. - pub fn iter(&self) -> MapIter<'_, u32, StackTrace> { + pub fn iter(&self) -> MapIter<'_, u32, StackTrace, Self> { MapIter::new(self) } @@ -179,7 +179,7 @@ impl TryFrom for StackTraceMap { impl<'a, T: Deref> IntoIterator for &'a StackTraceMap { type Item = Result<(u32, StackTrace), MapError>; - type IntoIter = MapIter<'a, u32, StackTrace>; + type IntoIter = MapIter<'a, u32, StackTrace, StackTraceMap>; fn into_iter(self) -> Self::IntoIter { self.iter() diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index 4964aa66..08d529d5 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -582,6 +582,12 @@ impl ProgramFd for Program { } } +impl<'a, P: ProgramFd> ProgramFd for &'a P { + fn fd(&self) -> Option { + (*self).fd() + } +} + macro_rules! impl_program_fd { ($($struct_name:ident),+ $(,)?) => { $( diff --git a/aya/src/util.rs b/aya/src/util.rs index e0db8da7..a2496c90 100644 --- a/aya/src/util.rs +++ b/aya/src/util.rs @@ -76,7 +76,7 @@ pub fn kernel_symbols() -> Result, io::Error> { parse_kernel_symbols(&mut reader) } -fn parse_kernel_symbols(reader: &mut dyn BufRead) -> Result, io::Error> { +fn parse_kernel_symbols(reader: impl BufRead) -> Result, io::Error> { let mut syms = BTreeMap::new(); for line in reader.lines() {