From 741022b2b29617a4cf64914a177993dc8e900737 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 2 Jan 2025 15:24:16 +0100 Subject: [PATCH] Put `Shape::Mesh` in an `Arc` to reduce the size of `Shape` --- crates/epaint/src/shape_transform.rs | 14 ++++++++------ crates/epaint/src/shapes/shape.rs | 22 ++++++++++++++++++---- crates/epaint/src/tessellator.rs | 4 ++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/crates/epaint/src/shape_transform.rs b/crates/epaint/src/shape_transform.rs index 5ff087d926a..25ff0d4699b 100644 --- a/crates/epaint/src/shape_transform.rs +++ b/crates/epaint/src/shape_transform.rs @@ -86,7 +86,7 @@ pub fn adjust_colors( } if !galley.is_empty() { - let galley = std::sync::Arc::make_mut(galley); + let galley = Arc::make_mut(galley); for row in &mut galley.rows { for vertex in &mut row.visuals.mesh.vertices { adjust_color(&mut vertex.color); @@ -95,11 +95,13 @@ pub fn adjust_colors( } } - Shape::Mesh(Mesh { - indices: _, - vertices, - texture_id: _, - }) => { + Shape::Mesh(mesh) => { + let Mesh { + indices: _, + vertices, + texture_id: _, + } = Arc::make_mut(mesh); + for v in vertices { adjust_color(&mut v.color); } diff --git a/crates/epaint/src/shapes/shape.rs b/crates/epaint/src/shapes/shape.rs index 93fff9e06d1..62af1b58166 100644 --- a/crates/epaint/src/shapes/shape.rs +++ b/crates/epaint/src/shapes/shape.rs @@ -56,7 +56,9 @@ pub enum Shape { /// A general triangle mesh. /// /// Can be used to display images. - Mesh(Mesh), + /// + /// Wrapped in an [`Arc`] to minimize the size of [`Shape`]. + Mesh(Arc), /// A quadratic [Bézier Curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve). QuadraticBezier(QuadraticBezierShape), @@ -71,9 +73,13 @@ pub enum Shape { #[test] fn shape_size() { assert_eq!( - std::mem::size_of::(), 72, + std::mem::size_of::(), 64, "Shape changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it." ); + assert!( + std::mem::size_of::() <= 64, + "Shape is getting way too big!" + ); } #[test] @@ -92,6 +98,13 @@ impl From> for Shape { impl From for Shape { #[inline(always)] fn from(mesh: Mesh) -> Self { + Self::Mesh(mesh.into()) + } +} + +impl From> for Shape { + #[inline(always)] + fn from(mesh: Arc) -> Self { Self::Mesh(mesh) } } @@ -322,7 +335,8 @@ impl Shape { } #[inline] - pub fn mesh(mesh: Mesh) -> Self { + pub fn mesh(mesh: impl Into>) -> Self { + let mesh = mesh.into(); debug_assert!(mesh.is_valid()); Self::Mesh(mesh) } @@ -454,7 +468,7 @@ impl Shape { galley.rect = transform.scaling * galley.rect; } Self::Mesh(mesh) => { - mesh.transform(transform); + Arc::make_mut(mesh).transform(transform); } Self::QuadraticBezier(bezier_shape) => { bezier_shape.points[0] = transform * bezier_shape.points[0]; diff --git a/crates/epaint/src/tessellator.rs b/crates/epaint/src/tessellator.rs index 8e5eb47dd40..57d30b11be8 100644 --- a/crates/epaint/src/tessellator.rs +++ b/crates/epaint/src/tessellator.rs @@ -1406,7 +1406,7 @@ impl Tessellator { return; } - out.append(mesh); + out.append_ref(&mesh); } Shape::LineSegment { points, stroke } => { self.tessellate_line_segment(points, stroke, out); @@ -2177,7 +2177,7 @@ impl Tessellator { profiling::scope!("distribute results", tessellated.len().to_string()); for (index, mesh) in tessellated { - shapes[index].shape = Shape::Mesh(mesh); + shapes[index].shape = Shape::Mesh(mesh.into()); } }