From 0c99a30ed2abc19ddece0e120f9ffdf86a1d46f8 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Tue, 1 Dec 2026 19:23:03 -0400 Subject: [PATCH 1/2] perf(classic): pass in-param strings as fast-pass HSTRING references --- runtime/src/method_call.rs | 15 +++++++++++++++ runtime/src/value.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/runtime/src/method_call.rs b/runtime/src/method_call.rs index fdf5adf..07c01b4 100644 --- a/runtime/src/method_call.rs +++ b/runtime/src/method_call.rs @@ -974,6 +974,10 @@ impl MethodCall { let mut buffer_args = Vec::new(); let mut reference_arrays = Vec::new(); + // In-param strings pass as fast-pass references to a copy on the stack: no heap + // HSTRING per call. Reserved up front so no entry moves while the call holds its handle. + let string_count = self.si.parse_parameter_types.iter().filter(|t| matches!(t, NativeType::String)).count(); + let mut string_refs: SmallVec<[crate::value::StringRef; 4]> = SmallVec::with_capacity(string_count); for (i, native_type) in self.si.parse_parameter_types.iter().enumerate() { let parameter = &self.si.parameters[i]; // Use the pre-computed signature string (cached at construction time) to avoid @@ -1048,6 +1052,17 @@ impl MethodCall { let value = args.get(i as i32); + if matches!(native_type, NativeType::String) && string_refs.len() < string_refs.capacity() { + if let Ok(text) = v8::Local::::try_from(value) { + string_refs.push(crate::value::StringRef::default()); + let handle = string_refs.last_mut().unwrap().reference(scope, text); + self.argument_buf.push(NativeValue { pointer: handle }); + // A reference owns nothing, so the release pass skips it. + self.argument_parse_types.push(None); + continue; + } + } + let value = match *native_type { NativeType::Void => return (call_failure(), std::ptr::null_mut(), Vec::new()), NativeType::Bool => ffi_parse_bool_arg(value), diff --git a/runtime/src/value.rs b/runtime/src/value.rs index 0f90cc2..58f244a 100644 --- a/runtime/src/value.rs +++ b/runtime/src/value.rs @@ -383,6 +383,43 @@ fn hstring_from_v8_string(scope: &mut v8::PinScope<'_, '_>, s: v8::Local, +} + +#[cfg(feature = "classic")] +impl StringRef { + /// The raw HSTRING handle (null for an empty string); never released. + pub fn reference(&mut self, scope: &mut v8::PinScope<'_, '_>, s: v8::Local) -> *mut c_void { + let len = s.length(); + if len == 0 { + return std::ptr::null_mut(); + } + self.text.resize(len + 1, 0); + s.write_v2(scope, 0, &mut self.text[..len], v8::WriteFlags::empty()); + self.text[len] = 0; + let mut handle = std::mem::MaybeUninit::::zeroed(); + let made = unsafe { + windows::Win32::System::WinRT::WindowsCreateStringReference( + windows_core::PCWSTR(self.text.as_ptr()), + len as u32, + &mut self.header, + handle.as_mut_ptr(), + ) + }; + if made.is_err() { + return std::ptr::null_mut(); + } + // The HSTRING wrapper would delete the handle on drop; a reference needs none. + unsafe { handle.as_ptr().cast::<*mut c_void>().read() } + } +} + /// As `hstring_from_v8_string`, but coerces a non-string JS value via ToString first (matching /// `Value::to_rust_string_lossy`'s behavior). Empty HSTRING if coercion fails. #[cfg(feature = "classic")] From 77162f7c16dcf37cc1aa747dfc0efda78bf2b9f3 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Tue, 1 Dec 2026 21:05:34 -0400 Subject: [PATCH 2/2] perf(napi): pass in-param strings as fast-pass HSTRING references napi writes a string argument's UTF-16 straight into per-call storage, wrapped with WindowsCreateStringReference, instead of a Vec plus a heap HSTRING. --- runtime/src/method_call.rs | 19 +++++++++++++++++++ runtime/src/napi_engine/value.rs | 27 +++++++++++++++++++++++++++ runtime/src/value.rs | 28 ++++++++++++++++++++-------- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/runtime/src/method_call.rs b/runtime/src/method_call.rs index 07c01b4..249e6ab 100644 --- a/runtime/src/method_call.rs +++ b/runtime/src/method_call.rs @@ -1751,6 +1751,9 @@ impl MethodCall { let mut buffer_args = Vec::new(); let mut reference_arrays = Vec::new(); + // In-param strings pass as fast-pass references; reserved so no entry moves mid-call. + let string_count = self.si.parse_parameter_types.iter().filter(|t| matches!(t, NativeType::String)).count(); + let mut string_refs: SmallVec<[crate::value::StringRef; 4]> = SmallVec::with_capacity(string_count); for (i, native_type) in self.si.parse_parameter_types.iter().enumerate() { let parameter = &self.si.parameters[i]; let param_sig = &self.si.param_sigs[i]; @@ -1823,6 +1826,22 @@ impl MethodCall { }; let vt = value.get_type().unwrap_or(ValueType::Undefined); + if matches!(native_type, NativeType::String) && vt == ValueType::String && string_refs.len() < string_refs.capacity() { + string_refs.push(crate::value::StringRef::default()); + match nv::napi_string_reference(env, &value, string_refs.last_mut().unwrap()) { + Ok(reference) => { + self.argument_buf.push(reference); + // A reference owns nothing, so the release pass skips it. + self.argument_parse_types.push(None); + continue; + } + Err(e) => { + crate::error::set_argument_error(e); + return fail3(); + } + } + } + let value = match *native_type { NativeType::Void => return fail3(), NativeType::Bool => nv::napi_parse_bool(&value), diff --git a/runtime/src/napi_engine/value.rs b/runtime/src/napi_engine/value.rs index 461de3d..894f30a 100644 --- a/runtime/src/napi_engine/value.rs +++ b/runtime/src/napi_engine/value.rs @@ -217,6 +217,33 @@ pub fn napi_parse_f64(arg: &JsUnknown) -> Result { Ok(NativeValue { f64_value: f }) } +/// A string in-param as a fast-pass reference over `slot`: napi writes the UTF-16 straight into +/// it, with no `Vec` and no heap HSTRING. `slot` must stay put for the call. +pub fn napi_string_reference( + env: &Env, + arg: &JsUnknown, + slot: &mut crate::value::StringRef, +) -> Result { + if value_type(arg)? != ValueType::String { + return Err(type_error("Invalid FFI String type, expected String")); + } + let (raw_env, raw_value) = (env.raw(), unsafe { arg.raw() }); + let mut len = 0usize; + let st = unsafe { sys::napi_get_value_string_utf16(raw_env, raw_value, std::ptr::null_mut(), 0, &mut len) }; + if st != sys::Status::napi_ok { + return Err(type_error("Invalid FFI String type, expected String")); + } + let buffer = slot.buffer(len); + let mut written = 0usize; + let st = unsafe { + sys::napi_get_value_string_utf16(raw_env, raw_value, buffer.as_mut_ptr(), buffer.len(), &mut written) + }; + if st != sys::Status::napi_ok { + return Err(type_error("Invalid FFI String type, expected String")); + } + Ok(NativeValue { pointer: slot.handle(written) }) +} + #[inline] pub fn napi_parse_string(arg: &JsUnknown) -> Result { // `v8::String::try_from` requires an actual string (no ToString coercion). The rusty_v8 diff --git a/runtime/src/value.rs b/runtime/src/value.rs index 58f244a..9ad252d 100644 --- a/runtime/src/value.rs +++ b/runtime/src/value.rs @@ -385,23 +385,25 @@ fn hstring_from_v8_string(scope: &mut v8::PinScope<'_, '_>, s: v8::Local, } -#[cfg(feature = "classic")] impl StringRef { - /// The raw HSTRING handle (null for an empty string); never released. - pub fn reference(&mut self, scope: &mut v8::PinScope<'_, '_>, s: v8::Local) -> *mut c_void { - let len = s.length(); - if len == 0 { + /// Room for `len` code units and the NUL after them. + pub fn buffer(&mut self, len: usize) -> &mut [u16] { + self.text.resize(len + 1, 0); + &mut self.text[..] + } + + /// The raw HSTRING handle over the first `len` code units (null for an empty string); never + /// released. + pub fn handle(&mut self, len: usize) -> *mut c_void { + if len == 0 || self.text.len() <= len { return std::ptr::null_mut(); } - self.text.resize(len + 1, 0); - s.write_v2(scope, 0, &mut self.text[..len], v8::WriteFlags::empty()); self.text[len] = 0; let mut handle = std::mem::MaybeUninit::::zeroed(); let made = unsafe { @@ -418,6 +420,16 @@ impl StringRef { // The HSTRING wrapper would delete the handle on drop; a reference needs none. unsafe { handle.as_ptr().cast::<*mut c_void>().read() } } + + #[cfg(feature = "classic")] + pub fn reference(&mut self, scope: &mut v8::PinScope<'_, '_>, s: v8::Local) -> *mut c_void { + let len = s.length(); + if len == 0 { + return std::ptr::null_mut(); + } + s.write_v2(scope, 0, &mut self.buffer(len)[..len], v8::WriteFlags::empty()); + self.handle(len) + } } /// As `hstring_from_v8_string`, but coerces a non-string JS value via ToString first (matching