tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Definitions

Remove style clone in grid algorithm

f54bd0a26cadf41681090aabeb0b088c10091b7b · by Nico Burns · (2 years ago) · parent 7b6efafe4 · browse the tree at this commit

Semantic delta · 4

src/compute/grid/mod.rs

src/tree/taffy_tree.rs

src/tree/traits.rs

3 files · +21 −13

diff --git a/src/compute/grid/mod.rs b/src/compute/grid/mod.rs
index eec788bab..8ad211865 100644
--- a/src/compute/grid/mod.rs
+++ b/src/compute/grid/mod.rs
@@ -40,7 +40,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
 
     let get_child_styles_iter =
         |node| tree.child_ids(node).map(|child_node: NodeId| tree.get_grid_child_style(child_node));
-    let style = tree.get_grid_container_style(node).clone();
+    let style = tree.get_grid_container_style(node);
     let child_styles_iter = get_child_styles_iter(node);
 
     let preferred_size = if inputs.sizing_mode == SizingMode::InherentSize {
@@ -130,6 +130,13 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
     content_box_inset.right += scrollbar_gutter.x;
     content_box_inset.bottom += scrollbar_gutter.y;
 
+    let align_content = style.align_content().unwrap_or(AlignContent::Stretch);
+    let align_items = style.align_items();
+    let justify_content = style.justify_content().unwrap_or(AlignContent::Stretch);
+    let justify_items = style.justify_items();
+
+    drop(style);
+
     let constrained_available_space = known_dimensions
         .or(size)
         .map(|size| size.map(AvailableSpace::Definite))
@@ -176,7 +183,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
         AbstractAxis::Inline,
         min_size.get(AbstractAxis::Inline),
         max_size.get(AbstractAxis::Inline),
-        style.grid_align_content(AbstractAxis::Block),
+        align_content,
         available_grid_space,
         inner_node_size,
         &mut columns,
@@ -196,7 +203,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
         AbstractAxis::Block,
         min_size.get(AbstractAxis::Block),
         max_size.get(AbstractAxis::Block),
-        style.grid_align_content(AbstractAxis::Inline),
+        justify_content,
         available_grid_space,
         inner_node_size,
         &mut rows,
@@ -309,7 +316,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
             AbstractAxis::Inline,
             min_size.get(AbstractAxis::Inline),
             max_size.get(AbstractAxis::Inline),
-            style.grid_align_content(AbstractAxis::Block),
+            align_content,
             available_grid_space,
             inner_node_size,
             &mut columns,
@@ -371,7 +378,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
                 AbstractAxis::Block,
                 min_size.get(AbstractAxis::Block),
                 max_size.get(AbstractAxis::Block),
-                style.grid_align_content(AbstractAxis::Inline),
+                justify_content,
                 available_grid_space,
                 inner_node_size,
                 &mut rows,
@@ -391,7 +398,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
         Line { start: padding.left, end: padding.right },
         Line { start: border.left, end: border.right },
         &mut columns,
-        style.justify_content().unwrap_or(AlignContent::Stretch),
+        justify_content,
     );
     // Align rows
     align_tracks(
@@ -399,7 +406,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
         Line { start: padding.top, end: padding.bottom },
         Line { start: border.top, end: border.bottom },
         &mut rows,
-        style.align_content().unwrap_or(AlignContent::Stretch),
+        align_content,
     );
 
     // 9. Size, Align, and Position Grid Items
@@ -410,8 +417,7 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
     // Sort items back into original order to allow them to be matched up with styles
     items.sort_by_key(|item| item.source_order);
 
-    let container_alignment_styles = InBothAbsAxis { horizontal: style.justify_items(), vertical: style.align_items() };
-    drop(style);
+    let container_alignment_styles = InBothAbsAxis { horizontal: justify_items, vertical: align_items };
 
     // Position in-flow children (stored in items vector)
     for (index, item) in items.iter_mut().enumerate() {
diff --git a/src/tree/taffy_tree.rs b/src/tree/taffy_tree.rs
index 72898bf09..7af6ae790 100644
--- a/src/tree/taffy_tree.rs
+++ b/src/tree/taffy_tree.rs
@@ -376,11 +376,11 @@ impl<'t, NodeContext, MeasureFunction> LayoutGridContainer for TaffyView<'t, Nod
 where
     MeasureFunction: FnMut(Size<Option<f32>>, Size<AvailableSpace>, NodeId, Option<&mut NodeContext>) -> Size<f32>,
 {
-    type ContainerStyle = Style;
+    type ContainerStyle<'a> = &'a Style where Self: 'a;
     type ItemStyle<'a> = &'a Style where Self: 'a;
 
     #[inline(always)]
-    fn get_grid_container_style(&self, node_id: NodeId) -> &Self::ContainerStyle {
+    fn get_grid_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_> {
         &self.taffy.nodes[node_id.into()].style
     }
 
diff --git a/src/tree/traits.rs b/src/tree/traits.rs
index 02324dbf9..f3e2be7ee 100644
--- a/src/tree/traits.rs
+++ b/src/tree/traits.rs
@@ -224,7 +224,9 @@ pub trait LayoutFlexboxContainer: LayoutPartialTree {
 /// Extends [`LayoutPartialTree`] with getters for the styles required for CSS Grid layout
 pub trait LayoutGridContainer: LayoutPartialTree {
     /// The style type representing the CSS Grid container's styles
-    type ContainerStyle: GridContainerStyle + Clone;
+    type ContainerStyle<'a>: GridContainerStyle
+    where
+        Self: 'a;
 
     /// The style type representing each CSS Grid item's styles
     type ItemStyle<'a>: GridItemStyle
@@ -232,7 +234,7 @@ pub trait LayoutGridContainer: LayoutPartialTree {
         Self: 'a;
 
     /// Get the container's styles
-    fn get_grid_container_style(&self, node_id: NodeId) -> &Self::ContainerStyle;
+    fn get_grid_container_style(&self, node_id: NodeId) -> Self::ContainerStyle<'_>;
 
     /// Get the child's styles
     fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::ItemStyle<'_>;