Skip to content

Commit

Permalink
Fix soundess for returned autoreleased refs. Refs #3
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed May 6, 2023
1 parent 2b01042 commit 824da71
Show file tree
Hide file tree
Showing 37 changed files with 180 additions and 134 deletions.
20 changes: 10 additions & 10 deletions cidre/benches/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
use cidre::{ns, objc::autoreleasepool};
use cidre::{ns, objc::ar_pool};
use criterion::{criterion_group, criterion_main, Criterion};

pub fn criterion_benchmark(c: &mut Criterion) {
let num = i64::MAX - 1;

c.bench_function("array_new_with_alloc_init", |b| {
b.iter(|| {
autoreleasepool(|| {
ar_pool(|| {
ns::Array::<ns::Id>::new();
})
})
});
c.bench_function("array_new_with_new", |b| {
b.iter(|| {
autoreleasepool(|| {
ar_pool(|| {
ns::Array::<ns::Id>::_new();
})
})
});

c.bench_function("alloc_init", |b| {
b.iter(|| {
autoreleasepool(|| {
ar_pool(|| {
ns::Number::with_i64(num);
})
})
});
c.bench_function("alloc_init_tagged", |b| {
b.iter(|| {
autoreleasepool(|| {
ar_pool(|| {
ns::Number::with_i64(1);
})
})
});
c.bench_function("alloc_tagged", |b| {
b.iter(|| {
autoreleasepool(|| {
ar_pool(|| {
ns::Number::tagged_i32(10);
});
})
Expand All @@ -49,28 +49,28 @@ pub fn criterion_benchmark(c: &mut Criterion) {
// });
c.bench_function("alloc_with_ar_retain", |b| {
b.iter(|| {
autoreleasepool(|| {
ar_pool(|| {
ns::Number::with_i64_ar_retain(num);
})
})
});
c.bench_function("alloc_tagged_ar_retain", |b| {
b.iter(|| {
autoreleasepool(|| {
ar_pool(|| {
ns::Number::with_i64_ar_retain(1);
})
})
});
c.bench_function("alloc_with_ar", |b| {
b.iter(|| {
autoreleasepool(|| {
ar_pool(|| {
ns::Number::with_i64_ar(num);
})
})
});
c.bench_function("alloc_with_fn_call", |b| {
b.iter(|| {
autoreleasepool(|| {
ar_pool(|| {
ns::Number::with_i64_call(num);
})
})
Expand Down
4 changes: 2 additions & 2 deletions cidre/examples/fence/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cidre::{mtl, objc::autoreleasepool};
use cidre::{mtl, objc::ar_pool};

fn main() {
autoreleasepool(|| {
ar_pool(|| {
let device = mtl::Device::default().unwrap();

let cmd_queue = device.new_cmd_queue().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions cidre/examples/read/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cidre::{arc, av, cv, ns, objc::autoreleasepool, vn};
use cidre::{arc, av, cv, ns, objc::ar_pool, vn};
use ndarray::{Array2, Axis};
// Import the linfa prelude and KMeans algorithm
use linfa::prelude::*;
Expand Down Expand Up @@ -64,7 +64,7 @@ async fn main() {
continue;
}
let pts = buf.pts();
autoreleasepool(|| {
ar_pool(|| {
handler
.perform_on_cv_pixel_buffer(&requests, &image)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions cidre/examples/stable-diffusion/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cidre::{arc, mps, mps::graph, ns, objc::autoreleasepool};
use cidre::{arc, mps, mps::graph, ns, objc::ar_pool};

fn make_graph(synchonize: bool) -> arc::R<graph::Graph> {
let mut graph = graph::Graph::new();
Expand Down Expand Up @@ -1262,7 +1262,7 @@ fn make_diffusion_step(

fn main() {
// unsafe {
autoreleasepool(|| {
ar_pool(|| {
let i64 = ns::Number::with_i64(1).string();
})
// let desc = cidre::objc::Id::retain_autoreleased(Some(num.debug_description()));
Expand Down
4 changes: 2 additions & 2 deletions cidre/examples/triangle/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cidre::{cg, ci, mtl, ns, objc::autoreleasepool, simd};
use cidre::{cg, ci, mtl, ns, objc::ar_pool, simd};

#[repr(C)]
struct Vertex {
Expand Down Expand Up @@ -59,7 +59,7 @@ fragment float4 pass_color(
"###;

fn main() {
autoreleasepool(|| {
ar_pool(|| {
let device = mtl::Device::default().unwrap();

let source = ns::String::with_str(LIB_SRC);
Expand Down
25 changes: 23 additions & 2 deletions cidre/src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::objc;
use std::{
arch::asm,
ops::{Deref, DerefMut},
ptr::NonNull,
};

pub trait Release {
Expand Down Expand Up @@ -140,11 +141,31 @@ impl<T: Retain> Clone for Retained<T> {
}
}

#[repr(transparent)]
pub struct ReturnedAutoReleased<T: objc::Obj>(NonNull<T>);

impl<T: objc::Obj> Deref for ReturnedAutoReleased<T> {
type Target = T;

#[inline]
fn deref(&self) -> &Self::Target {
unsafe { self.0.as_ref() }
}
}

impl<T: objc::Obj> DerefMut for ReturnedAutoReleased<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.0.as_mut() }
}
}

pub type A<T> = Allocated<T>;
pub type R<T> = Retained<T>;
pub type Rar<T> = ReturnedAutoReleased<T>;

#[inline]
pub fn rar_retain_option<T: objc::Obj>(id: Option<&T>) -> Option<R<T>> {
pub fn rar_retain_option<T: objc::Obj>(id: Option<Rar<T>>) -> Option<R<T>> {
unsafe {
// see comments in rar_retain
asm!("mov x29, x29");
Expand All @@ -155,7 +176,7 @@ pub fn rar_retain_option<T: objc::Obj>(id: Option<&T>) -> Option<R<T>> {
}

#[inline]
pub fn rar_retain<T: objc::Obj>(id: &T) -> R<T> {
pub fn rar_retain<T: objc::Obj>(id: Rar<T>) -> R<T> {
unsafe {
// latest runtimes don't need this marker anymore.
// see https://developer.apple.com/videos/play/wwdc2022/110363/ at 13:24
Expand Down
2 changes: 1 addition & 1 deletion cidre/src/av/audio/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Time {
/// anchor has a valid host time representation and sample time representation.AVAudioTime *anchor = [player playerTimeForNodeTime: player.lastRenderTime];
/// fill in valid host time representationAVAudioTime *fullTime0 = [time0 extrapolateTimeFromAnchor: anchor];
#[objc::msg_send(extrapolateTimeFromAnchor:)]
pub fn extrapolate_time_from_anchor_ar(&self, anchor: &Time) -> Option<&'ar Time>;
pub fn extrapolate_time_from_anchor_ar(&self, anchor: &Time) -> Option<arc::Rar<Time>>;

#[objc::rar_retain]
pub fn extrapolate_time_from_anchor(&self, anchor: &Time) -> Option<arc::R<Time>>;
Expand Down
7 changes: 4 additions & 3 deletions cidre/src/av/capture/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Device {
device_type: &Type,
media_type: Option<&MediaType>,
position: Position,
) -> Option<&'ar Self>;
) -> Option<arc::Rar<Self>>;

#[objc::cls_rar_retain]
pub fn with_device_type_media_and_position(
Expand All @@ -117,7 +117,7 @@ impl Device {
) -> Option<arc::R<Self>>;

#[objc::cls_msg_send(deviceWithUniqueID:)]
pub fn with_unique_id_ar(unique_id: ns::String) -> Option<&'ar Self>;
pub fn with_unique_id_ar(unique_id: ns::String) -> Option<arc::Rar<Self>>;

#[objc::cls_rar_retain]
pub fn with_unique_id(unique_id: ns::String) -> Option<arc::R<Self>>;
Expand Down Expand Up @@ -351,7 +351,8 @@ impl DiscoverySession {
device_types: &cf::ArrayOf<Type>,
media_type: Option<&MediaType>,
position: Position,
) -> &'ar Self;
) -> arc::Rar<Self>;

#[objc::cls_rar_retain]
pub fn with_device_types_media_and_position(
device_types: &cf::ArrayOf<Type>,
Expand Down
2 changes: 1 addition & 1 deletion cidre/src/av/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{arc, cm, ns, objc};

impl ns::Value {
#[objc::cls_msg_send(valueWithCMTimeRange:)]
pub fn with_cm_time_range_ar(range: &cm::TimeRange) -> &'ar ns::Value;
pub fn with_cm_time_range_ar(range: &cm::TimeRange) -> arc::Rar<ns::Value>;

#[objc::cls_rar_retain]
pub fn with_cm_time_range(range: &cm::TimeRange) -> arc::R<ns::Value>;
Expand Down
2 changes: 1 addition & 1 deletion cidre/src/ca/media_timing_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl MediaTimingFn {
define_cls!(CA_MEDIA_TIMING_FUNCTION);

#[objc::cls_msg_send(functionWithName:)]
pub fn with_name_ar(name: &Name) -> &'ar Self;
pub fn with_name_ar(name: &Name) -> arc::Rar<Self>;

#[objc::cls_rar_retain]
pub fn with_name(name: &Name) -> arc::R<Self>;
Expand Down
2 changes: 1 addition & 1 deletion cidre/src/ca/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Renderer {
pub fn with_mtl_texture_ar(
texture: &mtl::Texture,
options: Option<&ns::Dictionary<OptionKey, ns::Id>>,
) -> &'ar Self;
) -> arc::Rar<Self>;

#[objc::cls_rar_retain]
pub fn with_mtl_texture(
Expand Down
10 changes: 7 additions & 3 deletions cidre/src/mps/graph/activation_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use crate::{arc, mps::graph, ns, objc};

impl graph::Graph {
#[objc::msg_send(reLUWithTensor:name:)]
pub fn relu_ar(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> &'ar graph::Tensor;
pub fn relu_ar(
&self,
tensor: &graph::Tensor,
name: Option<&ns::String>,
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn relu(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> arc::R<graph::Tensor>;
Expand All @@ -12,7 +16,7 @@ impl graph::Graph {
&self,
tensor: &graph::Tensor,
name: Option<&ns::String>,
) -> &'ar graph::Tensor;
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn sigmoid(
Expand All @@ -27,7 +31,7 @@ impl graph::Graph {
tensor: &graph::Tensor,
axis: ns::Integer,
name: Option<&ns::String>,
) -> &'ar graph::Tensor;
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn soft_max(
Expand Down
43 changes: 31 additions & 12 deletions cidre/src/mps/graph/arithmetic_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl graph::Graph {
primary: &graph::Tensor,
secondary: &graph::Tensor,
name: Option<&ns::String>,
) -> &'ar graph::Tensor;
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn add(
Expand All @@ -24,7 +24,7 @@ impl graph::Graph {
min: &graph::Tensor,
max: &graph::Tensor,
name: Option<&ns::String>,
) -> &'ar graph::Tensor;
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn clamp(
Expand All @@ -44,7 +44,7 @@ impl graph::Graph {
primary: &graph::Tensor,
secondary: &graph::Tensor,
name: Option<&ns::String>,
) -> &'ar graph::Tensor;
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn mul(
Expand All @@ -55,8 +55,11 @@ impl graph::Graph {
) -> arc::R<graph::Tensor>;

#[objc::msg_send(roundWithTensor:name:)]
pub fn round_ar(&self, tensor: &graph::Tensor, name: Option<&ns::String>)
-> &'ar graph::Tensor;
pub fn round_ar(
&self,
tensor: &graph::Tensor,
name: Option<&ns::String>,
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn round(&self, tensor: &graph::Tensor, name: Option<&ns::String>)
Expand All @@ -67,7 +70,7 @@ impl graph::Graph {
&self,
tensor: &graph::Tensor,
name: Option<&ns::String>,
) -> &'ar graph::Tensor;
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn square_root(
Expand All @@ -82,7 +85,7 @@ impl graph::Graph {
primary: &graph::Tensor,
secondary: &graph::Tensor,
name: Option<&ns::String>,
) -> &'ar graph::Tensor;
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn sub(
Expand All @@ -98,7 +101,7 @@ impl graph::Graph {
primary: &graph::Tensor,
secondary: &graph::Tensor,
name: Option<&ns::String>,
) -> &'ar graph::Tensor;
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn div(
Expand All @@ -109,25 +112,41 @@ impl graph::Graph {
) -> arc::R<graph::Tensor>;

#[objc::msg_send(tanhWithTensor:name:)]
pub fn tanh_ar(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> &'ar graph::Tensor;
pub fn tanh_ar(
&self,
tensor: &graph::Tensor,
name: Option<&ns::String>,
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn tanh(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> arc::R<graph::Tensor>;

#[objc::msg_send(erfWithTensor:name:)]
pub fn erf_ar(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> &'ar graph::Tensor;
pub fn erf_ar(
&self,
tensor: &graph::Tensor,
name: Option<&ns::String>,
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn erf(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> arc::R<graph::Tensor>;

#[objc::msg_send(cosWithTensor:name:)]
pub fn cos_ar(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> &'ar graph::Tensor;
pub fn cos_ar(
&self,
tensor: &graph::Tensor,
name: Option<&ns::String>,
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn cos(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> arc::R<graph::Tensor>;

#[objc::msg_send(sinWithTensor:name:)]
pub fn sin_ar(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> &'ar graph::Tensor;
pub fn sin_ar(
&self,
tensor: &graph::Tensor,
name: Option<&ns::String>,
) -> arc::Rar<graph::Tensor>;

#[objc::rar_retain]
pub fn sin(&self, tensor: &graph::Tensor, name: Option<&ns::String>) -> arc::R<graph::Tensor>;
Expand Down
Loading

0 comments on commit 824da71

Please sign in to comment.