perf: add template cache for InertElement (#4099)

This commit is contained in:
bicarlsen
2025-06-20 23:01:19 +02:00
committed by GitHub
parent 5227221c96
commit 911be5007e
2 changed files with 19 additions and 7 deletions

View File

@@ -125,14 +125,14 @@ impl Render for InertElement {
type State = InertElementState;
fn build(self) -> Self::State {
let el = Rndr::create_element_from_html(&self.html);
let el = Rndr::create_element_from_html(self.html.clone());
InertElementState(self.html, el)
}
fn rebuild(self, state: &mut Self::State) {
let InertElementState(prev, el) = state;
if &self.html != prev {
let mut new_el = Rndr::create_element_from_html(&self.html);
let mut new_el = Rndr::create_element_from_html(self.html.clone());
el.insert_before_this(&mut new_el);
el.unmount();
*el = new_el;

View File

@@ -24,6 +24,7 @@ pub struct Dom;
thread_local! {
pub(crate) static GLOBAL_EVENTS: RefCell<FxHashSet<Cow<'static, str>>> = Default::default();
pub static TEMPLATE_CACHE: RefCell<Vec<(Cow<'static, str>, web_sys::Element)>> = Default::default();
}
pub type Node = web_sys::Node;
@@ -491,11 +492,22 @@ impl Dom {
.unchecked_into()
}
pub fn create_element_from_html(html: &str) -> Element {
// TODO can be optimized to cache HTML strings or cache <template>?
let tpl = document().create_element("template").unwrap();
tpl.set_inner_html(html);
let tpl = Self::clone_template(tpl.unchecked_ref());
pub fn create_element_from_html(html: Cow<'static, str>) -> Element {
let tpl = TEMPLATE_CACHE.with(|cache| {
let mut cache = cache.borrow_mut();
if let Some(tpl) = cache.iter().find_map(|(key, tpl)| {
(html == *key)
.then_some(Self::clone_template(tpl.unchecked_ref()))
}) {
tpl
} else {
let tpl = document().create_element("template").unwrap();
tpl.set_inner_html(&html);
let tpl2 = Self::clone_template(tpl.unchecked_ref());
cache.push((html, tpl));
tpl2
}
});
tpl.first_element_child().unwrap_or(tpl)
}
}