Skip to content

Commit

Permalink
Add cached dealloc selector and optimize NSObject class fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 23, 2023
1 parent efeaa0d commit 0405a38
Show file tree
Hide file tree
Showing 18 changed files with 689 additions and 715 deletions.
30 changes: 30 additions & 0 deletions crates/objc2/src/__macro_helpers/common_selectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Common selectors.
//!
//! These are put here to deduplicate the cached selector, and when using
//! `unstable-static-sel`, the statics.
//!
//! Note that our assembly tests of `unstable-static-sel-inlined` output a GOT
//! entry for such accesses, but that is just a limitation of our tests - the
//! actual assembly is as one would expect.
use crate::runtime::Sel;
use crate::{__sel_data, __sel_inner};

#[inline]
pub fn alloc_sel() -> Sel {
__sel_inner!(__sel_data!(alloc), "alloc")
}

#[inline]
pub fn init_sel() -> Sel {
__sel_inner!(__sel_data!(init), "init")
}

#[inline]
pub fn new_sel() -> Sel {
__sel_inner!(__sel_data!(new), "new")
}

#[inline]
pub fn dealloc_sel() -> Sel {
__sel_inner!(__sel_data!(dealloc), "dealloc")
}
25 changes: 2 additions & 23 deletions crates/objc2/src/__macro_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::rc::{Allocated, Id};
use crate::runtime::MethodDescription;
use crate::runtime::{AnyClass, AnyObject, AnyProtocol, Sel};
use crate::{Message, MessageArguments, MessageReceiver};
use crate::{__sel_data, __sel_inner};

pub use core::borrow::{Borrow, BorrowMut};
pub use core::cell::UnsafeCell;
Expand All @@ -29,36 +28,16 @@ pub use core::{compile_error, concat, panic, stringify};
pub use std::sync::Once;

mod cache;
mod common_selectors;
mod declare_class;

pub use self::cache::{CachedClass, CachedSel};
pub use self::common_selectors::{alloc_sel, dealloc_sel, init_sel, new_sel};
pub use self::declare_class::{
assert_mutability_matches_superclass_mutability, MaybeOptionId, MessageRecieveId,
ValidSubclassMutability,
};

// Common selectors.
//
// These are put here to deduplicate the cached selector, and when using
// `unstable-static-sel`, the statics.
//
// Note that our assembly tests of `unstable-static-sel-inlined` output a GOT
// entry for such accesses, but that is just a limitation of our tests - the
// actual assembly is as one would expect.

#[inline]
pub fn alloc_sel() -> Sel {
__sel_inner!(__sel_data!(alloc), "alloc")
}
#[inline]
pub fn init_sel() -> Sel {
__sel_inner!(__sel_data!(init), "init")
}
#[inline]
pub fn new_sel() -> Sel {
__sel_inner!(__sel_data!(new), "new")
}

/// Helper for specifying the retain semantics for a given selector family.
///
/// Note that we can't actually check if a method is in a method family; only
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/macros/extern_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ macro_rules! __inner_extern_class {

$crate::__class_inner!(
$crate::__select_name!($name; $($name_const)?),
$crate::__hash_idents!($name),
$crate::__hash_idents!($name)
)
}

Expand Down
19 changes: 11 additions & 8 deletions crates/objc2/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ macro_rules! class {
($name:ident) => {{
$crate::__class_inner!(
$crate::__macro_helpers::stringify!($name),
$crate::__hash_idents!($name),
$crate::__hash_idents!($name)
)
}};
}
Expand All @@ -82,7 +82,7 @@ macro_rules! class {
#[macro_export]
#[cfg(not(feature = "unstable-static-class"))]
macro_rules! __class_inner {
($name:expr, $_hash:expr,) => {{
($name:expr, $_hash:expr) => {{
static CACHED_CLASS: $crate::__macro_helpers::CachedClass =
$crate::__macro_helpers::CachedClass::new();
#[allow(unused_unsafe)]
Expand Down Expand Up @@ -220,14 +220,17 @@ macro_rules! __class_inner {
/// ```
#[macro_export]
macro_rules! sel {
(alloc) => ({
$crate::__macro_helpers::alloc_sel()
(new) => ({
$crate::__macro_helpers::new_sel()
});
(init) => ({
$crate::__macro_helpers::init_sel()
});
(new) => ({
$crate::__macro_helpers::new_sel()
(alloc) => ({
$crate::__macro_helpers::alloc_sel()
});
(dealloc) => ({
$crate::__macro_helpers::dealloc_sel()
});
($sel:ident) => ({
$crate::__sel_inner!(
Expand Down Expand Up @@ -660,7 +663,7 @@ macro_rules! __sel_inner {
not(feature = "unstable-static-class-inlined")
))]
macro_rules! __class_inner {
($name:expr, $hash:expr,) => {{
($name:expr, $hash:expr) => {{
$crate::__inner_statics!(@image_info $hash);
$crate::__inner_statics!(@class $name, $hash);

Expand All @@ -678,7 +681,7 @@ macro_rules! __class_inner {
#[macro_export]
#[cfg(feature = "unstable-static-class-inlined")]
macro_rules! __class_inner {
($name:expr, $hash:expr,) => {{
($name:expr, $hash:expr) => {{
$crate::__inner_statics!(@image_info $hash);
$crate::__inner_statics!(@class $name, $hash);

Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/runtime/nsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ unsafe impl ClassType for NSObject {
fn class() -> &'static AnyClass {
#[cfg(feature = "apple")]
{
crate::class!(NSObject)
crate::__class_inner!("NSObject", "NSObject")
}
#[cfg(feature = "gnustep-1-7")]
{
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/runtime/nsproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ unsafe impl ClassType for NSProxy {
fn class() -> &'static AnyClass {
#[cfg(feature = "apple")]
{
crate::class!(NSProxy)
crate::__class_inner!("NSProxy", "NSProxy")
}
#[cfg(feature = "gnustep-1-7")]
{
Expand Down
2 changes: 1 addition & 1 deletion crates/test-assembly/crates/test_declare_class/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ gnustep-2-0 = ["gnustep-1-9", "objc2?/gnustep-2-0"]
gnustep-2-1 = ["gnustep-2-0", "objc2?/gnustep-2-1"]

# Hack to prevent the feature flag from being enabled in the entire project
assembly-features = ["objc2?/unstable-static-sel-inlined", "objc2?/unstable-static-class"]
assembly-features = ["objc2?/unstable-static-sel-inlined", "objc2?/unstable-static-class-inlined"]
Loading

0 comments on commit 0405a38

Please sign in to comment.