on master help sign in
Use a hash set for cached roots
commit97d6f409db3b5a4b61c160bcc4a419d687a49f9c authorNick Fitzgerald <fitzgen@gmail.com> dateWed 10 Apr 2019, 16:29 (8 years ago) parent56f9f86
4 files changed+24−20
src/cached_set.rs+13−9 src/change_list/mod.rs+1−3 src/diff.rs+7−6 src/vdom.rs+3−2
src/cached_set.rs+13−9
@@ -38,27 +38,31 @@ pub(crate) struct CacheEntry {
3838}
3939
4040impl CachedSet {
41− pub(crate) fn gc(&mut self, registry: &mut EventsRegistry, roots: &[CacheId]) {
42− // TODO: We need to port a hash set over in `bumpalo::collections` so
43− // that we can use a temporary bump arena for this set.
44− let mut mark_bits = FxHashSet::default();
45− mark_bits.reserve(self.items.len());
41+ pub(crate) fn new_roots_set(&self) -> FxHashSet<CacheId> {
42+ let mut roots = FxHashSet::default();
43+ roots.reserve(self.items.len());
44+ roots
45+ }
46+
47+ pub(crate) fn gc(&mut self, registry: &mut EventsRegistry, roots: FxHashSet<CacheId>) {
48+ let mut marked = FxHashSet::default();
49+ marked.reserve(self.items.len());
4650
4751 for root in roots {
48− if mark_bits.insert(*root) {
52+ if marked.insert(root) {
4953 let entry = self
5054 .items
51− .get(root)
55+ .get(&root)
5256 .expect_throw("CachedSet::gc: should have root in cached set");
5357
5458 // NB: no need to recurse here because `edges` already contains
5559 // transitive edges!
56− mark_bits.extend(entry.edges.iter().cloned());
60+ marked.extend(entry.edges.iter().cloned());
5761 }
5862 }
5963
6064 self.items.retain(|id, entry| {
61− let keep = mark_bits.contains(id);
65+ let keep = marked.contains(id);
6266 if !keep {
6367 debug!("CachedSet::gc: removing {:?}", id);
6468 let node: &Node = unsafe { &*entry.node };
src/change_list/mod.rs+1−3
@@ -43,9 +43,7 @@ impl ChangeListPersistentState {
4343 }
4444
4545 pub(crate) fn builder<'a>(&'a mut self) -> ChangeListBuilder<'a> {
46− ChangeListBuilder {
47− state: self,
48− }
46+ ChangeListBuilder { state: self }
4947 }
5048}
5149
src/diff.rs+7−6
@@ -4,6 +4,7 @@ use crate::{
44 events::EventsRegistry,
55 node::{Attribute, ElementNode, Listener, Node, NodeKind, TextNode},
66};
7+use fxhash::FxHashSet;
78use std::cmp;
89
910pub(crate) fn diff(
@@ -12,7 +13,7 @@ pub(crate) fn diff(
1213 registry: &mut EventsRegistry,
1314 old: Node,
1415 new: Node,
15− cached_roots: &mut bumpalo::collections::Vec<CacheId>,
16+ cached_roots: &mut FxHashSet<CacheId>,
1617) {
1718 match (&new.kind, &old.kind) {
1819 (
@@ -79,7 +80,7 @@ pub(crate) fn diff(
7980
8081 // Both the new and old nodes are cached.
8182 (&NodeKind::Cached(ref new), &NodeKind::Cached(ref old)) => {
82− cached_roots.push(new.id);
83+ cached_roots.insert(new.id);
8384
8485 if new.id == old.id {
8586 // This is the same cached node, so nothing has changed!
@@ -95,7 +96,7 @@ pub(crate) fn diff(
9596 // we assume that they are pretty different, and it isn't worth diffing
9697 // the subtrees, so we just create the new cached node afresh.
9798 (&NodeKind::Cached(ref c), _) => {
98− cached_roots.push(c.id);
99+ cached_roots.insert(c.id);
99100 let new = cached_set.get(c.id);
100101 create(cached_set, change_list, registry, new, cached_roots);
101102 registry.remove_subtree(&old);
@@ -187,7 +188,7 @@ fn diff_children(
187188 registry: &mut EventsRegistry,
188189 old: &[Node],
189190 new: &[Node],
190− cached_roots: &mut bumpalo::collections::Vec<CacheId>,
191+ cached_roots: &mut FxHashSet<CacheId>,
191192) {
192193 debug!(" updating children shared by old and new");
193194
@@ -259,7 +260,7 @@ fn create(
259260 change_list: &mut ChangeListBuilder,
260261 registry: &mut EventsRegistry,
261262 node: Node,
262− cached_roots: &mut bumpalo::collections::Vec<CacheId>,
263+ cached_roots: &mut FxHashSet<CacheId>,
263264) {
264265 match node.kind {
265266 NodeKind::Text(TextNode { text }) => {
@@ -298,7 +299,7 @@ fn create(
298299 }
299300 }
300301 NodeKind::Cached(c) => {
301− cached_roots.push(c.id);
302+ cached_roots.insert(c.id);
302303 let node = cached_set.get(c.id);
303304 create(cached_set, change_list, registry, node, cached_roots)
304305 }
src/vdom.rs+3−2
@@ -268,9 +268,10 @@ impl VdomInnerExclusive {
268268
269269 // Diff the old contents with the new contents.
270270 let old_contents = self.current_root.take().unwrap();
271− let mut cache_roots = bumpalo::collections::Vec::new_in(&dom_buffers[1]);
271+ let mut cache_roots;
272272 {
273273 let cached_set = self.cached_set.borrow();
274+ cache_roots = cached_set.new_roots_set();
274275 let mut change_list = self.change_list.builder();
275276 crate::diff::diff(
276277 &cached_set,
@@ -288,7 +289,7 @@ impl VdomInnerExclusive {
288289 {
289290 // Clean up unused cached renders.
290291 let mut cached_set = self.cached_set.borrow_mut();
291− cached_set.gc(&mut registry, &cache_roots);
292+ cached_set.gc(&mut registry, cache_roots);
292293 }
293294
294295 // Swap the buffers to make the bump arena with the new contents the