Skip to content

Commit

Permalink
Faster clone for list and portal
Browse files Browse the repository at this point in the history
  • Loading branch information
cecton committed Sep 30, 2023
1 parent fd1187f commit df2d8ef
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 26 deletions.
4 changes: 1 addition & 3 deletions packages/yew-macro/src/html_tree/html_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,7 @@ impl ToTokens for HtmlElement {
// e.g. html!{<@{"div"}/>} will set `#expr` to `{"div"}`
// (note the extra braces). Hence the need for the `allow`.
// Anyways to remove the braces?
let mut #vtag_name = ::std::convert::Into::<
::std::borrow::Cow::<'static, ::std::primitive::str>
>::into(#expr);
let mut #vtag_name = ::yew::virtual_dom::AttrValue::from(#expr);
::std::debug_assert!(
#vtag_name.is_ascii(),
"a dynamic tag returned a tag name containing non ASCII characters: `{}`",
Expand Down
4 changes: 2 additions & 2 deletions packages/yew-macro/src/html_tree/html_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ impl ToTokens for HtmlList {
};

tokens.extend(quote_spanned! {spanned.span()=>
::yew::virtual_dom::VNode::VList(
::yew::virtual_dom::VNode::VList(::std::rc::Rc::new(
::yew::virtual_dom::VList::with_children(#children, #key)
)
))
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/yew-macro/src/html_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl ToTokens for HtmlTree {
lint::lint_all(self);
match self {
HtmlTree::Empty => tokens.extend(quote! {
::yew::virtual_dom::VNode::VList(::yew::virtual_dom::VList::new())
::yew::virtual_dom::VNode::default()
}),
HtmlTree::Component(comp) => comp.to_tokens(tokens),
HtmlTree::Element(tag) => tag.to_tokens(tokens),
Expand Down
3 changes: 2 additions & 1 deletion packages/yew/src/html/component/children.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Component children module

use std::fmt;
use std::rc::Rc;

use crate::html::Html;
use crate::virtual_dom::{VChild, VComp, VList, VNode};
Expand Down Expand Up @@ -241,7 +242,7 @@ impl From<ChildrenRenderer<Html>> for Html {
}
}

Html::VList(val.into())
Html::VList(Rc::new(val.into()))
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/yew/src/html/conversion/to_html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ where
{
#[inline(always)]
fn to_html(&self) -> Html {
Html::VList(VList::with_children(
Html::VList(Rc::new(VList::with_children(
self.iter().map(ToHtml::to_html).collect(),
None,
))
)))
}

#[inline(always)]
fn into_html(self) -> Html {
Html::VList(VList::with_children(
Html::VList(Rc::new(VList::with_children(
self.into_iter().map(ToHtml::into_html).collect(),
None,
))
)))
}
}

Expand All @@ -81,7 +81,7 @@ impl ToHtml for Vec<VNode> {

#[inline(always)]
fn into_html(self) -> Html {
Html::VList(VList::with_children(self, None))
Html::VList(Rc::new(VList::with_children(self, None)))
}
}

Expand All @@ -105,7 +105,7 @@ impl ToHtml for VList {

#[inline(always)]
fn into_html(self) -> Html {
Html::VList(self)
Html::VList(Rc::new(self))
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ mod feat_csr {
/// ## Relevant examples
/// - [Portals](https://github.com/yewstack/yew/tree/master/examples/portals)
pub fn create_portal(child: Html, host: Element) -> Html {
VNode::VPortal(VPortal::new(child, host))
VNode::VPortal(Rc::new(VPortal::new(child, host)))
}
19 changes: 10 additions & 9 deletions packages/yew/src/virtual_dom/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ pub enum VNode {
/// A bind between `VComp` and `Element`.
VComp(Rc<VComp>),
/// A holder for a list of other nodes.
VList(VList),
VList(Rc<VList>),
/// A portal to another part of the document
VPortal(VPortal),
VPortal(Rc<VPortal>),
/// A holder for any `Node` (necessary for replacing node).
VRef(Node),
/// A suspendible document fragment.
Expand Down Expand Up @@ -63,9 +63,10 @@ impl VNode {
pub fn to_vlist_mut(&mut self) -> &mut VList {
loop {
match *self {
Self::VList(ref mut m) => return m,
Self::VList(ref mut m) => return Rc::make_mut(m),
_ => {
*self = VNode::VList(VList::with_children(vec![mem::take(self)], None));
*self =
VNode::VList(Rc::new(VList::with_children(vec![mem::take(self)], None)));
}
}
}
Expand Down Expand Up @@ -108,7 +109,7 @@ impl VNode {

impl Default for VNode {
fn default() -> Self {
VNode::VList(VList::default())
VNode::VList(Rc::new(VList::default()))
}
}

Expand All @@ -122,7 +123,7 @@ impl From<VText> for VNode {
impl From<VList> for VNode {
#[inline]
fn from(vlist: VList) -> Self {
VNode::VList(vlist)
VNode::VList(Rc::new(vlist))
}
}

Expand Down Expand Up @@ -150,7 +151,7 @@ impl From<VSuspense> for VNode {
impl From<VPortal> for VNode {
#[inline]
fn from(vportal: VPortal) -> Self {
VNode::VPortal(vportal)
VNode::VPortal(Rc::new(vportal))
}
}

Expand All @@ -171,10 +172,10 @@ impl<T: ToString> From<T> for VNode {

impl<A: Into<VNode>> FromIterator<A> for VNode {
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
VNode::VList(VList::with_children(
VNode::VList(Rc::new(VList::with_children(
iter.into_iter().map(|n| n.into()).collect(),
None,
))
)))
}
}

Expand Down
8 changes: 5 additions & 3 deletions packages/yew/src/virtual_dom/vportal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! This module contains the implementation of a portal `VPortal`.

use std::rc::Rc;

use web_sys::{Element, Node};

use super::VNode;
Expand All @@ -12,7 +14,7 @@ pub struct VPortal {
/// The next sibling after the inserted content. Must be a child of `host`.
pub inner_sibling: Option<Node>,
/// The inserted node
pub node: Box<VNode>,
pub node: Rc<VNode>,
}

impl ImplicitClone for VPortal {}
Expand All @@ -23,7 +25,7 @@ impl VPortal {
Self {
host,
inner_sibling: None,
node: Box::new(content),
node: Rc::new(content),
}
}

Expand All @@ -34,7 +36,7 @@ impl VPortal {
Self {
host,
inner_sibling,
node: Box::new(content),
node: Rc::new(content),
}
}
}

0 comments on commit df2d8ef

Please sign in to comment.