Ver código fonte

Use linear scans on contexts

Jonathan Kelley 2 anos atrás
pai
commit
3a1b22aec2
1 arquivos alterados com 13 adições e 6 exclusões
  1. 13 6
      packages/core/src/scopes.rs

+ 13 - 6
packages/core/src/scopes.rs

@@ -11,7 +11,7 @@ use crate::{
 };
 use bumpalo::{boxed::Box as BumpBox, Bump};
 use bumpslab::{BumpSlab, Slot};
-use rustc_hash::{FxHashMap, FxHashSet};
+use rustc_hash::FxHashSet;
 use slab::{Slab, VacantEntry};
 use std::{
     any::{Any, TypeId},
@@ -173,7 +173,7 @@ pub struct ScopeState {
     pub(crate) hooks: RefCell<Vec<Box<UnsafeCell<dyn Any>>>>,
     pub(crate) hook_idx: Cell<usize>,
 
-    pub(crate) shared_contexts: RefCell<FxHashMap<TypeId, Box<dyn Any>>>,
+    pub(crate) shared_contexts: RefCell<Vec<(TypeId, Box<dyn Any>)>>,
 
     pub(crate) tasks: Rc<Scheduler>,
     pub(crate) spawned_tasks: RefCell<FxHashSet<TaskId>>,
@@ -347,7 +347,9 @@ impl<'src> ScopeState {
     pub fn has_context<T: 'static + Clone>(&self) -> Option<T> {
         self.shared_contexts
             .borrow()
-            .get(&TypeId::of::<T>())?
+            .iter()
+            .find(|(k, _)| *k == TypeId::of::<T>())
+            .map(|(_, v)| v)?
             .downcast_ref::<T>()
             .cloned()
     }
@@ -364,8 +366,13 @@ impl<'src> ScopeState {
         while let Some(parent_ptr) = search_parent {
             // safety: all parent pointers are valid thanks to the bump arena
             let parent = unsafe { &*parent_ptr };
-            if let Some(shared) = parent.shared_contexts.borrow().get(&TypeId::of::<T>()) {
-                return shared.downcast_ref::<T>().cloned();
+            if let Some(shared) = parent
+                .shared_contexts
+                .borrow()
+                .iter()
+                .find(|(k, _)| *k == TypeId::of::<T>())
+            {
+                return shared.1.downcast_ref::<T>().cloned();
             }
             search_parent = parent.parent;
         }
@@ -398,7 +405,7 @@ impl<'src> ScopeState {
 
         self.shared_contexts
             .borrow_mut()
-            .insert(TypeId::of::<T>(), Box::new(value));
+            .push((TypeId::of::<T>(), Box::new(value)));
 
         value2
     }