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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions reactive_graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async-lock = { workspace = true, default-features = true }
send_wrapper = { features = [
"futures",
], workspace = true, default-features = true }
indexmap = { workspace = true, default-features = true }

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
web-sys = { version = "0.3.77", features = ["console"] }
Expand Down
53 changes: 25 additions & 28 deletions reactive_graph/src/graph/sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@
//! a linear search is not significantly more expensive than a hash and lookup.

use super::{AnySource, AnySubscriber, Source};
use core::slice;
use std::{mem, vec::IntoIter};
use indexmap::IndexSet;
use rustc_hash::FxHasher;
use std::{hash::BuildHasherDefault, mem};

type FxIndexSet<T> = IndexSet<T, BuildHasherDefault<FxHasher>>;

#[derive(Default, Clone, Debug)]
pub struct SourceSet(Vec<AnySource>);
pub struct SourceSet(FxIndexSet<AnySource>);

impl SourceSet {
pub fn new() -> Self {
Self(Default::default())
}

pub fn insert(&mut self, source: AnySource) {
self.0.push(source);
self.0.insert(source);
}

pub fn remove(&mut self, source: &AnySource) {
if let Some(pos) = self.0.iter().position(|s| s == source) {
self.0.remove(pos);
}
self.0.shift_remove(source);
}

pub fn take(&mut self) -> Vec<AnySource> {
pub fn take(&mut self) -> FxIndexSet<AnySource> {
mem::take(&mut self.0)
}

Expand All @@ -44,7 +45,7 @@ impl SourceSet {

impl IntoIterator for SourceSet {
type Item = AnySource;
type IntoIter = IntoIter<AnySource>;
type IntoIter = <FxIndexSet<AnySource> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
Expand All @@ -53,40 +54,36 @@ impl IntoIterator for SourceSet {

impl<'a> IntoIterator for &'a SourceSet {
type Item = &'a AnySource;
type IntoIter = slice::Iter<'a, AnySource>;
type IntoIter = <&'a FxIndexSet<AnySource> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
#[derive(Debug, Default, Clone)]
pub struct SubscriberSet(Vec<AnySubscriber>);
pub struct SubscriberSet(FxIndexSet<AnySubscriber>);

impl SubscriberSet {
pub fn new() -> Self {
Self(Vec::with_capacity(2))
Self(FxIndexSet::with_capacity_and_hasher(2, Default::default()))
}

pub fn subscribe(&mut self, subscriber: AnySubscriber) {
if !self.0.contains(&subscriber) {
self.0.push(subscriber);
}
self.0.insert(subscriber);
}

pub fn unsubscribe(&mut self, subscriber: &AnySubscriber) {
if let Some(pos) = self.0.iter().position(|s| s == subscriber) {
// note: do not use `.swap_remove()` here.
// using `.remove()` is slower because it shifts other items
// but it maintains the order of the subscribers, which is important
// to correctness when you're using this to drive something like a UI,
// which can have nested effects, where the inner one assumes the outer
// has already run (for example, an outer effect that checks .is_some(),
// and an inner effect that unwraps)
self.0.remove(pos);
}
// note: do not use `.swap_remove()` here.
// using `.remove()` is slower because it shifts other items
// but it maintains the order of the subscribers, which is important
// to correctness when you're using this to drive something like a UI,
// which can have nested effects, where the inner one assumes the outer
// has already run (for example, an outer effect that checks .is_some(),
// and an inner effect that unwraps)
self.0.shift_remove(subscriber);
}

pub fn take(&mut self) -> Vec<AnySubscriber> {
pub fn take(&mut self) -> FxIndexSet<AnySubscriber> {
mem::take(&mut self.0)
}

Expand All @@ -97,7 +94,7 @@ impl SubscriberSet {

impl IntoIterator for SubscriberSet {
type Item = AnySubscriber;
type IntoIter = IntoIter<AnySubscriber>;
type IntoIter = <FxIndexSet<AnySubscriber> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
Expand All @@ -106,7 +103,7 @@ impl IntoIterator for SubscriberSet {

impl<'a> IntoIterator for &'a SubscriberSet {
type Item = &'a AnySubscriber;
type IntoIter = slice::Iter<'a, AnySubscriber>;
type IntoIter = <&'a FxIndexSet<AnySubscriber> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.iter()
Expand Down
Loading