Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fn basics(c: &mut Criterion) {
});

group.bench_function("experimental_owned", |b| {
#[allow(clippy::arc_with_non_send_sync)]
let pool = Arc::new(ExperimentalPool::from_iter(&[()]));
b.iter(|| pool.pull_owned())
});
Expand Down
26 changes: 15 additions & 11 deletions src/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ impl<A> FromIterator<A> for Pool<A> {
}

impl<T> Pool<T> {
#[allow(clippy::must_use_candidate)]
pub fn pull(&self) -> Option<ObjectRef<T>> {
unsafe {
self.freelist.first_free().map(|index| ObjectRef {
pool: &self,
pool: self,
value: (*self.objects[index].get()).assume_init_mut(),
index,
})
}
}

#[cfg(not(loom))]
#[allow(clippy::must_use_candidate)]
pub fn pull_owned(self: &Arc<Self>) -> Option<Object<T>> {
unsafe {
self.freelist.first_free().map(|index| Object {
Expand All @@ -58,14 +60,16 @@ impl<T> Pool<T> {
}
}

#[allow(clippy::must_use_candidate)]
pub fn len(&self) -> usize {
let mut len = 0;
for int in self.freelist.ints.iter() {
len += int.load(Relaxed).count_ones() as usize
for int in &self.freelist.ints {
len += int.load(Relaxed).count_ones() as usize;
}
len
}

#[allow(clippy::must_use_candidate)]
pub fn capacity(&self) -> usize {
self.objects.len()
}
Expand All @@ -75,7 +79,7 @@ impl<T> Drop for Pool<T> {
fn drop(&mut self) {
unsafe {
self.freelist
.iter_taken(|index| (*self.objects[index].get()).assume_init_drop())
.iter_taken(|index| (*self.objects[index].get()).assume_init_drop());
}
}
}
Expand All @@ -86,21 +90,21 @@ pub struct ObjectRef<'a, T> {
index: usize,
}

impl<'a, T> Deref for ObjectRef<'a, T> {
impl<T> Deref for ObjectRef<'_, T> {
type Target = T;

fn deref(&self) -> &Self::Target {
self.value as _
}
}

impl<'a, T> DerefMut for ObjectRef<'a, T> {
impl<T> DerefMut for ObjectRef<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.value
}
}

impl<'a, T> Drop for ObjectRef<'a, T> {
impl<T> Drop for ObjectRef<'_, T> {
fn drop(&mut self) {
self.pool.freelist.free(self.index);
}
Expand Down Expand Up @@ -133,7 +137,7 @@ impl<T> Drop for Object<T> {
.get()
.write(MaybeUninit::new(ManuallyDrop::take(&mut self.value)));
}
self.pool.freelist.free(self.index)
self.pool.freelist.free(self.index);
}
}

Expand Down Expand Up @@ -180,7 +184,7 @@ impl FreeList {
let int = index / U64_BITS;
let bit = index % U64_BITS;
let bits = self.ints[int].fetch_or(1 << bit, Release);
debug_assert_eq!(bits & 1 << bit, 0)
debug_assert_eq!(bits & 1 << bit, 0);
}

fn iter_taken<F: Fn(usize)>(&self, f: F) {
Expand Down Expand Up @@ -247,7 +251,7 @@ mod tests {

impl Drop for DropTest {
fn drop(&mut self) {
self.drops.borrow_mut()[self.index] = true
self.drops.borrow_mut()[self.index] = true;
}
}

Expand Down Expand Up @@ -292,7 +296,7 @@ mod tests {
if let Some(ref mut o) = o {
**o += 1;
}
objects.push(o)
objects.push(o);
}

assert!(p
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'a, T> Reusable<'a, T> {
}
}

impl<'a, T> Deref for Reusable<'a, T> {
impl<T> Deref for Reusable<'_, T> {
type Target = T;

#[inline]
Expand All @@ -207,14 +207,14 @@ impl<'a, T> Deref for Reusable<'a, T> {
}
}

impl<'a, T> DerefMut for Reusable<'a, T> {
impl<T> DerefMut for Reusable<'_, T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}

impl<'a, T> Drop for Reusable<'a, T> {
impl<T> Drop for Reusable<'_, T> {
#[inline]
fn drop(&mut self) {
unsafe { self.pool.attach(self.take()) }
Expand Down Expand Up @@ -281,16 +281,16 @@ mod tests {

#[test]
fn detach() {
let pool = Pool::new(1, || Vec::new());
let pool = Pool::new(1, Vec::new);
let (pool, mut object) = pool.try_pull().unwrap().detach();
object.push(1);
Reusable::new(&pool, object);
Reusable::new(pool, object);
assert_eq!(pool.try_pull().unwrap()[0], 1);
}

#[test]
fn detach_then_attach() {
let pool = Pool::new(1, || Vec::new());
let pool = Pool::new(1, Vec::new);
let (pool, mut object) = pool.try_pull().unwrap().detach();
object.push(1);
pool.attach(object);
Expand All @@ -299,11 +299,11 @@ mod tests {

#[test]
fn pull() {
let pool = Pool::<Vec<u8>>::new(1, || Vec::new());
let pool = Pool::<Vec<u8>>::new(1, Vec::new);

let object1 = pool.try_pull();
let object2 = pool.try_pull();
let object3 = pool.pull(|| Vec::new());
let object3 = pool.pull(Vec::new);

assert!(object1.is_some());
assert!(object2.is_none());
Expand All @@ -315,7 +315,7 @@ mod tests {

#[test]
fn e2e() {
let pool = Pool::new(10, || Vec::new());
let pool = Pool::new(10, Vec::new);
let mut objects = Vec::new();

for i in 0..10 {
Expand All @@ -328,7 +328,7 @@ mod tests {
drop(objects);
assert!(pool.try_pull().is_some());

for i in (10..0).rev() {
for i in (0..10).rev() {
let mut object = pool.objects.lock().pop().unwrap();
assert_eq!(object.pop(), Some(i));
}
Expand Down
Loading