Skip to content

Commit

Permalink
feat: Use FxHashSet for dirty nodes in Torin
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Sep 23, 2023
1 parent 0aba793 commit c6488ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
10 changes: 5 additions & 5 deletions crates/torin/src/torin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;

pub use euclid::Rect;
use fxhash::FxHashMap;
use fxhash::{FxHashMap, FxHashSet};
use tracing::info;

use crate::{
Expand Down Expand Up @@ -30,7 +30,7 @@ pub struct Torin<Key: NodeKey> {
pub results: FxHashMap<Key, NodeAreas>,

/// Invalid registered nodes since previous layout measurement
pub dirty: HashSet<Key>,
pub dirty: FxHashSet<Key>,

/// Best Root node candidate from where to start measuring
pub root_node_candidate: RootNodeCandidate<Key>,
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<Key: NodeKey> Torin<Key> {
pub fn new() -> Self {
Self {
results: HashMap::default(),
dirty: HashSet::new(),
dirty: FxHashSet::default(),
root_node_candidate: RootNodeCandidate::None,
}
}
Expand All @@ -112,7 +112,7 @@ impl<Key: NodeKey> Torin<Key> {
}

/// Read the HashSet of dirty nodes
pub fn get_dirty_nodes(&self) -> &HashSet<Key> {
pub fn get_dirty_nodes(&self) -> &FxHashSet<Key> {
&self.dirty
}

Expand Down
13 changes: 7 additions & 6 deletions crates/torin/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;

use fxhash::FxHashSet;
use torin::prelude::*;

struct TestingMeasurer;
Expand Down Expand Up @@ -277,7 +278,7 @@ pub fn layout_dirty_nodes() {
);
layout.invalidate(2);

assert_eq!(layout.get_dirty_nodes(), &HashSet::from([2]));
assert_eq!(layout.get_dirty_nodes(), &FxHashSet::from_iter([2]));

// CASE 2
// Same as Case 1 but we make Child A depend on Child A[0]'s size
Expand All @@ -292,7 +293,7 @@ pub fn layout_dirty_nodes() {
);
layout.invalidate(1);

assert_eq!(layout.get_dirty_nodes(), &HashSet::from([2, 1]));
assert_eq!(layout.get_dirty_nodes(), &FxHashSet::from_iter([2, 1]));

// CASE 3
// Same as Case 2, but triggers a change in Child A[0]
Expand All @@ -307,7 +308,7 @@ pub fn layout_dirty_nodes() {
);
layout.invalidate(2);

assert_eq!(layout.get_dirty_nodes(), &HashSet::from([2, 1]));
assert_eq!(layout.get_dirty_nodes(), &FxHashSet::from_iter([2, 1]));

// CASE 4
// Same as Case 3, but triggers a change in the root
Expand All @@ -322,7 +323,7 @@ pub fn layout_dirty_nodes() {
);
layout.invalidate(0);

assert_eq!(layout.get_dirty_nodes(), &HashSet::from([2, 1, 0]));
assert_eq!(layout.get_dirty_nodes(), &FxHashSet::from_iter([2, 1, 0]));
}

#[test]
Expand Down Expand Up @@ -685,7 +686,7 @@ pub fn node_removal() {

layout.find_best_root(&mut mocked_dom);

assert_eq!(layout.get_dirty_nodes(), &HashSet::from([1, 3]));
assert_eq!(layout.get_dirty_nodes(), &FxHashSet::from_iter([1, 3]));

layout.measure(
0,
Expand Down

0 comments on commit c6488ad

Please sign in to comment.