feat(core): memoize components by value of Element props - #5756
Open
rexlunae wants to merge 1 commit into
Open
Conversation
When a component re-renders and passes an Element to a child component, the Element is rebuilt from scratch, so pointer equality always fails and the child re-renders even when its props are unchanged. Make VNode's PartialEq compare by value instead of by allocation identity: the template is compared by its content hash, dynamic attributes by value, and dynamic nodes structurally (text, placeholders, fragments, and components — component props are compared with the same type-erased equality used for component memoization). The diff fast path intentionally keeps pointer equality (Rc::ptr_eq), since the diff algorithm walks the tree structurally and a value comparison there would be wasted work. Closes DioxusLabs#1929
Author
|
cc @ealmloff @jkelleyrtp — this makes Element props memoizable by value (issue #1929). I kept the diff fast path on pointer equality so the renderer's hot path is untouched. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1929
Problem
When a component re-renders and passes an
Element(e.g.children) to a child component, theElementis rebuilt from scratch on every render.VNode'sPartialEqwas pointer-based (Rc::ptr_eq), so the new allocation never compared equal to the old one — the child component re-rendered even when its props were completely unchanged.Change
VNode'sPartialEqnow compares by value:diff/node.rskeeps pointer equality (VNode::ptr_eq), since the diff algorithm walks the tree structurally and a value comparison there would be wasted work.FragmentPropsdoc comment is updated: children are now memoized by value, so borrowing nodes from a parent no longer defeats memoization.Tests
tests/memoize_element_props.rs: a child component with unchangedchildrendoes not re-render across parent re-renders; it does re-render when the children's content changes.tests/diff_dynamic_node.rs(toggle_template, regression for bug: Panic on diff/node.rs #2815): updated — the first phase (parent re-render) now correctly produces no edits because the child is memoized; the placeholder/text toggling is still covered by driving the child scope directly.All
dioxus-coretests pass (including the fuzzing and kitchen-sink suites); clippy and fmt clean.