1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-03 07:10:04 -04:00

Use Arc to avoid some expensive clones

This commit is contained in:
Emil Ernerfeldt
2023-08-14 13:36:52 +02:00
parent 29eb119714
commit 75617d5a15
2 changed files with 12 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
use std::{fmt::Debug, ops::RangeInclusive}; use std::{fmt::Debug, ops::RangeInclusive, sync::Arc};
use epaint::{ use epaint::{
emath::{lerp, remap_clamp, round_to_decimals}, emath::{lerp, remap_clamp, round_to_decimals},
@@ -143,7 +143,7 @@ pub(super) struct AxisWidget<const AXIS: usize> {
pub(super) hints: AxisHints<AXIS>, pub(super) hints: AxisHints<AXIS>,
pub(super) rect: Rect, pub(super) rect: Rect,
pub(super) transform: Option<PlotTransform>, pub(super) transform: Option<PlotTransform>,
pub(super) steps: Vec<GridMark>, pub(super) steps: Arc<Vec<GridMark>>,
} }
impl<const AXIS: usize> AxisWidget<AXIS> { impl<const AXIS: usize> AxisWidget<AXIS> {
@@ -154,7 +154,7 @@ impl<const AXIS: usize> AxisWidget<AXIS> {
hints, hints,
rect, rect,
transform: None, transform: None,
steps: Vec::new(), steps: Default::default(),
} }
} }
} }
@@ -228,7 +228,7 @@ impl<const AXIS: usize> Widget for AxisWidget<AXIS> {
None => return response, 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); let text = (self.hints.formatter)(step.value, self.hints.digits, &self.range);
if !text.is_empty() { if !text.is_empty() {
const MIN_TEXT_SPACING: f32 = 20.0; const MIN_TEXT_SPACING: f32 = 20.0;

View File

@@ -1,9 +1,8 @@
//! Simple plotting library. //! Simple plotting library.
use ahash::HashMap; use std::{ops::RangeInclusive, sync::Arc};
use std::ops::RangeInclusive;
use crate::*; use ahash::HashMap;
use epaint::util::FloatOrd; use epaint::util::FloatOrd;
use epaint::Hsva; use epaint::Hsva;
@@ -11,6 +10,8 @@ use axis::{XAxisWidget, YAxisWidget, X_AXIS, Y_AXIS};
use items::PlotItem; use items::PlotItem;
use legend::LegendWidget; use legend::LegendWidget;
use crate::*;
pub use items::{ pub use items::{
Arrows, Bar, BarChart, BoxElem, BoxPlot, BoxSpread, HLine, Line, LineStyle, MarkerShape, Arrows, Bar, BarChart, BoxElem, BoxPlot, BoxSpread, HLine, Line, LineStyle, MarkerShape,
Orientation, PlotImage, PlotPoint, PlotPoints, Points, Polygon, Text, VLine, Orientation, PlotImage, PlotPoint, PlotPoints, Points, Polygon, Text, VLine,
@@ -1178,21 +1179,21 @@ impl Plot {
// Add legend widgets to plot // Add legend widgets to plot
let bounds = transform.bounds(); let bounds = transform.bounds();
let x_axis_range = bounds.range_x(); let x_axis_range = bounds.range_x();
let x_steps = { let x_steps = Arc::new({
let input = GridInput { let input = GridInput {
bounds: (bounds.min[X_AXIS], bounds.max[X_AXIS]), bounds: (bounds.min[X_AXIS], bounds.max[X_AXIS]),
base_step_size: transform.dvalue_dpos()[X_AXIS] * MIN_LINE_SPACING_IN_POINTS * 2.0, base_step_size: transform.dvalue_dpos()[X_AXIS] * MIN_LINE_SPACING_IN_POINTS * 2.0,
}; };
(grid_spacers[X_AXIS])(input) (grid_spacers[X_AXIS])(input)
}; });
let y_axis_range = bounds.range_y(); let y_axis_range = bounds.range_y();
let y_steps = { let y_steps = Arc::new({
let input = GridInput { let input = GridInput {
bounds: (bounds.min[Y_AXIS], bounds.max[Y_AXIS]), bounds: (bounds.min[Y_AXIS], bounds.max[Y_AXIS]),
base_step_size: transform.dvalue_dpos()[Y_AXIS] * MIN_LINE_SPACING_IN_POINTS * 2.0, base_step_size: transform.dvalue_dpos()[Y_AXIS] * MIN_LINE_SPACING_IN_POINTS * 2.0,
}; };
(grid_spacers[Y_AXIS])(input) (grid_spacers[Y_AXIS])(input)
}; });
for mut widget in x_axis_widgets { for mut widget in x_axis_widgets {
widget.range = x_axis_range.clone(); widget.range = x_axis_range.clone();
widget.transform = Some(transform); widget.transform = Some(transform);