From 9c3f8e11be92288767892ccf43f5f389482ee586 Mon Sep 17 00:00:00 2001 From: Kelly Thomas Kline Date: Tue, 8 Oct 2019 20:36:34 -0700 Subject: [PATCH 1/2] Optimize the default `change` implementation Return `false` if `Self::Properties` has a value of `()`. --- src/html/mod.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/html/mod.rs b/src/html/mod.rs index 76f67b91bfa..6362063860a 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -38,9 +38,14 @@ pub trait Component: Sized + 'static { /// component's place in the DOM tree remains unchanged. If the component's /// place in the DOM tree changes, calling this method is unnecessary as the /// component is recreated from scratch. It defaults - /// to true if not implemented. - fn change(&mut self, _: Self::Properties) -> ShouldRender { - true + /// to true if not implemented and Self::Properties doesn't have a () value. + fn change(&mut self, props: Self::Properties) -> ShouldRender { + if props == () { + false + } + else { + true + } } /// Called by rendering loop. fn view(&self) -> Html; From 49a5f2c2e0fb72b10ac35cd98394a911fb9c1acb Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Sun, 13 Oct 2019 15:47:22 -0400 Subject: [PATCH 2/2] Use TypeId for checking Properties == () --- src/html/mod.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/html/mod.rs b/src/html/mod.rs index 6362063860a..a0ced9322df 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -12,6 +12,7 @@ pub use scope::{NodeCell, Scope}; use crate::callback::Callback; use crate::virtual_dom::{VChild, VList, VNode}; +use std::any::TypeId; use std::fmt; /// This type indicates that component should be rendered again. @@ -37,15 +38,10 @@ pub trait Component: Sized + 'static { /// Called when the component's parent component re-renders and the /// component's place in the DOM tree remains unchanged. If the component's /// place in the DOM tree changes, calling this method is unnecessary as the - /// component is recreated from scratch. It defaults - /// to true if not implemented and Self::Properties doesn't have a () value. - fn change(&mut self, props: Self::Properties) -> ShouldRender { - if props == () { - false - } - else { - true - } + /// component is recreated from scratch. It defaults to true if not implemented + /// and Self::Properties is not the unit type `()`. + fn change(&mut self, _props: Self::Properties) -> ShouldRender { + TypeId::of::() != TypeId::of::<()>() } /// Called by rendering loop. fn view(&self) -> Html;