From d53c60e0719775f07b025eee5e52caab7efa7332 Mon Sep 17 00:00:00 2001 From: PThorpe92 Date: Thu, 17 Apr 2025 13:11:21 -0400 Subject: [PATCH] Prevent double allocations for VFilter args in vdbe --- core/lib.rs | 13 +++++-------- core/vdbe/execute.rs | 9 +++++++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/core/lib.rs b/core/lib.rs index 68384e77d..e130306f7 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -705,19 +705,16 @@ impl VirtualTable { VTabOpaqueCursor::new(cursor) } + #[tracing::instrument(skip(cursor))] pub fn filter( &self, cursor: &VTabOpaqueCursor, idx_num: i32, idx_str: Option, arg_count: usize, - args: Vec, + args: Vec, ) -> Result { - let mut filter_args = Vec::with_capacity(arg_count); - for i in 0..arg_count { - let ownedvalue_arg = args.get(i).unwrap(); - filter_args.push(ownedvalue_arg.to_ffi()); - } + tracing::trace!("xFilter"); let c_idx_str = idx_str .map(|s| std::ffi::CString::new(s).unwrap()) .map(|cstr| cstr.into_raw()) @@ -726,12 +723,12 @@ impl VirtualTable { (self.implementation.filter)( cursor.as_ptr(), arg_count as i32, - filter_args.as_ptr(), + args.as_ptr(), c_idx_str, idx_num, ) }; - for arg in filter_args { + for arg in args { unsafe { arg.__free_internal_type(); } diff --git a/core/vdbe/execute.rs b/core/vdbe/execute.rs index 9abc7c6c3..de871f54c 100644 --- a/core/vdbe/execute.rs +++ b/core/vdbe/execute.rs @@ -979,9 +979,14 @@ pub fn op_vfilter( let has_rows = { let mut cursor = state.get_cursor(*cursor_id); let cursor = cursor.as_virtual_mut(); - let mut args = Vec::new(); + let mut args = Vec::with_capacity(*arg_count); for i in 0..*arg_count { - args.push(state.registers[args_reg + i].get_owned_value().clone()); + args.push( + state.registers[args_reg + i] + .get_owned_value() + .clone() + .to_ffi(), + ); } let idx_str = if let Some(idx_str) = idx_str { Some(state.registers[*idx_str].get_owned_value().to_string())