From 23c48cf7959037a4ae94b237f036329ddfd92c75 Mon Sep 17 00:00:00 2001 From: Demonthos Date: Sun, 9 Oct 2022 01:24:41 -0500 Subject: [PATCH] render optimizations (#585) --- packages/core-macro/src/lib.rs | 11 +++++++++-- packages/rsx/src/lib.rs | 31 ++++++++++++++++++------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/packages/core-macro/src/lib.rs b/packages/core-macro/src/lib.rs index c3effe36b..de0155e14 100644 --- a/packages/core-macro/src/lib.rs +++ b/packages/core-macro/src/lib.rs @@ -65,8 +65,15 @@ pub fn rsx_without_templates(s: TokenStream) -> TokenStream { pub fn render(s: TokenStream) -> TokenStream { match syn::parse::(s) { Err(err) => err.to_compile_error().into(), - Ok(body) => quote::quote! { - cx.render(#body) + Ok(body) => { + let mut inner = proc_macro2::TokenStream::new(); + body.to_tokens_without_lazynodes(&mut inner); + quote::quote! { + { + let __cx = NodeFactory::new(&cx.scope); + Some(#inner) + } + } } .into_token_stream() .into(), diff --git a/packages/rsx/src/lib.rs b/packages/rsx/src/lib.rs index 83cf26cd2..d3c6c9fa4 100644 --- a/packages/rsx/src/lib.rs +++ b/packages/rsx/src/lib.rs @@ -67,20 +67,9 @@ impl Parse for CallBody { /// Serialize the same way, regardless of flavor impl ToTokens for CallBody { fn to_tokens(&self, out_tokens: &mut TokenStream2) { - let template = TemplateBuilder::from_roots(self.roots.clone()); - let inner = if let Some(template) = template { - quote! { #template } - } else { - let children = &self.roots; - if children.len() == 1 { - let inner = &self.roots[0]; - quote! { #inner } - } else { - quote! { __cx.fragment_root([ #(#children),* ]) } - } - }; + let mut inner = TokenStream2::new(); + self.to_tokens_without_lazynodes(&mut inner); - // Otherwise we just build the LazyNode wrapper out_tokens.append_all(quote! { LazyNodes::new(move |__cx: NodeFactory| -> VNode { use dioxus_elements::{GlobalAttributes, SvgAttributes}; @@ -107,4 +96,20 @@ impl CallBody { }) }) } + + pub fn to_tokens_without_lazynodes(&self, out_tokens: &mut TokenStream2) { + let template = TemplateBuilder::from_roots(self.roots.clone()); + let inner = if let Some(template) = template { + quote! { #template } + } else { + let children = &self.roots; + if children.len() == 1 { + let inner = &self.roots[0]; + quote! { #inner } + } else { + quote! { __cx.fragment_root([ #(#children),* ]) } + } + }; + out_tokens.append_all(inner); + } }