Skip to content

Commit

Permalink
Remove style clone in grid algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed May 14, 2024
1 parent bd9c9ca commit a76a81b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
24 changes: 15 additions & 9 deletions src/compute/grid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -391,15 +398,15 @@ 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(
container_content_box.get(AbstractAxis::Block),
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
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/tree/taffy_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,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
}

Expand Down
6 changes: 4 additions & 2 deletions src/tree/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,17 @@ 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
where
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<'_>;
Expand Down

0 comments on commit a76a81b

Please sign in to comment.