Skip to content

Commit

Permalink
enable id reusing (vercel/turborepo#2895)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
sokra and kodiakhq[bot] authored Dec 5, 2022
1 parent 72e5974 commit 35f5ddd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/turbo-tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ any_key = "0.1.1"
anyhow = "1.0.47"
auto-hash-map = { path = "../auto-hash-map" }
bitflags = "1.3.2"
concurrent-queue = "1.2.2"
dashmap = "5.4.0"
erased-serde = "0.3.20"
event-listener = "2.5.3"
Expand Down
14 changes: 10 additions & 4 deletions crates/turbo-tasks/src/id_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::{
sync::atomic::{AtomicUsize, Ordering},
};

use concurrent_queue::ConcurrentQueue;
use once_cell::sync::Lazy;

pub struct IdFactory<T> {
next_id: AtomicUsize,
// TODO
// free_ids: AtomicPtr<...>
free_ids: Lazy<ConcurrentQueue<T>>,
phantom_data: PhantomData<T>,
}

Expand All @@ -21,18 +23,22 @@ impl<T: From<usize> + Deref<Target = usize>> IdFactory<T> {
pub const fn new() -> Self {
Self {
next_id: AtomicUsize::new(1),
free_ids: Lazy::new(|| ConcurrentQueue::unbounded()),
phantom_data: PhantomData,
}
}

pub fn get(&self) -> T {
if let Ok(id) = self.free_ids.pop() {
return id;
}
self.next_id.fetch_add(1, Ordering::Relaxed).into()
}

/// # Safety
///
/// It must be ensured that the id is no longer used
pub unsafe fn reuse(&self, _id: T) {
// TODO
pub unsafe fn reuse(&self, id: T) {
let _ = self.free_ids.push(id);
}
}

0 comments on commit 35f5ddd

Please sign in to comment.