diff.rs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. //! This module contains the stateful DiffMachine and all methods to diff VNodes, their properties, and their children.
  2. //!
  3. //! The [`DiffMachine`] calculates the diffs between the old and new frames, updates the new nodes, and generates a set
  4. //! of mutations for the RealDom to apply.
  5. //!
  6. //! ## Notice:
  7. //!
  8. //! The inspiration and code for this module was originally taken from Dodrio (@fitzgen) and then modified to support
  9. //! Components, Fragments, Suspense, SubTree memoization, incremental diffing, cancelation, NodeRefs, pausing, priority
  10. //! scheduling, and additional batching operations.
  11. //!
  12. //! ## Implementation Details:
  13. //!
  14. //! ### IDs for elements
  15. //! --------------------
  16. //! All nodes are addressed by their IDs. The RealDom provides an imperative interface for making changes to these nodes.
  17. //! We don't necessarily require that DOM changes happen instantly during the diffing process, so the implementor may choose
  18. //! to batch nodes if it is more performant for their application. The element IDs are indicies into the internal element
  19. //! array. The expectation is that implemenetors will use the ID as an index into a Vec of real nodes, allowing for passive
  20. //! garbage collection as the VirtualDOM replaces old nodes.
  21. //!
  22. //! When new vnodes are created through `cx.render`, they won't know which real node they correspond to. During diffing,
  23. //! we always make sure to copy over the ID. If we don't do this properly, the ElementId will be populated incorrectly
  24. //! and brick the user's page.
  25. //!
  26. //! ### Fragment Support
  27. //! --------------------
  28. //! Fragments (nodes without a parent) are supported through a combination of "replace with" and anchor vnodes. Fragments
  29. //! can be particularly challenging when they are empty, so the anchor node lets us "reserve" a spot for the empty
  30. //! fragment to be replaced with when it is no longer empty. This is guaranteed by logic in the NodeFactory - it is
  31. //! impossible to craft a fragment with 0 elements - they must always have at least a single placeholder element. Adding
  32. //! "dummy" nodes _is_ inefficient, but it makes our diffing algorithm faster and the implementation is completely up to
  33. //! the platform.
  34. //!
  35. //! Other implementations either don't support fragments or use a "child + sibling" pattern to represent them. Our code is
  36. //! vastly simpler and more performant when we can just create a placeholder element while the fragment has no children.
  37. //!
  38. //! ### Suspense
  39. //! ------------
  40. //! Dioxus implements suspense slightly differently than React. In React, each fiber is manually progressed until it runs
  41. //! into a promise-like value. React will then work on the next "ready" fiber, checking back on the previous fiber once
  42. //! it has finished its new work. In Dioxus, we use a similar approach, but try to completely render the tree before
  43. //! switching sub-fibers. Instead, each future is submitted into a futures-queue and the node is manually loaded later on.
  44. //! Due to the frequent calls to "yield_now" we can get the pure "fetch-as-you-render" behavior of React fiber.
  45. //!
  46. //! We're able to use this approach because we use placeholder nodes - futures that aren't ready still get submitted to
  47. //! DOM, but as a placeholder.
  48. //!
  49. //! Right now, the "suspense" queue is intertwined the hooks. In the future, we should allow any future to drive attributes
  50. //! and contents, without the need for the "use_suspense" hook. In the interim, this is the quickest way to get suspense working.
  51. //!
  52. //! ## Subtree Memoization
  53. //! -----------------------
  54. //! We also employ "subtree memoization" which saves us from having to check trees which take no dynamic content. We can
  55. //! detect if a subtree is "static" by checking if its children are "static". Since we dive into the tree depth-first, the
  56. //! calls to "create" propogate this information upwards. Structures like the one below are entirely static:
  57. //! ```rust
  58. //! rsx!( div { class: "hello world", "this node is entirely static" } )
  59. //! ```
  60. //! Because the subtrees won't be diffed, their "real node" data will be stale (invalid), so its up to the reconciler to
  61. //! track nodes created in a scope and clean up all relevant data. Support for this is currently WIP and depends on comp-time
  62. //! hashing of the subtree from the rsx! macro. We do a very limited form of static analysis via static string pointers as
  63. //! a way of short-circuiting the most expensive checks.
  64. //!
  65. //! ## Bloom Filter and Heuristics
  66. //! ------------------------------
  67. //! For all components, we employ some basic heuristics to speed up allocations and pre-size bump arenas. The heuristics are
  68. //! currently very rough, but will get better as time goes on. The information currently tracked includes the size of a
  69. //! bump arena after first render, the number of hooks, and the number of nodes in the tree.
  70. //!
  71. //! ## Garbage Collection
  72. //! ---------------------
  73. //! Dioxus uses a passive garbage collection system to clean up old nodes once the work has been completed. This garabge
  74. //! collection is done internally once the main diffing work is complete. After the "garbage" is collected, Dioxus will then
  75. //! start to re-use old keys for new nodes. This results in a passive memory management system that is very efficient.
  76. //!
  77. //! The IDs used by the key/map are just an index into a vec. This means that Dioxus will drive the key allocation strategy
  78. //! so the client only needs to maintain a simple list of nodes. By default, Dioxus will not manually clean up old nodes
  79. //! for the client. As new nodes are created, old nodes will be over-written.
  80. //!
  81. //! ## Further Reading and Thoughts
  82. //! ----------------------------
  83. //! There are more ways of increasing diff performance here that are currently not implemented.
  84. //! - Strong memoization of subtrees.
  85. //! - Guided diffing.
  86. //! - Certain web-dom-specific optimizations.
  87. //!
  88. //! More info on how to improve this diffing algorithm:
  89. //! - https://hacks.mozilla.org/2019/03/fast-bump-allocated-virtual-doms-with-rust-and-wasm/
  90. use crate::innerlude::*;
  91. use fxhash::{FxHashMap, FxHashSet};
  92. use DomEdit::*;
  93. /// Our DiffMachine is an iterative tree differ.
  94. ///
  95. /// It uses techniques of a stack machine to allow pausing and restarting of the diff algorithm. This
  96. /// was origially implemented using recursive techniques, but Rust lacks the abilty to call async functions recursively,
  97. /// meaning we could not "pause" the original diffing algorithm.
  98. ///
  99. /// Instead, we use a traditional stack machine approach to diff and create new nodes. The diff algorithm periodically
  100. /// calls "yield_now" which allows the machine to pause and return control to the caller. The caller can then wait for
  101. /// the next period of idle time, preventing our diff algorithm from blocking the main thread.
  102. ///
  103. /// Funnily enough, this stack machine's entire job is to create instructions for another stack machine to execute. It's
  104. /// stack machines all the way down!
  105. pub(crate) struct DiffMachine<'bump> {
  106. pub vdom: &'bump ResourcePool,
  107. pub mutations: Mutations<'bump>,
  108. pub stack: DiffStack<'bump>,
  109. pub seen_scopes: FxHashSet<ScopeId>,
  110. pub cfg: DiffCfg,
  111. }
  112. pub(crate) struct DiffCfg {
  113. pub force_diff: bool,
  114. }
  115. impl Default for DiffCfg {
  116. fn default() -> Self {
  117. Self {
  118. force_diff: Default::default(),
  119. }
  120. }
  121. }
  122. /// a "saved" form of a diff machine
  123. /// in regular diff machine, the &'bump reference is a stack borrow, but the
  124. /// bump lifetimes are heap borrows.
  125. pub(crate) struct SavedDiffWork<'bump> {
  126. pub mutations: Mutations<'bump>,
  127. pub stack: DiffStack<'bump>,
  128. pub seen_scopes: FxHashSet<ScopeId>,
  129. }
  130. impl<'a> SavedDiffWork<'a> {
  131. pub unsafe fn extend(self: SavedDiffWork<'a>) -> SavedDiffWork<'static> {
  132. std::mem::transmute(self)
  133. }
  134. pub unsafe fn promote<'b>(self, vdom: &'b mut ResourcePool) -> DiffMachine<'b> {
  135. let extended: SavedDiffWork<'b> = std::mem::transmute(self);
  136. DiffMachine {
  137. vdom,
  138. cfg: DiffCfg::default(),
  139. mutations: extended.mutations,
  140. stack: extended.stack,
  141. seen_scopes: extended.seen_scopes,
  142. }
  143. }
  144. }
  145. impl<'bump> DiffMachine<'bump> {
  146. pub(crate) fn new(mutations: Mutations<'bump>, shared: &'bump ResourcePool) -> Self {
  147. Self {
  148. mutations,
  149. cfg: DiffCfg::default(),
  150. stack: DiffStack::new(),
  151. vdom: shared,
  152. seen_scopes: FxHashSet::default(),
  153. }
  154. }
  155. pub fn save(self) -> SavedDiffWork<'bump> {
  156. SavedDiffWork {
  157. mutations: self.mutations,
  158. stack: self.stack,
  159. seen_scopes: self.seen_scopes,
  160. }
  161. }
  162. pub fn diff_scope(&mut self, id: ScopeId) {
  163. if let Some(component) = self.vdom.get_scope_mut(id) {
  164. let (old, new) = (component.frames.wip_head(), component.frames.fin_head());
  165. self.stack.push(DiffInstruction::Diff { new, old });
  166. self.work(|| false);
  167. }
  168. }
  169. /// Progress the diffing for this "fiber"
  170. ///
  171. /// This method implements a depth-first iterative tree traversal.
  172. ///
  173. /// We do depth-first to maintain high cache locality (nodes were originally generated recursively).
  174. ///
  175. /// Returns a `bool` indicating that the work completed properly.
  176. pub fn work(&mut self, mut deadline_expired: impl FnMut() -> bool) -> bool {
  177. while let Some(instruction) = self.stack.pop() {
  178. match instruction {
  179. DiffInstruction::Diff { old, new } => self.diff_node(old, new),
  180. DiffInstruction::Create { node } => self.create_node(node),
  181. DiffInstruction::Mount { and } => self.mount(and),
  182. DiffInstruction::PrepareMove { node } => self.prepare_move_node(node),
  183. DiffInstruction::PopScope => self.stack.pop_off_scope(),
  184. };
  185. if deadline_expired() {
  186. return false;
  187. }
  188. }
  189. true
  190. }
  191. fn prepare_move_node(&mut self, node: &'bump VNode<'bump>) {
  192. for el in RealChildIterator::new(node, self.vdom) {
  193. self.mutations.push_root(el.mounted_id());
  194. self.stack.add_child_count(1);
  195. }
  196. }
  197. fn mount(&mut self, and: MountType<'bump>) {
  198. let nodes_created = self.stack.pop_nodes_created();
  199. match and {
  200. // add the nodes from this virtual list to the parent
  201. // used by fragments and components
  202. MountType::Absorb => {
  203. self.stack.add_child_count(nodes_created);
  204. }
  205. MountType::Replace { old } => {
  206. if let Some(old_id) = old.try_mounted_id() {
  207. self.mutations.replace_with(old_id, nodes_created as u32);
  208. } else {
  209. let mut iter = RealChildIterator::new(old, self.vdom);
  210. let first = iter.next().unwrap();
  211. self.mutations
  212. .replace_with(first.mounted_id(), nodes_created as u32);
  213. self.remove_nodes(iter);
  214. }
  215. }
  216. MountType::ReplaceByElementId { el } => {
  217. if let Some(old) = el {
  218. self.mutations.replace_with(old, nodes_created as u32);
  219. }
  220. }
  221. MountType::Append => {
  222. self.mutations.edits.push(AppendChildren {
  223. many: nodes_created as u32,
  224. });
  225. }
  226. MountType::InsertAfter { other_node } => {
  227. let root = self.find_last_element(other_node).unwrap();
  228. self.mutations.insert_after(root, nodes_created as u32);
  229. }
  230. MountType::InsertBefore { other_node } => {
  231. let root = self.find_first_element_id(other_node).unwrap();
  232. self.mutations.insert_before(root, nodes_created as u32);
  233. }
  234. }
  235. }
  236. // =================================
  237. // Tools for creating new nodes
  238. // =================================
  239. fn create_node(&mut self, node: &'bump VNode<'bump>) {
  240. match node {
  241. VNode::Text(vtext) => self.create_text_node(vtext, node),
  242. VNode::Suspended(suspended) => self.create_suspended_node(suspended, node),
  243. VNode::Anchor(anchor) => self.create_anchor_node(anchor, node),
  244. VNode::Element(element) => self.create_element_node(element, node),
  245. VNode::Fragment(frag) => self.create_fragment_node(frag),
  246. VNode::Component(component) => self.create_component_node(component),
  247. }
  248. }
  249. fn create_text_node(&mut self, vtext: &'bump VText<'bump>, node: &'bump VNode<'bump>) {
  250. let real_id = self.vdom.reserve_node(node);
  251. self.mutations.create_text_node(vtext.text, real_id);
  252. vtext.dom_id.set(Some(real_id));
  253. self.stack.add_child_count(1);
  254. }
  255. fn create_suspended_node(&mut self, suspended: &'bump VSuspended, node: &'bump VNode<'bump>) {
  256. let real_id = self.vdom.reserve_node(node);
  257. self.mutations.create_placeholder(real_id);
  258. suspended.dom_id.set(Some(real_id));
  259. self.stack.add_child_count(1);
  260. self.attach_suspended_node_to_scope(suspended);
  261. }
  262. fn create_anchor_node(&mut self, anchor: &'bump VAnchor, node: &'bump VNode<'bump>) {
  263. let real_id = self.vdom.reserve_node(node);
  264. self.mutations.create_placeholder(real_id);
  265. anchor.dom_id.set(Some(real_id));
  266. self.stack.add_child_count(1);
  267. }
  268. fn create_element_node(&mut self, element: &'bump VElement<'bump>, node: &'bump VNode<'bump>) {
  269. let VElement {
  270. tag_name,
  271. listeners,
  272. attributes,
  273. children,
  274. namespace,
  275. dom_id,
  276. ..
  277. } = element;
  278. let real_id = self.vdom.reserve_node(node);
  279. dom_id.set(Some(real_id));
  280. self.mutations.create_element(tag_name, *namespace, real_id);
  281. self.stack.add_child_count(1);
  282. if let Some(cur_scope_id) = self.stack.current_scope() {
  283. let scope = self.vdom.get_scope(cur_scope_id).unwrap();
  284. listeners.iter().for_each(|listener| {
  285. self.attach_listener_to_scope(listener, scope);
  286. listener.mounted_node.set(Some(real_id));
  287. self.mutations
  288. .new_event_listener(listener, cur_scope_id.clone());
  289. });
  290. } else {
  291. log::warn!("create element called with no scope on the stack - this is an error for a live dom");
  292. }
  293. for attr in *attributes {
  294. self.mutations.set_attribute(attr);
  295. }
  296. if children.is_empty() {
  297. self.stack.element_id_stack.push(real_id);
  298. // push our element_id onto the stack
  299. // drop our element off the stack
  300. self.stack.create_children(children, MountType::Append);
  301. }
  302. }
  303. fn create_fragment_node(&mut self, frag: &'bump VFragment<'bump>) {
  304. self.stack.create_children(frag.children, MountType::Absorb);
  305. }
  306. fn create_component_node(&mut self, vcomponent: &'bump VComponent<'bump>) {
  307. let caller = vcomponent.caller;
  308. let parent_idx = self.stack.current_scope().unwrap();
  309. let shared = self.vdom.channel.clone();
  310. // Insert a new scope into our component list
  311. let parent_scope = self.vdom.get_scope(parent_idx).unwrap();
  312. let new_idx = self.vdom.insert_scope_with_key(|new_idx| {
  313. let height = parent_scope.height + 1;
  314. Scope::new(
  315. caller,
  316. new_idx,
  317. Some(parent_idx),
  318. height,
  319. ScopeChildren(vcomponent.children),
  320. shared,
  321. )
  322. });
  323. // Actually initialize the caller's slot with the right address
  324. vcomponent.associated_scope.set(Some(new_idx));
  325. if !vcomponent.can_memoize {
  326. let cur_scope = self.vdom.get_scope_mut(parent_idx).unwrap();
  327. let extended = vcomponent as *const VComponent;
  328. let extended: *const VComponent<'static> = unsafe { std::mem::transmute(extended) };
  329. cur_scope.borrowed_props.borrow_mut().push(extended);
  330. }
  331. // TODO:
  332. // add noderefs to current noderef list Noderefs
  333. // add effects to current effect list Effects
  334. let new_component = self.vdom.get_scope_mut(new_idx).unwrap();
  335. // Run the scope for one iteration to initialize it
  336. if new_component.run_scope(self.vdom) {
  337. // Take the node that was just generated from running the component
  338. let nextnode = new_component.frames.fin_head();
  339. self.stack.create_component(new_idx, nextnode);
  340. }
  341. // Finally, insert this scope as a seen node.
  342. self.seen_scopes.insert(new_idx);
  343. }
  344. // =================================
  345. // Tools for diffing nodes
  346. // =================================
  347. pub fn diff_node(&mut self, old_node: &'bump VNode<'bump>, new_node: &'bump VNode<'bump>) {
  348. use VNode::*;
  349. match (old_node, new_node) {
  350. // Check the most common cases first
  351. (Text(old), Text(new)) => self.diff_text_nodes(old, new),
  352. (Component(old), Component(new)) => {
  353. self.diff_component_nodes(old_node, new_node, old, new)
  354. }
  355. (Fragment(old), Fragment(new)) => self.diff_fragment_nodes(old, new),
  356. (Anchor(old), Anchor(new)) => new.dom_id.set(old.dom_id.get()),
  357. (Suspended(old), Suspended(new)) => self.diff_suspended_nodes(old, new),
  358. (Element(old), Element(new)) => self.diff_element_nodes(old, new, new_node),
  359. // Anything else is just a basic replace and create
  360. (
  361. Component(_) | Fragment(_) | Text(_) | Element(_) | Anchor(_) | Suspended(_),
  362. Component(_) | Fragment(_) | Text(_) | Element(_) | Anchor(_) | Suspended(_),
  363. ) => self
  364. .stack
  365. .create_node(new_node, MountType::Replace { old: old_node }),
  366. }
  367. }
  368. fn diff_text_nodes(&mut self, old: &'bump VText<'bump>, new: &'bump VText<'bump>) {
  369. if let Some(root) = old.dom_id.get() {
  370. if old.text != new.text {
  371. self.mutations.push_root(root);
  372. self.mutations.set_text(new.text);
  373. self.mutations.pop();
  374. }
  375. new.dom_id.set(Some(root));
  376. }
  377. }
  378. fn diff_element_nodes(
  379. &mut self,
  380. old: &'bump VElement<'bump>,
  381. new: &'bump VElement<'bump>,
  382. new_node: &'bump VNode<'bump>,
  383. ) {
  384. let root = old.dom_id.get();
  385. // If the element type is completely different, the element needs to be re-rendered completely
  386. // This is an optimization React makes due to how users structure their code
  387. //
  388. // This case is rather rare (typically only in non-keyed lists)
  389. if new.tag_name != old.tag_name || new.namespace != old.namespace {
  390. // maybe make this an instruction?
  391. // issue is that we need the "vnode" but this method only has the velement
  392. self.stack.push_nodes_created(0);
  393. self.stack.push(DiffInstruction::Mount {
  394. and: MountType::ReplaceByElementId {
  395. el: old.dom_id.get(),
  396. },
  397. });
  398. self.create_element_node(new, new_node);
  399. return;
  400. }
  401. new.dom_id.set(root);
  402. // Don't push the root if we don't have to
  403. let mut has_comitted = false;
  404. let mut please_commit = |edits: &mut Vec<DomEdit>| {
  405. if !has_comitted {
  406. has_comitted = true;
  407. if let Some(root) = root {
  408. edits.push(PushRoot { id: root.as_u64() });
  409. }
  410. }
  411. };
  412. // Diff Attributes
  413. //
  414. // It's extraordinarily rare to have the number/order of attributes change
  415. // In these cases, we just completely erase the old set and make a new set
  416. //
  417. // TODO: take a more efficient path than this
  418. if old.attributes.len() == new.attributes.len() {
  419. for (old_attr, new_attr) in old.attributes.iter().zip(new.attributes.iter()) {
  420. if old_attr.value != new_attr.value {
  421. please_commit(&mut self.mutations.edits);
  422. self.mutations.set_attribute(new_attr);
  423. }
  424. }
  425. } else {
  426. please_commit(&mut self.mutations.edits);
  427. for attribute in old.attributes {
  428. self.mutations.remove_attribute(attribute);
  429. }
  430. for attribute in new.attributes {
  431. self.mutations.set_attribute(attribute)
  432. }
  433. }
  434. // Diff listeners
  435. //
  436. // It's extraordinarily rare to have the number/order of listeners change
  437. // In the cases where the listeners change, we completely wipe the data attributes and add new ones
  438. //
  439. // We also need to make sure that all listeners are properly attached to the parent scope (fix_listener)
  440. //
  441. // TODO: take a more efficient path than this
  442. if let Some(cur_scope_id) = self.stack.current_scope() {
  443. let scope = self.vdom.get_scope(cur_scope_id).unwrap();
  444. if old.listeners.len() == new.listeners.len() {
  445. for (old_l, new_l) in old.listeners.iter().zip(new.listeners.iter()) {
  446. if old_l.event != new_l.event {
  447. please_commit(&mut self.mutations.edits);
  448. self.mutations.remove_event_listener(old_l.event);
  449. self.mutations.new_event_listener(new_l, cur_scope_id);
  450. }
  451. new_l.mounted_node.set(old_l.mounted_node.get());
  452. self.attach_listener_to_scope(new_l, scope);
  453. }
  454. } else {
  455. please_commit(&mut self.mutations.edits);
  456. for listener in old.listeners {
  457. self.mutations.remove_event_listener(listener.event);
  458. }
  459. for listener in new.listeners {
  460. listener.mounted_node.set(root);
  461. self.mutations.new_event_listener(listener, cur_scope_id);
  462. self.attach_listener_to_scope(listener, scope);
  463. }
  464. }
  465. }
  466. self.diff_children(old.children, new.children);
  467. }
  468. fn diff_component_nodes(
  469. &mut self,
  470. old_node: &'bump VNode<'bump>,
  471. new_node: &'bump VNode<'bump>,
  472. old: &'bump VComponent<'bump>,
  473. new: &'bump VComponent<'bump>,
  474. ) {
  475. let scope_addr = old.associated_scope.get().unwrap();
  476. // Make sure we're dealing with the same component (by function pointer)
  477. if old.user_fc == new.user_fc {
  478. //
  479. self.stack.scope_stack.push(scope_addr);
  480. // Make sure the new component vnode is referencing the right scope id
  481. new.associated_scope.set(Some(scope_addr));
  482. // make sure the component's caller function is up to date
  483. let scope = self.vdom.get_scope_mut(scope_addr).unwrap();
  484. scope.update_scope_dependencies(new.caller, ScopeChildren(new.children));
  485. // React doesn't automatically memoize, but we do.
  486. let props_are_the_same = old.comparator.unwrap();
  487. if self.cfg.force_diff || !props_are_the_same(new) {
  488. let succeeded = scope.run_scope(self.vdom);
  489. if succeeded {
  490. self.diff_node(scope.frames.wip_head(), scope.frames.fin_head());
  491. }
  492. }
  493. self.stack.scope_stack.pop();
  494. self.seen_scopes.insert(scope_addr);
  495. } else {
  496. self.stack
  497. .create_node(new_node, MountType::Replace { old: old_node });
  498. }
  499. }
  500. fn diff_fragment_nodes(&mut self, old: &'bump VFragment<'bump>, new: &'bump VFragment<'bump>) {
  501. // This is the case where options or direct vnodes might be used.
  502. // In this case, it's faster to just skip ahead to their diff
  503. if old.children.len() == 1 && new.children.len() == 1 {
  504. self.diff_node(&old.children[0], &new.children[0]);
  505. return;
  506. }
  507. self.diff_children(old.children, new.children);
  508. }
  509. fn diff_suspended_nodes(&mut self, old: &'bump VSuspended, new: &'bump VSuspended) {
  510. new.dom_id.set(old.dom_id.get());
  511. self.attach_suspended_node_to_scope(new);
  512. }
  513. // =============================================
  514. // Utilites for creating new diff instructions
  515. // =============================================
  516. // Diff the given set of old and new children.
  517. //
  518. // The parent must be on top of the change list stack when this function is
  519. // entered:
  520. //
  521. // [... parent]
  522. //
  523. // the change list stack is in the same state when this function returns.
  524. //
  525. // If old no anchors are provided, then it's assumed that we can freely append to the parent.
  526. //
  527. // Remember, non-empty lists does not mean that there are real elements, just that there are virtual elements.
  528. //
  529. // Frament nodes cannot generate empty children lists, so we can assume that when a list is empty, it belongs only
  530. // to an element, and appending makes sense.
  531. fn diff_children(&mut self, old: &'bump [VNode<'bump>], new: &'bump [VNode<'bump>]) {
  532. // Remember, fragments can never be empty (they always have a single child)
  533. match (old, new) {
  534. ([], []) => {}
  535. ([], _) => {
  536. self.stack.create_children(new, MountType::Append);
  537. }
  538. (_, []) => {
  539. for node in old {
  540. self.remove_nodes(Some(node));
  541. }
  542. }
  543. ([VNode::Anchor(old_anchor)], [VNode::Anchor(new_anchor)]) => {
  544. old_anchor.dom_id.set(new_anchor.dom_id.get());
  545. }
  546. ([VNode::Anchor(anchor)], _) => {
  547. let el = anchor.dom_id.get();
  548. self.stack
  549. .create_children(new, MountType::ReplaceByElementId { el });
  550. }
  551. (_, [VNode::Anchor(_)]) => {
  552. self.replace_and_create_many_with_one(old, &new[0]);
  553. }
  554. _ => {
  555. let new_is_keyed = new[0].key().is_some();
  556. let old_is_keyed = old[0].key().is_some();
  557. debug_assert!(
  558. new.iter().all(|n| n.key().is_some() == new_is_keyed),
  559. "all siblings must be keyed or all siblings must be non-keyed"
  560. );
  561. debug_assert!(
  562. old.iter().all(|o| o.key().is_some() == old_is_keyed),
  563. "all siblings must be keyed or all siblings must be non-keyed"
  564. );
  565. if new_is_keyed && old_is_keyed {
  566. self.diff_keyed_children(old, new);
  567. } else {
  568. self.diff_non_keyed_children(old, new);
  569. }
  570. }
  571. }
  572. }
  573. // Diff children that are not keyed.
  574. //
  575. // The parent must be on the top of the change list stack when entering this
  576. // function:
  577. //
  578. // [... parent]
  579. //
  580. // the change list stack is in the same state when this function returns.
  581. fn diff_non_keyed_children(&mut self, old: &'bump [VNode<'bump>], new: &'bump [VNode<'bump>]) {
  582. // Handled these cases in `diff_children` before calling this function.
  583. log::debug!("diffing non-keyed case");
  584. debug_assert!(!new.is_empty());
  585. debug_assert!(!old.is_empty());
  586. for (new, old) in new.iter().zip(old.iter()).rev() {
  587. self.stack.push(DiffInstruction::Diff { new, old });
  588. }
  589. use std::cmp::Ordering;
  590. match old.len().cmp(&new.len()) {
  591. Ordering::Greater => self.remove_nodes(&old[new.len()..]),
  592. Ordering::Less => {
  593. self.stack.create_children(
  594. &new[old.len()..],
  595. MountType::InsertAfter {
  596. other_node: old.last().unwrap(),
  597. },
  598. );
  599. }
  600. Ordering::Equal => {
  601. // nothing - they're the same size
  602. }
  603. }
  604. }
  605. // Diffing "keyed" children.
  606. //
  607. // With keyed children, we care about whether we delete, move, or create nodes
  608. // versus mutate existing nodes in place. Presumably there is some sort of CSS
  609. // transition animation that makes the virtual DOM diffing algorithm
  610. // observable. By specifying keys for nodes, we know which virtual DOM nodes
  611. // must reuse (or not reuse) the same physical DOM nodes.
  612. //
  613. // This is loosely based on Inferno's keyed patching implementation. However, we
  614. // have to modify the algorithm since we are compiling the diff down into change
  615. // list instructions that will be executed later, rather than applying the
  616. // changes to the DOM directly as we compare virtual DOMs.
  617. //
  618. // https://github.com/infernojs/inferno/blob/36fd96/packages/inferno/src/DOM/patching.ts#L530-L739
  619. //
  620. // The stack is empty upon entry.
  621. fn diff_keyed_children(&mut self, old: &'bump [VNode<'bump>], new: &'bump [VNode<'bump>]) {
  622. if cfg!(debug_assertions) {
  623. let mut keys = fxhash::FxHashSet::default();
  624. let mut assert_unique_keys = |children: &'bump [VNode<'bump>]| {
  625. keys.clear();
  626. for child in children {
  627. let key = child.key();
  628. debug_assert!(
  629. key.is_some(),
  630. "if any sibling is keyed, all siblings must be keyed"
  631. );
  632. keys.insert(key);
  633. }
  634. debug_assert_eq!(
  635. children.len(),
  636. keys.len(),
  637. "keyed siblings must each have a unique key"
  638. );
  639. };
  640. assert_unique_keys(old);
  641. assert_unique_keys(new);
  642. }
  643. // First up, we diff all the nodes with the same key at the beginning of the
  644. // children.
  645. //
  646. // `shared_prefix_count` is the count of how many nodes at the start of
  647. // `new` and `old` share the same keys.
  648. let (left_offset, right_offset) = match self.diff_keyed_ends(old, new) {
  649. Some(count) => count,
  650. None => return,
  651. };
  652. log::debug!(
  653. "Left offset, right offset, {}, {}",
  654. left_offset,
  655. right_offset,
  656. );
  657. // Ok, we now hopefully have a smaller range of children in the middle
  658. // within which to re-order nodes with the same keys, remove old nodes with
  659. // now-unused keys, and create new nodes with fresh keys.
  660. self.diff_keyed_middle(
  661. &old[left_offset..(old.len() - right_offset)],
  662. &new[left_offset..(new.len() - right_offset)],
  663. );
  664. }
  665. /// Diff both ends of the children that share keys.
  666. ///
  667. /// Returns a left offset and right offset of that indicates a smaller section to pass onto the middle diffing.
  668. ///
  669. /// If there is no offset, then this function returns None and the diffing is complete.
  670. fn diff_keyed_ends(
  671. &mut self,
  672. old: &'bump [VNode<'bump>],
  673. new: &'bump [VNode<'bump>],
  674. ) -> Option<(usize, usize)> {
  675. let mut left_offset = 0;
  676. for (old, new) in old.iter().zip(new.iter()) {
  677. // abort early if we finally run into nodes with different keys
  678. if old.key() != new.key() {
  679. break;
  680. }
  681. self.stack.push(DiffInstruction::Diff { old, new });
  682. left_offset += 1;
  683. }
  684. // If that was all of the old children, then create and append the remaining
  685. // new children and we're finished.
  686. if left_offset == old.len() {
  687. self.stack.create_children(
  688. &new[left_offset..],
  689. MountType::InsertAfter {
  690. other_node: old.last().unwrap(),
  691. },
  692. );
  693. return None;
  694. }
  695. // And if that was all of the new children, then remove all of the remaining
  696. // old children and we're finished.
  697. if left_offset == new.len() {
  698. self.remove_nodes(&old[left_offset..]);
  699. return None;
  700. }
  701. // if the shared prefix is less than either length, then we need to walk backwards
  702. let mut right_offset = 0;
  703. for (old, new) in old.iter().rev().zip(new.iter().rev()) {
  704. // abort early if we finally run into nodes with different keys
  705. if old.key() != new.key() {
  706. break;
  707. }
  708. self.diff_node(old, new);
  709. right_offset += 1;
  710. }
  711. Some((left_offset, right_offset))
  712. }
  713. // The most-general, expensive code path for keyed children diffing.
  714. //
  715. // We find the longest subsequence within `old` of children that are relatively
  716. // ordered the same way in `new` (via finding a longest-increasing-subsequence
  717. // of the old child's index within `new`). The children that are elements of
  718. // this subsequence will remain in place, minimizing the number of DOM moves we
  719. // will have to do.
  720. //
  721. // Upon entry to this function, the change list stack must be empty.
  722. //
  723. // This function will load the appropriate nodes onto the stack and do diffing in place.
  724. //
  725. // Upon exit from this function, it will be restored to that same state.
  726. fn diff_keyed_middle(&mut self, old: &'bump [VNode<'bump>], new: &'bump [VNode<'bump>]) {
  727. /*
  728. 1. Map the old keys into a numerical ordering based on indicies.
  729. 2. Create a map of old key to its index
  730. 3. Map each new key to the old key, carrying over the old index.
  731. - IE if we have ABCD becomes BACD, our sequence would be 1,0,2,3
  732. - if we have ABCD to ABDE, our sequence would be 0,1,3,MAX because E doesn't exist
  733. now, we should have a list of integers that indicates where in the old list the new items map to.
  734. 4. Compute the LIS of this list
  735. - this indicates the longest list of new children that won't need to be moved.
  736. 5. Identify which nodes need to be removed
  737. 6. Identify which nodes will need to be diffed
  738. 7. Going along each item in the new list, create it and insert it before the next closest item in the LIS.
  739. - if the item already existed, just move it to the right place.
  740. 8. Finally, generate instructions to remove any old children.
  741. 9. Generate instructions to finally diff children that are the same between both
  742. */
  743. // 0. Debug sanity checks
  744. // Should have already diffed the shared-key prefixes and suffixes.
  745. debug_assert_ne!(new.first().map(|n| n.key()), old.first().map(|o| o.key()));
  746. debug_assert_ne!(new.last().map(|n| n.key()), old.last().map(|o| o.key()));
  747. // 1. Map the old keys into a numerical ordering based on indicies.
  748. // 2. Create a map of old key to its index
  749. // IE if the keys were A B C, then we would have (A, 1) (B, 2) (C, 3).
  750. let old_key_to_old_index = old
  751. .iter()
  752. .enumerate()
  753. .map(|(i, o)| (o.key().unwrap(), i))
  754. .collect::<FxHashMap<_, _>>();
  755. let mut shared_keys = FxHashSet::default();
  756. // 3. Map each new key to the old key, carrying over the old index.
  757. let new_index_to_old_index = new
  758. .iter()
  759. .map(|node| {
  760. let key = node.key().unwrap();
  761. if let Some(&index) = old_key_to_old_index.get(&key) {
  762. shared_keys.insert(key);
  763. index
  764. } else {
  765. u32::MAX as usize
  766. }
  767. })
  768. .collect::<Vec<_>>();
  769. // If none of the old keys are reused by the new children, then we remove all the remaining old children and
  770. // create the new children afresh.
  771. if shared_keys.is_empty() {
  772. self.replace_and_create_many_with_many(old, new);
  773. return;
  774. }
  775. // 4. Compute the LIS of this list
  776. let mut lis_sequence = Vec::default();
  777. lis_sequence.reserve(new_index_to_old_index.len());
  778. let mut predecessors = vec![0; new_index_to_old_index.len()];
  779. let mut starts = vec![0; new_index_to_old_index.len()];
  780. longest_increasing_subsequence::lis_with(
  781. &new_index_to_old_index,
  782. &mut lis_sequence,
  783. |a, b| a < b,
  784. &mut predecessors,
  785. &mut starts,
  786. );
  787. // the lis comes out backwards, I think. can't quite tell.
  788. lis_sequence.sort_unstable();
  789. // if a new node gets u32 max and is at the end, then it might be part of our LIS (because u32 max is a valid LIS)
  790. if lis_sequence.last().map(|f| new_index_to_old_index[*f]) == Some(u32::MAX as usize) {
  791. lis_sequence.pop();
  792. }
  793. let apply = |new_idx, new_node: &'bump VNode<'bump>, stack: &mut DiffStack<'bump>| {
  794. let old_index = new_index_to_old_index[new_idx];
  795. if old_index == u32::MAX as usize {
  796. stack.create_node(new_node, MountType::Absorb);
  797. } else {
  798. // this funciton should never take LIS indicies
  799. stack.push(DiffInstruction::PrepareMove { node: new_node });
  800. stack.push(DiffInstruction::Diff {
  801. new: new_node,
  802. old: &old[old_index],
  803. });
  804. }
  805. };
  806. // add mount instruction for the last items not covered by the lis
  807. let first_lis = *lis_sequence.first().unwrap();
  808. if first_lis > 0 {
  809. self.stack.push_nodes_created(0);
  810. self.stack.push(DiffInstruction::Mount {
  811. and: MountType::InsertBefore {
  812. other_node: &new[first_lis],
  813. },
  814. });
  815. for (idx, new_node) in new[..first_lis].iter().enumerate().rev() {
  816. apply(idx, new_node, &mut self.stack);
  817. }
  818. }
  819. // for each spacing, generate a mount instruction
  820. let mut lis_iter = lis_sequence.iter().rev();
  821. let mut last = *lis_iter.next().unwrap();
  822. for next in lis_iter {
  823. if last - next > 1 {
  824. self.stack.push_nodes_created(0);
  825. self.stack.push(DiffInstruction::Mount {
  826. and: MountType::InsertBefore {
  827. other_node: &new[last],
  828. },
  829. });
  830. for (idx, new_node) in new[(next + 1)..last].iter().enumerate().rev() {
  831. apply(idx + next + 1, new_node, &mut self.stack);
  832. }
  833. }
  834. last = *next;
  835. }
  836. // add mount instruction for the first items not covered by the lis
  837. let last = *lis_sequence.last().unwrap();
  838. if last < (new.len() - 1) {
  839. self.stack.push_nodes_created(0);
  840. self.stack.push(DiffInstruction::Mount {
  841. and: MountType::InsertAfter {
  842. other_node: &new[last],
  843. },
  844. });
  845. for (idx, new_node) in new[(last + 1)..].iter().enumerate().rev() {
  846. apply(idx + last + 1, new_node, &mut self.stack);
  847. }
  848. }
  849. for idx in lis_sequence.iter().rev() {
  850. self.stack.push(DiffInstruction::Diff {
  851. new: &new[*idx],
  852. old: &old[new_index_to_old_index[*idx]],
  853. });
  854. }
  855. }
  856. // =====================
  857. // Utilities
  858. // =====================
  859. fn find_last_element(&mut self, vnode: &'bump VNode<'bump>) -> Option<ElementId> {
  860. let mut search_node = Some(vnode);
  861. loop {
  862. match &search_node.take().unwrap() {
  863. VNode::Text(t) => break t.dom_id.get(),
  864. VNode::Element(t) => break t.dom_id.get(),
  865. VNode::Suspended(t) => break t.dom_id.get(),
  866. VNode::Anchor(t) => break t.dom_id.get(),
  867. VNode::Fragment(frag) => {
  868. search_node = frag.children.last();
  869. }
  870. VNode::Component(el) => {
  871. let scope_id = el.associated_scope.get().unwrap();
  872. let scope = self.vdom.get_scope(scope_id).unwrap();
  873. search_node = Some(scope.root_node());
  874. }
  875. }
  876. }
  877. }
  878. fn find_first_element_id(&mut self, vnode: &'bump VNode<'bump>) -> Option<ElementId> {
  879. let mut search_node = Some(vnode);
  880. loop {
  881. match &search_node.take().unwrap() {
  882. // the ones that have a direct id
  883. VNode::Fragment(frag) => {
  884. search_node = Some(&frag.children[0]);
  885. }
  886. VNode::Component(el) => {
  887. let scope_id = el.associated_scope.get().unwrap();
  888. let scope = self.vdom.get_scope(scope_id).unwrap();
  889. search_node = Some(scope.root_node());
  890. }
  891. VNode::Text(t) => break t.dom_id.get(),
  892. VNode::Element(t) => break t.dom_id.get(),
  893. VNode::Suspended(t) => break t.dom_id.get(),
  894. VNode::Anchor(t) => break t.dom_id.get(),
  895. }
  896. }
  897. }
  898. fn replace_and_create_one_with_many(
  899. &mut self,
  900. old: &'bump VNode<'bump>,
  901. new: &'bump [VNode<'bump>],
  902. ) {
  903. //
  904. }
  905. fn replace_and_create_many_with_one(
  906. &mut self,
  907. old: &'bump [VNode<'bump>],
  908. new: &'bump VNode<'bump>,
  909. ) {
  910. if let Some(first_old) = old.get(0) {
  911. self.remove_nodes(&old[1..]);
  912. self.stack
  913. .create_node(new, MountType::Replace { old: first_old });
  914. } else {
  915. self.stack.create_node(new, MountType::Append {});
  916. }
  917. }
  918. /// schedules nodes for garbage collection and pushes "remove" to the mutation stack
  919. /// remove can happen whenever
  920. fn remove_nodes(&mut self, nodes: impl IntoIterator<Item = &'bump VNode<'bump>>) {
  921. // or cache the vec on the diff machine
  922. for node in nodes {
  923. match node {
  924. VNode::Text(t) => {
  925. if let Some(id) = t.dom_id.get() {
  926. self.mutations.remove(id.as_u64());
  927. self.vdom.collect_garbage(id);
  928. }
  929. }
  930. VNode::Suspended(s) => {
  931. if let Some(id) = s.dom_id.get() {
  932. self.mutations.remove(id.as_u64());
  933. self.vdom.collect_garbage(id);
  934. }
  935. }
  936. VNode::Anchor(a) => {
  937. if let Some(id) = a.dom_id.get() {
  938. self.mutations.remove(id.as_u64());
  939. self.vdom.collect_garbage(id);
  940. }
  941. }
  942. VNode::Element(e) => {
  943. if let Some(id) = e.dom_id.get() {
  944. self.mutations.remove(id.as_u64());
  945. }
  946. }
  947. VNode::Fragment(f) => {
  948. self.remove_nodes(f.children);
  949. }
  950. VNode::Component(c) => {
  951. let scope_id = c.associated_scope.get().unwrap();
  952. let scope = self.vdom.get_scope(scope_id).unwrap();
  953. let root = scope.root_node();
  954. self.remove_nodes(Some(root));
  955. }
  956. }
  957. }
  958. }
  959. /// Remove all the old nodes and replace them with newly created new nodes.
  960. ///
  961. /// The new nodes *will* be created - don't create them yourself!
  962. fn replace_and_create_many_with_many(
  963. &mut self,
  964. old: &'bump [VNode<'bump>],
  965. new: &'bump [VNode<'bump>],
  966. ) {
  967. if let Some(first_old) = old.get(0) {
  968. self.remove_nodes(&old[1..]);
  969. self.stack
  970. .create_children(new, MountType::Replace { old: first_old })
  971. } else {
  972. self.stack.create_children(new, MountType::Append {});
  973. }
  974. }
  975. /// Adds a listener closure to a scope during diff.
  976. fn attach_listener_to_scope<'a>(&mut self, listener: &'a Listener<'a>, scope: &Scope) {
  977. let mut queue = scope.listeners.borrow_mut();
  978. let long_listener: &'a Listener<'static> = unsafe { std::mem::transmute(listener) };
  979. queue.push(long_listener as *const _)
  980. }
  981. fn attach_suspended_node_to_scope(&mut self, suspended: &'bump VSuspended) {
  982. if let Some(scope) = self
  983. .stack
  984. .current_scope()
  985. .and_then(|id| self.vdom.get_scope_mut(id))
  986. {
  987. // safety: this lifetime is managed by the logic on scope
  988. let extended: &VSuspended<'static> = unsafe { std::mem::transmute(suspended) };
  989. scope
  990. .suspended_nodes
  991. .borrow_mut()
  992. .insert(suspended.task_id, extended as *const _);
  993. }
  994. }
  995. }
  996. fn compare_strs(a: &str, b: &str) -> bool {
  997. // Check by pointer, optimizing for static strs
  998. if !std::ptr::eq(a, b) {
  999. // If the pointers are different then check by value
  1000. a == b
  1001. } else {
  1002. true
  1003. }
  1004. }