Skip to content

Commit

Permalink
Change alloc fast path to use AllocSelector helper
Browse files Browse the repository at this point in the history
To allow us to merge `msg_send!` and `msg_send_id!` in the future.
  • Loading branch information
madsmtm committed Jan 20, 2025
1 parent 2a2be79 commit aa6e383
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
5 changes: 5 additions & 0 deletions crates/objc2/src/__macro_helpers/method_family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ pub type MutableCopyFamily = MethodFamily<5>;
/// No family.
pub type NoneFamily = MethodFamily<6>;

/// The `alloc` selector itself.
///
/// Used for a fast-path optimization using `objc_alloc`.
pub type AllocSelector = MethodFamily<7>;

/// Helper module where `#[unsafe(method_family = $family:ident)]` will import
/// its value from.
#[allow(non_camel_case_types)]
Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/__macro_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub use self::define_class::{
pub use self::defined_ivars::DefinedIvarsHelper;
pub use self::image_info::ImageInfo;
pub use self::method_family::{
method_family, method_family_import, AllocFamily, CopyFamily, InitFamily, MethodFamily, MutableCopyFamily,
NewFamily, NoneFamily,
method_family, method_family_import, AllocFamily, AllocSelector, CopyFamily, InitFamily,
MethodFamily, MutableCopyFamily, NewFamily, NoneFamily,
};
pub use self::module_info::ModuleInfo;
pub use self::msg_send::MsgSend;
Expand Down
21 changes: 14 additions & 7 deletions crates/objc2/src/__macro_helpers/msg_send_retained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{sel, ClassType, DefinedClass, Message};
use super::defined_ivars::set_finalized;
use super::null_error::encountered_error;
use super::{
AllocFamily, ConvertArguments, CopyFamily, InitFamily, MsgSend, MutableCopyFamily, NewFamily,
NoneFamily, TupleExtender,
AllocFamily, AllocSelector, ConvertArguments, CopyFamily, InitFamily, MsgSend,
MutableCopyFamily, NewFamily, NoneFamily, TupleExtender,
};

pub trait MsgSendRetained<T, U> {
Expand Down Expand Up @@ -241,19 +241,26 @@ impl<T: ?Sized + Message> MsgSendSuperRetained<&'_ AnyClass, Allocated<T>> for A
}
}

impl AllocFamily {
/// Fast path optimization for `msg_send_id![cls, alloc]`.
impl<T: Message> MsgSendRetained<&'_ AnyClass, Allocated<T>> for AllocSelector {
#[inline]
pub unsafe fn send_message_retained_alloc<T: Message, R: MaybeUnwrap<Input = Allocated<T>>>(
unsafe fn send_message_retained<A: ConvertArguments, R: MaybeUnwrap<Input = Allocated<T>>>(
cls: &AnyClass,
sel: Sel,
args: A,
) -> R {
// Available on non-fragile Apple runtimes.
#[cfg(all(
target_vendor = "apple",
not(all(target_os = "macos", target_arch = "x86"))
))]
{
// SAFETY: Checked by caller
// We completely ignore both the selector and the arguments, since
// we know them to be `alloc` and empty (since this is the
// `AllocSelector` "family").
let _ = sel;
let _ = args;

// SAFETY: Checked by caller.
let obj: *mut T = unsafe { crate::ffi::objc_alloc(cls).cast() };
// SAFETY: The object is newly allocated, so this has +1 retain count
let obj = unsafe { Allocated::new(obj) };
Expand All @@ -265,7 +272,7 @@ impl AllocFamily {
)))]
{
// SAFETY: Checked by caller
unsafe { AllocFamily::send_message_retained(cls, sel!(alloc), ()) }
unsafe { AllocFamily::send_message_retained(cls, sel, args) }
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/objc2/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,11 @@ macro_rules! msg_send_id {
});
[$obj:expr, alloc $(,)?] => ({
let result;
result = $crate::__macro_helpers::AllocFamily::send_message_retained_alloc($obj);
result = <$crate::__macro_helpers::AllocSelector as $crate::__macro_helpers::MsgSendRetained<_, _>>::send_message_retained(
$obj,
$crate::sel!(alloc),
(),
);
result
});
[$obj:expr, init $(,)?] => ({
Expand Down

0 comments on commit aa6e383

Please sign in to comment.