From 272022bdac3a401e7c6c0d08a99d04940ebe54e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 5 Aug 2020 10:30:05 +0300 Subject: [PATCH] gstreamer/pad: Factor out PadProbeInfo handling from pad probe trampoline into separate functions This makes that part of the code non-generic and thus allows the compiler to not put a copy of it into every caller with a different closure. For a test with 3 pad probes this overall reduced the number of LLVM IR lines needed for the pad probes to about 8.5% of what it was before (4485 -> 381 lines). --- gstreamer/src/pad.rs | 52 +++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/gstreamer/src/pad.rs b/gstreamer/src/pad.rs index bd735c453..28ac4a9b0 100644 --- a/gstreamer/src/pad.rs +++ b/gstreamer/src/pad.rs @@ -1070,21 +1070,11 @@ impl> PadExtManual for O { } } -unsafe extern "C" fn trampoline_pad_probe< - T, - F: Fn(&T, &mut PadProbeInfo) -> PadProbeReturn + Send + Sync + 'static, ->( - pad: *mut gst_sys::GstPad, +unsafe fn create_probe_info<'a>( info: *mut gst_sys::GstPadProbeInfo, - func: gpointer, -) -> gst_sys::GstPadProbeReturn -where - T: IsA, -{ - let func: &F = &*(func as *const F); +) -> (PadProbeInfo<'a>, Option) { let mut data_type = None; - - let mut probe_info = PadProbeInfo { + let info = PadProbeInfo { mask: from_glib((*info).type_), id: PadProbeId(NonZeroU64::new_unchecked((*info).id as u64)), offset: (*info).offset, @@ -1120,12 +1110,15 @@ where }, flow_ret: from_glib((*info).ABI.abi.flow_ret), }; + (info, data_type) +} - let ret = func( - &Pad::from_glib_borrow(pad).unsafe_cast_ref(), - &mut probe_info, - ); - +unsafe fn update_probe_info( + ret: PadProbeReturn, + probe_info: PadProbeInfo, + data_type: Option, + info: *mut gst_sys::GstPadProbeInfo, +) { if ret == PadProbeReturn::Handled { // Handled queries need to be returned // Handled buffers are consumed @@ -1177,6 +1170,29 @@ where } (*info).ABI.abi.flow_ret = probe_info.flow_ret.to_glib(); +} + +unsafe extern "C" fn trampoline_pad_probe< + T, + F: Fn(&T, &mut PadProbeInfo) -> PadProbeReturn + Send + Sync + 'static, +>( + pad: *mut gst_sys::GstPad, + info: *mut gst_sys::GstPadProbeInfo, + func: gpointer, +) -> gst_sys::GstPadProbeReturn +where + T: IsA, +{ + let func: &F = &*(func as *const F); + + let (mut probe_info, data_type) = create_probe_info(info); + + let ret = func( + &Pad::from_glib_borrow(pad).unsafe_cast_ref(), + &mut probe_info, + ); + + update_probe_info(ret, probe_info, data_type, info); ret.to_glib() }