Skip to content

Commit 7661ce6

Browse files
committed
bridge: Mark error-throwing paths as #[cold]
Tiny code size improvement, possibly also a tiny happy-path performance improvement. (Branch predictors these days are very good, but it still makes a difference how the code is laid out in the binary.)
1 parent 9a46883 commit 7661ce6

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

rust/bridge/shared/types/src/ffi/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ impl SignalFfiError {
186186
pub fn downcast_ref<T: FfiError>(&self) -> Option<&T> {
187187
(*self.0).upcast_as_any().downcast_ref()
188188
}
189+
190+
#[cold]
191+
pub fn into_raw_box_for_ffi(self) -> *mut Self {
192+
Box::into_raw(Box::new(self))
193+
}
189194
}
190195

191196
/// SignalFfiError is a typed wrapper around a Box, and as such it's reasonable for it to have the

rust/bridge/shared/types/src/ffi/futures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<T: ResultTypeInfo + std::panic::UnwindSafe> ResultReporter for FutureResult
9595
std::mem::forget(value);
9696
}
9797
Err(err) => (promise.complete)(
98-
Box::into_raw(Box::new(err)),
98+
err.into_raw_box_for_ffi(),
9999
std::ptr::null(),
100100
promise.context,
101101
),

rust/bridge/shared/types/src/ffi/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,9 @@ pub fn run_ffi_safe<F: FnOnce() -> Result<(), SignalFfiError> + std::panic::Unwi
308308
Err(r) => Err(UnexpectedPanic(r).into()),
309309
};
310310

311-
// When ThinBox is stabilized, we can return that instead of double-boxing.
312-
// (Unfortunately, Box<dyn MyTrait> is two pointers wide and not FFI-safe.)
313311
match result {
314312
Ok(()) => std::ptr::null_mut(),
315-
Err(e) => Box::into_raw(Box::new(e)),
313+
Err(e) => e.into_raw_box_for_ffi(),
316314
}
317315
}
318316

rust/bridge/shared/types/src/jni/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl<'a, T> From<JavaCompletableFuture<'a, T>> for JObject<'a> {
102102
}
103103
}
104104

105+
#[cold]
105106
fn convert_to_exception<'a, 'env, F>(env: &'a mut JNIEnv<'env>, error: SignalJniError, consume: F)
106107
where
107108
F: 'a + FnOnce(&'a mut JNIEnv<'env>, Result<JThrowable<'a>, BridgeLayerError>, SignalJniError),
@@ -896,6 +897,7 @@ impl JniError for RateLimitChallenge {
896897
///
897898
/// Exceptions thrown in callbacks will be rethrown; all other errors will be mapped to an
898899
/// appropriate Java exception class and thrown.
900+
#[cold]
899901
fn throw_error(env: &mut JNIEnv, error: SignalJniError) {
900902
convert_to_exception(env, error, |env, throwable, error| match throwable {
901903
Err(failure) => log::error!("failed to create exception for {error}: {failure}"),

0 commit comments

Comments
 (0)