From 75617d5a154734473e4633291c86034e028540ff Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 14 Aug 2023 13:36:52 +0200 Subject: [PATCH] Use `Arc` to avoid some expensive clones --- crates/egui/src/widgets/plot/axis.rs | 8 ++++---- crates/egui/src/widgets/plot/mod.rs | 15 ++++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/crates/egui/src/widgets/plot/axis.rs b/crates/egui/src/widgets/plot/axis.rs index 1bcec840f..8fbe08394 100644 --- a/crates/egui/src/widgets/plot/axis.rs +++ b/crates/egui/src/widgets/plot/axis.rs @@ -1,4 +1,4 @@ -use std::{fmt::Debug, ops::RangeInclusive}; +use std::{fmt::Debug, ops::RangeInclusive, sync::Arc}; use epaint::{ emath::{lerp, remap_clamp, round_to_decimals}, @@ -143,7 +143,7 @@ pub(super) struct AxisWidget { pub(super) hints: AxisHints, pub(super) rect: Rect, pub(super) transform: Option, - pub(super) steps: Vec, + pub(super) steps: Arc>, } impl AxisWidget { @@ -154,7 +154,7 @@ impl AxisWidget { hints, rect, transform: None, - steps: Vec::new(), + steps: Default::default(), } } } @@ -228,7 +228,7 @@ impl Widget for AxisWidget { None => return response, }; - for step in self.steps { + for step in self.steps.iter() { let text = (self.hints.formatter)(step.value, self.hints.digits, &self.range); if !text.is_empty() { const MIN_TEXT_SPACING: f32 = 20.0; diff --git a/crates/egui/src/widgets/plot/mod.rs b/crates/egui/src/widgets/plot/mod.rs index 22ce2438e..5f8018668 100644 --- a/crates/egui/src/widgets/plot/mod.rs +++ b/crates/egui/src/widgets/plot/mod.rs @@ -1,9 +1,8 @@ //! Simple plotting library. -use ahash::HashMap; -use std::ops::RangeInclusive; +use std::{ops::RangeInclusive, sync::Arc}; -use crate::*; +use ahash::HashMap; use epaint::util::FloatOrd; use epaint::Hsva; @@ -11,6 +10,8 @@ use axis::{XAxisWidget, YAxisWidget, X_AXIS, Y_AXIS}; use items::PlotItem; use legend::LegendWidget; +use crate::*; + pub use items::{ Arrows, Bar, BarChart, BoxElem, BoxPlot, BoxSpread, HLine, Line, LineStyle, MarkerShape, Orientation, PlotImage, PlotPoint, PlotPoints, Points, Polygon, Text, VLine, @@ -1178,21 +1179,21 @@ impl Plot { // Add legend widgets to plot let bounds = transform.bounds(); let x_axis_range = bounds.range_x(); - let x_steps = { + let x_steps = Arc::new({ let input = GridInput { bounds: (bounds.min[X_AXIS], bounds.max[X_AXIS]), base_step_size: transform.dvalue_dpos()[X_AXIS] * MIN_LINE_SPACING_IN_POINTS * 2.0, }; (grid_spacers[X_AXIS])(input) - }; + }); let y_axis_range = bounds.range_y(); - let y_steps = { + let y_steps = Arc::new({ let input = GridInput { bounds: (bounds.min[Y_AXIS], bounds.max[Y_AXIS]), base_step_size: transform.dvalue_dpos()[Y_AXIS] * MIN_LINE_SPACING_IN_POINTS * 2.0, }; (grid_spacers[Y_AXIS])(input) - }; + }); for mut widget in x_axis_widgets { widget.range = x_axis_range.clone(); widget.transform = Some(transform);