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.
pull/115/head
Thia Wyrod 3 years ago
parent 98b36b23bc
commit daa7ea6d0d
No known key found for this signature in database
GPG Key ID: 55D3AB7E5658CA0C

@ -28,7 +28,7 @@ use crate::{
/// ```no_run /// ```no_run
/// # let bpf = aya::Bpf::load(&[])?; /// # let bpf = aya::Bpf::load(&[])?;
/// use aya::maps::ProgramArray; /// use aya::maps::ProgramArray;
/// use aya::programs::CgroupSkb; /// use aya::programs::{CgroupSkb, ProgramFd};
/// use std::convert::{TryFrom, TryInto}; /// use std::convert::{TryFrom, TryInto};
/// ///
/// let mut prog_array = ProgramArray::try_from(bpf.map_mut("JUMP_TABLE")?)?; /// let mut prog_array = ProgramArray::try_from(bpf.map_mut("JUMP_TABLE")?)?;
@ -98,7 +98,7 @@ impl<T: Deref<Target = Map> + DerefMut<Target = Map>> ProgramArray<T> {
/// ///
/// When an eBPF program calls `bpf_tail_call(ctx, prog_array, index)`, control /// When an eBPF program calls `bpf_tail_call(ctx, prog_array, index)`, control
/// flow will jump to `program`. /// 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()?; let fd = self.inner.fd_or_err()?;
self.check_bounds(index)?; self.check_bounds(index)?;
let prog_fd = program.fd().ok_or(MapError::ProgramNotLoaded)?; let prog_fd = program.fd().ok_or(MapError::ProgramNotLoaded)?;

@ -75,7 +75,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> HashMap<T, K, V> {
/// An iterator visiting all key-value pairs in arbitrary order. The /// An iterator visiting all key-value pairs in arbitrary order. The
/// iterator item type is `Result<(K, V), MapError>`. /// 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) MapIter::new(self)
} }

@ -87,7 +87,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
/// An iterator visiting all key-value pairs in arbitrary order. The /// An iterator visiting all key-value pairs in arbitrary order. The
/// iterator item type is `Result<(K, PerCpuValues<V>), MapError>`. /// iterator item type is `Result<(K, PerCpuValues<V>), MapError>`.
pub unsafe fn iter(&self) -> MapIter<'_, K, PerCpuValues<V>> { pub unsafe fn iter(&self) -> MapIter<'_, K, PerCpuValues<V>, Self> {
MapIter::new(self) MapIter::new(self)
} }

@ -241,7 +241,7 @@ impl Drop for Map {
} }
} }
pub(crate) trait IterableMap<K: Pod, V> { pub trait IterableMap<K: Pod, V> {
fn map(&self) -> &Map; fn map(&self) -> &Map;
unsafe fn get(&self, key: &K) -> Result<V, MapError>; unsafe fn get(&self, key: &K) -> Result<V, MapError>;
@ -302,14 +302,14 @@ impl<K: Pod> Iterator for MapKeys<'_, K> {
} }
/// Iterator returned by `map.iter()`. /// Iterator returned by `map.iter()`.
pub struct MapIter<'coll, K: Pod, V> { pub struct MapIter<'coll, K: Pod, V, I: IterableMap<K, V>> {
keys: MapKeys<'coll, K>, keys: MapKeys<'coll, K>,
map: &'coll dyn IterableMap<K, V>, map: &'coll I,
_v: PhantomData<V>, _v: PhantomData<V>,
} }
impl<'coll, K: Pod, V> MapIter<'coll, K, V> { impl<'coll, K: Pod, V, I: IterableMap<K, V>> MapIter<'coll, K, V, I> {
fn new(map: &'coll dyn IterableMap<K, V>) -> MapIter<'coll, K, V> { fn new(map: &'coll I) -> MapIter<'coll, K, V, I> {
MapIter { MapIter {
keys: MapKeys::new(map.map()), keys: MapKeys::new(map.map()),
map, map,
@ -318,7 +318,7 @@ impl<'coll, K: Pod, V> MapIter<'coll, K, V> {
} }
} }
impl<K: Pod, V> Iterator for MapIter<'_, K, V> { impl<K: Pod, V, I: IterableMap<K, V>> Iterator for MapIter<'_, K, V, I> {
type Item = Result<(K, V), MapError>; type Item = Result<(K, V), MapError>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {

@ -101,7 +101,7 @@ impl<T: Deref<Target = Map>, K: Pod> SockHash<T, K> {
/// An iterator visiting all key-value pairs in arbitrary order. The /// An iterator visiting all key-value pairs in arbitrary order. The
/// iterator item type is `Result<(K, V), MapError>`. /// 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) MapIter::new(self)
} }

@ -140,7 +140,7 @@ impl<T: Deref<Target = Map>> StackTraceMap<T> {
/// An iterator visiting all (`stack_id`, `stack_trace`) pairs in arbitrary order. The /// An iterator visiting all (`stack_id`, `stack_trace`) pairs in arbitrary order. The
/// iterator item type is `Result<(u32, StackTrace), MapError>`. /// 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) MapIter::new(self)
} }
@ -179,7 +179,7 @@ impl TryFrom<MapRefMut> for StackTraceMap<MapRefMut> {
impl<'a, T: Deref<Target = Map>> IntoIterator for &'a StackTraceMap<T> { impl<'a, T: Deref<Target = Map>> IntoIterator for &'a StackTraceMap<T> {
type Item = Result<(u32, StackTrace), MapError>; type Item = Result<(u32, StackTrace), MapError>;
type IntoIter = MapIter<'a, u32, StackTrace>; type IntoIter = MapIter<'a, u32, StackTrace, StackTraceMap<T>>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
self.iter() self.iter()

@ -582,6 +582,12 @@ impl ProgramFd for Program {
} }
} }
impl<'a, P: ProgramFd> ProgramFd for &'a P {
fn fd(&self) -> Option<RawFd> {
(*self).fd()
}
}
macro_rules! impl_program_fd { macro_rules! impl_program_fd {
($($struct_name:ident),+ $(,)?) => { ($($struct_name:ident),+ $(,)?) => {
$( $(

@ -76,7 +76,7 @@ pub fn kernel_symbols() -> Result<BTreeMap<u64, String>, io::Error> {
parse_kernel_symbols(&mut reader) parse_kernel_symbols(&mut reader)
} }
fn parse_kernel_symbols(reader: &mut dyn BufRead) -> Result<BTreeMap<u64, String>, io::Error> { fn parse_kernel_symbols(reader: impl BufRead) -> Result<BTreeMap<u64, String>, io::Error> {
let mut syms = BTreeMap::new(); let mut syms = BTreeMap::new();
for line in reader.lines() { for line in reader.lines() {

Loading…
Cancel
Save