From 0fd88fbe3fc483c5f7ab3b9e75558b0061402c0e Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Fri, 25 Nov 2022 12:07:10 +0100 Subject: [PATCH] enable id reusing --- Cargo.lock | 1 + crates/turbo-tasks/Cargo.toml | 1 + crates/turbo-tasks/src/id_factory.rs | 14 ++++++++++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0dd55a7e20452..794dd3d76968d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6371,6 +6371,7 @@ dependencies = [ "anyhow", "auto-hash-map", "bitflags", + "concurrent-queue", "dashmap", "erased-serde", "event-listener", diff --git a/crates/turbo-tasks/Cargo.toml b/crates/turbo-tasks/Cargo.toml index f50be5505ac92..a4a6c0c9428d9 100644 --- a/crates/turbo-tasks/Cargo.toml +++ b/crates/turbo-tasks/Cargo.toml @@ -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" diff --git a/crates/turbo-tasks/src/id_factory.rs b/crates/turbo-tasks/src/id_factory.rs index f269220dbf4f7..5335862466742 100644 --- a/crates/turbo-tasks/src/id_factory.rs +++ b/crates/turbo-tasks/src/id_factory.rs @@ -4,10 +4,12 @@ use std::{ sync::atomic::{AtomicUsize, Ordering}, }; +use concurrent_queue::ConcurrentQueue; +use once_cell::sync::Lazy; + pub struct IdFactory { next_id: AtomicUsize, - // TODO - // free_ids: AtomicPtr<...> + free_ids: Lazy>, phantom_data: PhantomData, } @@ -21,18 +23,22 @@ impl + Deref> IdFactory { 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); } }