Skip to content

Commit

Permalink
Support for triomphe::Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Oct 24, 2023
1 parent 11bdf0a commit 2276eaf
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ codecov = { repository = "orium/archery", branch = "master", service = "github"

[dependencies]
static_assertions = "1.1.0"
triomphe = { version = "0.1.9", optional = true }

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand All @@ -49,6 +50,7 @@ compiletest_rs = "0.10.2"

[features]
fatal-warnings = []
triomphe = ["dep:triomphe"]

[[bench]]
name = "std_rc"
Expand Down
38 changes: 37 additions & 1 deletion benches/archery_shared_pointer_arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,41 @@ fn archery_shared_pointer_arc_clone(c: &mut Criterion) {
});
}

criterion_group!(benches, archery_shared_pointer_arc_deref, archery_shared_pointer_arc_clone);
fn archery_shared_pointer_arct_deref(c: &mut Criterion) {
let limit = 200_000;

c.bench_function("archery shared pointer arct deref", move |b| {
b.iter(|| {
let rc: SharedPointer<_, ArcTK> = SharedPointer::new(42);

for _ in 0..limit {
black_box(rc.deref());
}

rc
})
});
}

fn archery_shared_pointer_arct_clone(c: &mut Criterion) {
let limit = 100_000;

c.bench_function("archery shared pointer arct clone and drop", move |b| {
b.iter_with_setup(
|| Vec::with_capacity(limit),
|mut vec| {
vec.resize(limit, SharedPointer::<_, ArcTK>::new(42));
vec
},
)
});
}

criterion_group!(
benches,
archery_shared_pointer_arc_deref,
archery_shared_pointer_arc_clone,
archery_shared_pointer_arct_deref,
archery_shared_pointer_arct_clone
);
criterion_main!(benches);
45 changes: 45 additions & 0 deletions benches/archery_shared_pointer_arct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#![cfg_attr(feature = "fatal-warnings", deny(warnings))]

use archery::*;
use std::ops::Deref;

use criterion::{criterion_group, criterion_main, Criterion};
use std::hint::black_box;

fn archery_shared_pointer_arct_deref(c: &mut Criterion) {
let limit = 200_000;

c.bench_function("archery shared pointer arct deref", move |b| {
b.iter(|| {
let rc: SharedPointer<_, ArcTK> = SharedPointer::new(42);

for _ in 0..limit {
black_box(rc.deref());
}

rc
})
});
}

fn archery_shared_pointer_arct_clone(c: &mut Criterion) {
let limit = 100_000;

c.bench_function("archery shared pointer arct clone and drop", move |b| {
b.iter_with_setup(
|| Vec::with_capacity(limit),
|mut vec| {
vec.resize(limit, SharedPointer::<_, ArcTK>::new(42));
vec
},
)
});
}

criterion_group!(benches, archery_shared_pointer_arct_deref, archery_shared_pointer_arct_clone);
criterion_main!(benches);
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,8 @@ pub use shared_pointer::kind::SharedPointerKind;

#[doc(no_inline)]
pub use shared_pointer::kind::ArcK;
#[cfg(feature = "triomphe")]
#[doc(no_inline)]
pub use shared_pointer::kind::ArcTK;
#[doc(no_inline)]
pub use shared_pointer::kind::RcK;
124 changes: 124 additions & 0 deletions src/shared_pointer/kind/arct/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

use crate::shared_pointer::kind::SharedPointerKind;
use alloc::boxed::Box;
use core::fmt;
use core::fmt::Debug;
use core::fmt::Formatter;
use core::mem;
use core::mem::ManuallyDrop;
use core::ops::Deref;
use core::ops::DerefMut;
use core::ptr;
use triomphe::Arc;

type UntypedArc = Arc<()>;

/// [Type constructors](https://en.wikipedia.org/wiki/Type_constructor) for
/// [`Arc`] pointers.
pub struct ArcTK {
/// We use [`ManuallyDrop`] here, so that we can drop it explicitly as
/// [`Arc<T>`](triomphe::Arc). Not sure if it can be dropped as [`UntypedArc`], but it
/// seems to be playing with fire (even more than we already are).
inner: ManuallyDrop<UntypedArc>,
}

impl ArcTK {
#[inline(always)]
fn new_from_inner<T>(arc: Arc<T>) -> ArcTK {
ArcTK { inner: ManuallyDrop::new(unsafe { mem::transmute(arc) }) }
}

#[inline(always)]
unsafe fn take_inner<T>(self) -> Arc<T> {
let arc: UntypedArc = ManuallyDrop::into_inner(self.inner);

mem::transmute(arc)
}

#[inline(always)]
unsafe fn as_inner_ref<T>(&self) -> &Arc<T> {
let arc_t: *const Arc<T> = (self.inner.deref() as *const UntypedArc).cast::<Arc<T>>();

// Static check to make sure we are not messing up the sizes.
// This could happen if we allowed for `T` to be unsized, because it would need to be
// represented as a wide pointer inside `Arc`.
// TODO Use static_assertion when https://github.com/nvzqz/static-assertions-rs/issues/21
// gets fixed
let _ = mem::transmute::<UntypedArc, Arc<T>>;

&*arc_t
}

#[inline(always)]
unsafe fn as_inner_mut<T>(&mut self) -> &mut Arc<T> {
let arc_t: *mut Arc<T> = (self.inner.deref_mut() as *mut UntypedArc).cast::<Arc<T>>();

&mut *arc_t
}
}

unsafe impl SharedPointerKind for ArcTK {
#[inline(always)]
fn new<T>(v: T) -> ArcTK {
ArcTK::new_from_inner(Arc::new(v))
}

#[inline(always)]
fn from_box<T>(v: Box<T>) -> ArcTK {
ArcTK::new_from_inner::<T>(Arc::from(v))
}

#[inline(always)]
unsafe fn as_ptr<T>(&self) -> *const T {
Arc::as_ptr(self.as_inner_ref())
}

#[inline(always)]
unsafe fn deref<T>(&self) -> &T {
self.as_inner_ref::<T>().as_ref()
}

#[inline(always)]
unsafe fn try_unwrap<T>(self) -> Result<T, ArcTK> {
Arc::try_unwrap(self.take_inner()).map_err(ArcTK::new_from_inner)
}

#[inline(always)]
unsafe fn get_mut<T>(&mut self) -> Option<&mut T> {
Arc::get_mut(self.as_inner_mut())
}

#[inline(always)]
unsafe fn make_mut<T: Clone>(&mut self) -> &mut T {
Arc::make_mut(self.as_inner_mut())
}

#[inline(always)]
unsafe fn strong_count<T>(&self) -> usize {
Arc::count(self.as_inner_ref::<T>())
}

#[inline(always)]
unsafe fn clone<T>(&self) -> ArcTK {
ArcTK { inner: ManuallyDrop::new(Arc::clone(self.as_inner_ref())) }
}

#[inline(always)]
unsafe fn drop<T>(&mut self) {
ptr::drop_in_place::<Arc<T>>(self.as_inner_mut());
}
}

impl Debug for ArcTK {
#[inline(always)]
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
f.write_str("ArcTK")
}
}

#[cfg(test)]
mod test;
Loading

0 comments on commit 2276eaf

Please sign in to comment.