Skip to content

Commit

Permalink
Better inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Feb 22, 2022
1 parent 2623f9d commit a134b39
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
8 changes: 4 additions & 4 deletions objc2/src/rc/autorelease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl AutoreleasePool {
/// Additionally, the pools must be dropped in the same order they were
/// created.
#[doc(alias = "objc_autoreleasePoolPush")]
#[inline]
unsafe fn new() -> Self {
// TODO: Make this function pub when we're more certain of the API
let context = unsafe { ffi::objc_autoreleasePoolPush() };
Expand All @@ -57,10 +58,7 @@ impl AutoreleasePool {
}

/// This will be removed in a future version.
#[cfg_attr(
not(all(debug_assertions, not(feature = "unstable_autoreleasesafe"))),
inline
)]
#[inline]
#[doc(hidden)]
pub fn __verify_is_inner(&self) {
#[cfg(all(debug_assertions, not(feature = "unstable_autoreleasesafe")))]
Expand Down Expand Up @@ -139,6 +137,7 @@ impl Drop for AutoreleasePool {
/// [clang documentation]: https://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool
/// [revision `371`]: https://github.com/apple-oss-distributions/objc4/blob/objc4-371/runtime/objc-exception.m#L479-L482
#[doc(alias = "objc_autoreleasePoolPop")]
#[inline]
fn drop(&mut self) {
unsafe { ffi::objc_autoreleasePoolPop(self.context) }
#[cfg(all(debug_assertions, not(feature = "unstable_autoreleasesafe")))]
Expand Down Expand Up @@ -292,6 +291,7 @@ impl !AutoreleaseSafe for AutoreleasePool {}
/// # panic!("Does not panic in release mode, so for testing we make it!");
/// ```
#[doc(alias = "@autoreleasepool")]
#[inline(always)]
pub fn autoreleasepool<T, F>(f: F) -> T
where
for<'p> F: FnOnce(&'p AutoreleasePool) -> T + AutoreleaseSafe,
Expand Down
4 changes: 2 additions & 2 deletions objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<T: Message, O: Ownership> Id<T, O> {
// let y: &T = &*retained;
// ```
#[doc(alias = "objc_retain")]
#[cfg_attr(not(debug_assertions), inline)]
#[inline]
pub unsafe fn retain(ptr: NonNull<T>) -> Id<T, O> {
let ptr = ptr.as_ptr() as *mut ffi::objc_object;
// SAFETY: The caller upholds that the pointer is valid
Expand Down Expand Up @@ -266,7 +266,7 @@ impl<T: Message, O: Ownership> Id<T, O> {
NonNull::new(ptr).map(|ptr| unsafe { Id::retain(ptr) })
}

#[cfg_attr(not(debug_assertions), inline)]
#[inline]
fn autorelease_inner(self) -> *mut T {
// Note that this (and the actual `autorelease`) is not an associated
// function. This breaks the guideline that smart pointers shouldn't
Expand Down
1 change: 1 addition & 0 deletions objc2/src/rc/id_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub trait DefaultId {
}

impl<T: DefaultId + ?Sized> Default for Id<T, T::Ownership> {
#[inline]
fn default() -> Self {
T::default_id()
}
Expand Down
7 changes: 5 additions & 2 deletions objc2/src/rc/weak_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use core::convert::TryFrom;
use core::fmt;
use core::marker::PhantomData;
use core::ptr;
use core::ptr::NonNull;
use std::panic::{RefUnwindSafe, UnwindSafe};

use super::{Id, Shared};
Expand Down Expand Up @@ -35,6 +34,7 @@ pub struct WeakId<T: ?Sized> {
impl<T: Message> WeakId<T> {
/// Construct a new [`WeakId`] referencing the given shared [`Id`].
#[doc(alias = "objc_initWeak")]
#[inline]
pub fn new(obj: &Id<T, Shared>) -> Self {
// Note that taking `&Id<T, Owned>` would not be safe since that would
// allow loading an `Id<T, Shared>` later on.
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<T: Message> WeakId<T> {
pub fn load(&self) -> Option<Id<T, Shared>> {
let ptr: *mut *mut ffi::objc_object = self.inner.get() as _;
let obj = unsafe { ffi::objc_loadWeakRetained(ptr) } as *mut T;
NonNull::new(obj).map(|obj| unsafe { Id::new(obj) })
unsafe { Id::new_null(obj) }
}

// TODO: Add `autorelease(&self) -> Option<&T>` using `objc_loadWeak`?
Expand All @@ -77,6 +77,7 @@ impl<T: Message> WeakId<T> {
impl<T: ?Sized> Drop for WeakId<T> {
/// Drops the `WeakId` pointer.
#[doc(alias = "objc_destroyWeak")]
#[inline]
fn drop(&mut self) {
unsafe { ffi::objc_destroyWeak(self.inner.get() as _) }
}
Expand All @@ -101,6 +102,7 @@ impl<T: Message> Default for WeakId<T> {
/// Constructs a new `WeakId<T>` that doesn't reference any object.
///
/// Calling [`Self::load`] on the return value always gives [`None`].
#[inline]
fn default() -> Self {
// SAFETY: The pointer is null
unsafe { Self::new_inner(ptr::null_mut()) }
Expand Down Expand Up @@ -130,6 +132,7 @@ impl<T: RefUnwindSafe + ?Sized> RefUnwindSafe for WeakId<T> {}
impl<T: RefUnwindSafe + ?Sized> UnwindSafe for WeakId<T> {}

impl<T: Message> From<Id<T, Shared>> for WeakId<T> {
#[inline]
fn from(obj: Id<T, Shared>) -> Self {
WeakId::new(&obj)
}
Expand Down

0 comments on commit a134b39

Please sign in to comment.