Forráskód Böngészése

switch from slab to slotmap for tasks to fix that ABA problem (#2488)

Evan Almloff 1 éve
szülő
commit
e4764f2bba
5 módosított fájl, 40 hozzáadás és 19 törlés
  1. 11 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 1 0
      packages/core/Cargo.toml
  4. 3 1
      packages/core/src/runtime.rs
  5. 24 18
      packages/core/src/tasks.rs

+ 11 - 0
Cargo.lock

@@ -2253,6 +2253,7 @@ dependencies = [
  "rustversion",
  "serde",
  "slab",
+ "slotmap",
  "tokio",
  "tracing",
  "tracing-fluent-assertions",
@@ -8343,6 +8344,16 @@ dependencies = [
  "rustc-hash",
 ]
 
+[[package]]
+name = "slotmap"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dbff4acf519f630b3a3ddcfaea6c06b42174d9a44bc70c620e9ed1649d58b82a"
+dependencies = [
+ "serde",
+ "version_check",
+]
+
 [[package]]
 name = "smallvec"
 version = "1.13.2"

+ 1 - 0
Cargo.toml

@@ -86,6 +86,7 @@ tracing-futures = "0.2.5"
 toml = "0.8"
 tokio = "1.28"
 slab = "0.4.2"
+slotmap = { version = "1.0.7", features = ["serde"] }
 futures-channel = "0.3.21"
 futures-util = { version = "0.3", default-features = false }
 rustc-hash = "1.1.0"

+ 1 - 0
packages/core/Cargo.toml

@@ -17,6 +17,7 @@ futures-util = { workspace = true, default-features = false, features = [
     "std",
 ] }
 slab = { workspace = true }
+slotmap = { workspace = true }
 futures-channel = { workspace = true }
 tracing = { workspace = true }
 serde = { version = "1", features = ["derive"], optional = true }

+ 3 - 1
packages/core/src/runtime.rs

@@ -1,3 +1,5 @@
+use slotmap::DefaultKey;
+
 use crate::innerlude::Effect;
 use crate::{
     innerlude::{LocalTask, SchedulerMsg},
@@ -27,7 +29,7 @@ pub struct Runtime {
     pub(crate) current_task: Cell<Option<Task>>,
 
     /// Tasks created with cx.spawn
-    pub(crate) tasks: RefCell<slab::Slab<Rc<LocalTask>>>,
+    pub(crate) tasks: RefCell<slotmap::SlotMap<DefaultKey, Rc<LocalTask>>>,
 
     // Currently suspended tasks
     pub(crate) suspended_tasks: Cell<usize>,

+ 24 - 18
packages/core/src/tasks.rs

@@ -3,6 +3,7 @@ use crate::innerlude::ScopeOrder;
 use crate::innerlude::{remove_future, spawn, Runtime};
 use crate::ScopeId;
 use futures_util::task::ArcWake;
+use slotmap::DefaultKey;
 use std::sync::Arc;
 use std::task::Waker;
 use std::{cell::Cell, future::Future};
@@ -14,7 +15,7 @@ use std::{pin::Pin, task::Poll};
 /// `Task` is a unique identifier for a task that has been spawned onto the runtime. It can be used to cancel the task
 #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
-pub struct Task(pub(crate) usize);
+pub struct Task(pub(crate) slotmap::DefaultKey);
 
 impl Task {
     /// Start a new future on the same thread as the rest of the VirtualDom.
@@ -139,24 +140,29 @@ impl Runtime {
         let (task, task_id) = {
             let mut tasks = self.tasks.borrow_mut();
 
-            let entry = tasks.vacant_entry();
-            let task_id = Task(entry.key());
-
-            let task = Rc::new(LocalTask {
-                scope,
-                active: Cell::new(true),
-                parent: self.current_task(),
-                task: RefCell::new(Box::pin(task)),
-                waker: futures_util::task::waker(Arc::new(LocalTaskHandle {
-                    id: task_id,
-                    tx: self.sender.clone(),
-                })),
-                ty: Cell::new(ty),
+            let mut task_id = Task(DefaultKey::default());
+            let mut local_task = None;
+            tasks.insert_with_key(|key| {
+                task_id = Task(key);
+
+                let new_task = Rc::new(LocalTask {
+                    scope,
+                    active: Cell::new(true),
+                    parent: self.current_task(),
+                    task: RefCell::new(Box::pin(task)),
+                    waker: futures_util::task::waker(Arc::new(LocalTaskHandle {
+                        id: task_id,
+                        tx: self.sender.clone(),
+                    })),
+                    ty: Cell::new(ty),
+                });
+
+                local_task = Some(new_task.clone());
+
+                new_task
             });
 
-            entry.insert(task.clone());
-
-            (task, task_id)
+            (local_task.unwrap(), task_id)
         };
 
         // Get a borrow on the task, holding no borrows on the tasks map
@@ -244,7 +250,7 @@ impl Runtime {
     ///
     /// This does not abort the task, so you'll want to wrap it in an abort handle if that's important to you
     pub(crate) fn remove_task(&self, id: Task) -> Option<Rc<LocalTask>> {
-        let task = self.tasks.borrow_mut().try_remove(id.0);
+        let task = self.tasks.borrow_mut().remove(id.0);
         if let Some(task) = &task {
             if task.suspended() {
                 self.suspended_tasks.set(self.suspended_tasks.get() - 1);