From e351a625e074d2a5d22b1a3e7fc33f7f4aa71b3f Mon Sep 17 00:00:00 2001 From: Yury Korolev Date: Sat, 17 Jun 2023 04:13:15 +0300 Subject: [PATCH] Fast retain. Refs #1 --- cidre/src/arc.rs | 5 ++++- cidre/src/objc.rs | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cidre/src/arc.rs b/cidre/src/arc.rs index a36338f4..619c1d28 100644 --- a/cidre/src/arc.rs +++ b/cidre/src/arc.rs @@ -3,7 +3,6 @@ use crate::objc; #[cfg(feature = "objc")] use std::{ - arch::asm, ops::{Deref, DerefMut}, ptr::NonNull, }; @@ -177,6 +176,8 @@ pub type Rar = ReturnedAutoReleased; #[cfg(feature = "objc")] #[inline] pub fn rar_retain_option(id: Option>) -> Option> { + use std::arch::asm; + unsafe { // see comments in rar_retain asm!("mov x29, x29"); @@ -189,6 +190,8 @@ pub fn rar_retain_option(id: Option>) -> Option> { #[cfg(feature = "objc")] #[inline] pub fn rar_retain(id: Rar) -> R { + use std::arch::asm; + unsafe { // latest runtimes don't need this marker anymore. // see https://developer.apple.com/videos/play/wwdc2022/110363/ at 13:24 diff --git a/cidre/src/objc.rs b/cidre/src/objc.rs index a0aacd54..9a0d08b4 100644 --- a/cidre/src/objc.rs +++ b/cidre/src/objc.rs @@ -73,7 +73,15 @@ impl arc::Retain for T { pub trait Obj: Sized + arc::Retain { #[inline] unsafe fn retain(id: &Self) -> arc::R { - transmute(objc_retain(transmute(id))) + let result: *mut Self; + core::arch::asm!( + "bl _objc_retain_{obj:x}", + obj = in(reg) id, + lateout("x0") result, + clobber_abi("C"), + ); + transmute(result) + // transmute(objc_retain(transmute(id))) } #[msg_send(description)] @@ -158,7 +166,7 @@ where #[link(name = "objc", kind = "dylib")] extern "C" { - fn objc_retain<'a>(obj: &Id) -> &'a Id; + // fn objc_retain<'a>(obj: &Id) -> &'a Id; // fn objc_release(obj: &mut Id); fn class_createInstance(cls: &Class, extra_bytes: usize) -> Option>;