Skip to content

Commit 0405a38

Browse files
committed
Add cached dealloc selector and optimize NSObject class fetching
1 parent efeaa0d commit 0405a38

File tree

18 files changed

+689
-715
lines changed

18 files changed

+689
-715
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//! Common selectors.
2+
//!
3+
//! These are put here to deduplicate the cached selector, and when using
4+
//! `unstable-static-sel`, the statics.
5+
//!
6+
//! Note that our assembly tests of `unstable-static-sel-inlined` output a GOT
7+
//! entry for such accesses, but that is just a limitation of our tests - the
8+
//! actual assembly is as one would expect.
9+
use crate::runtime::Sel;
10+
use crate::{__sel_data, __sel_inner};
11+
12+
#[inline]
13+
pub fn alloc_sel() -> Sel {
14+
__sel_inner!(__sel_data!(alloc), "alloc")
15+
}
16+
17+
#[inline]
18+
pub fn init_sel() -> Sel {
19+
__sel_inner!(__sel_data!(init), "init")
20+
}
21+
22+
#[inline]
23+
pub fn new_sel() -> Sel {
24+
__sel_inner!(__sel_data!(new), "new")
25+
}
26+
27+
#[inline]
28+
pub fn dealloc_sel() -> Sel {
29+
__sel_inner!(__sel_data!(dealloc), "dealloc")
30+
}

crates/objc2/src/__macro_helpers/mod.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::rc::{Allocated, Id};
1313
use crate::runtime::MethodDescription;
1414
use crate::runtime::{AnyClass, AnyObject, AnyProtocol, Sel};
1515
use crate::{Message, MessageArguments, MessageReceiver};
16-
use crate::{__sel_data, __sel_inner};
1716

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

3130
mod cache;
31+
mod common_selectors;
3232
mod declare_class;
3333

3434
pub use self::cache::{CachedClass, CachedSel};
35+
pub use self::common_selectors::{alloc_sel, dealloc_sel, init_sel, new_sel};
3536
pub use self::declare_class::{
3637
assert_mutability_matches_superclass_mutability, MaybeOptionId, MessageRecieveId,
3738
ValidSubclassMutability,
3839
};
3940

40-
// Common selectors.
41-
//
42-
// These are put here to deduplicate the cached selector, and when using
43-
// `unstable-static-sel`, the statics.
44-
//
45-
// Note that our assembly tests of `unstable-static-sel-inlined` output a GOT
46-
// entry for such accesses, but that is just a limitation of our tests - the
47-
// actual assembly is as one would expect.
48-
49-
#[inline]
50-
pub fn alloc_sel() -> Sel {
51-
__sel_inner!(__sel_data!(alloc), "alloc")
52-
}
53-
#[inline]
54-
pub fn init_sel() -> Sel {
55-
__sel_inner!(__sel_data!(init), "init")
56-
}
57-
#[inline]
58-
pub fn new_sel() -> Sel {
59-
__sel_inner!(__sel_data!(new), "new")
60-
}
61-
6241
/// Helper for specifying the retain semantics for a given selector family.
6342
///
6443
/// Note that we can't actually check if a method is in a method family; only

crates/objc2/src/macros/extern_class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ macro_rules! __inner_extern_class {
369369

370370
$crate::__class_inner!(
371371
$crate::__select_name!($name; $($name_const)?),
372-
$crate::__hash_idents!($name),
372+
$crate::__hash_idents!($name)
373373
)
374374
}
375375

crates/objc2/src/macros/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ macro_rules! class {
7373
($name:ident) => {{
7474
$crate::__class_inner!(
7575
$crate::__macro_helpers::stringify!($name),
76-
$crate::__hash_idents!($name),
76+
$crate::__hash_idents!($name)
7777
)
7878
}};
7979
}
@@ -82,7 +82,7 @@ macro_rules! class {
8282
#[macro_export]
8383
#[cfg(not(feature = "unstable-static-class"))]
8484
macro_rules! __class_inner {
85-
($name:expr, $_hash:expr,) => {{
85+
($name:expr, $_hash:expr) => {{
8686
static CACHED_CLASS: $crate::__macro_helpers::CachedClass =
8787
$crate::__macro_helpers::CachedClass::new();
8888
#[allow(unused_unsafe)]
@@ -220,14 +220,17 @@ macro_rules! __class_inner {
220220
/// ```
221221
#[macro_export]
222222
macro_rules! sel {
223-
(alloc) => ({
224-
$crate::__macro_helpers::alloc_sel()
223+
(new) => ({
224+
$crate::__macro_helpers::new_sel()
225225
});
226226
(init) => ({
227227
$crate::__macro_helpers::init_sel()
228228
});
229-
(new) => ({
230-
$crate::__macro_helpers::new_sel()
229+
(alloc) => ({
230+
$crate::__macro_helpers::alloc_sel()
231+
});
232+
(dealloc) => ({
233+
$crate::__macro_helpers::dealloc_sel()
231234
});
232235
($sel:ident) => ({
233236
$crate::__sel_inner!(
@@ -660,7 +663,7 @@ macro_rules! __sel_inner {
660663
not(feature = "unstable-static-class-inlined")
661664
))]
662665
macro_rules! __class_inner {
663-
($name:expr, $hash:expr,) => {{
666+
($name:expr, $hash:expr) => {{
664667
$crate::__inner_statics!(@image_info $hash);
665668
$crate::__inner_statics!(@class $name, $hash);
666669

@@ -678,7 +681,7 @@ macro_rules! __class_inner {
678681
#[macro_export]
679682
#[cfg(feature = "unstable-static-class-inlined")]
680683
macro_rules! __class_inner {
681-
($name:expr, $hash:expr,) => {{
684+
($name:expr, $hash:expr) => {{
682685
$crate::__inner_statics!(@image_info $hash);
683686
$crate::__inner_statics!(@class $name, $hash);
684687

crates/objc2/src/runtime/nsobject.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ unsafe impl ClassType for NSObject {
5252
fn class() -> &'static AnyClass {
5353
#[cfg(feature = "apple")]
5454
{
55-
crate::class!(NSObject)
55+
crate::__class_inner!("NSObject", "NSObject")
5656
}
5757
#[cfg(feature = "gnustep-1-7")]
5858
{

crates/objc2/src/runtime/nsproxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ unsafe impl ClassType for NSProxy {
4444
fn class() -> &'static AnyClass {
4545
#[cfg(feature = "apple")]
4646
{
47-
crate::class!(NSProxy)
47+
crate::__class_inner!("NSProxy", "NSProxy")
4848
}
4949
#[cfg(feature = "gnustep-1-7")]
5050
{

crates/test-assembly/crates/test_declare_class/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ gnustep-2-0 = ["gnustep-1-9", "objc2?/gnustep-2-0"]
2222
gnustep-2-1 = ["gnustep-2-0", "objc2?/gnustep-2-1"]
2323

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

0 commit comments

Comments
 (0)