Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Arc instead of Vec for cheaper PlotPoints clone #4562

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions crates/egui_plot/src/items/values.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::ops::{Bound, RangeBounds, RangeInclusive};
use std::{
ops::{Bound, RangeBounds, RangeInclusive},
sync::Arc,
};

use egui::{Pos2, Shape, Stroke, Vec2};

Expand Down Expand Up @@ -154,43 +157,71 @@ impl Default for Orientation {
///
/// These can be an owned `Vec` or generated with a function.
pub enum PlotPoints {
Owned(Vec<PlotPoint>),
Owned(Arc<[PlotPoint]>),
Generator(ExplicitGenerator),
// Borrowed(&[PlotPoint]), // TODO(EmbersArc): Lifetimes are tricky in this case.
}

impl Default for PlotPoints {
fn default() -> Self {
Self::Owned(Vec::new())
Self::Owned(Vec::new().into())
}
}

impl From<[f64; 2]> for PlotPoints {
fn from(coordinate: [f64; 2]) -> Self {
Self::new(vec![coordinate])
std::iter::once(coordinate).collect()
}
}

impl From<&[[f64; 2]]> for PlotPoints {
fn from(coordinates: &[[f64; 2]]) -> Self {
coordinates.iter().collect()
}
}

impl From<Vec<[f64; 2]>> for PlotPoints {
fn from(coordinates: Vec<[f64; 2]>) -> Self {
Self::new(coordinates)
coordinates.into_iter().collect()
}
}

impl From<Arc<[[f64; 2]]>> for PlotPoints {
fn from(coordinates: Arc<[[f64; 2]]>) -> Self {
coordinates.iter().collect()
}
}

impl<'a> FromIterator<&'a [f64; 2]> for PlotPoints {
fn from_iter<T: IntoIterator<Item = &'a [f64; 2]>>(iter: T) -> Self {
Self::Owned(
iter.into_iter()
.map(|point| (*point).into())
.collect::<Vec<_>>()
.into(),
)
}
}

impl FromIterator<[f64; 2]> for PlotPoints {
fn from_iter<T: IntoIterator<Item = [f64; 2]>>(iter: T) -> Self {
Self::Owned(iter.into_iter().map(|point| point.into()).collect())
Self::Owned(
iter.into_iter()
.map(|point| point.into())
.collect::<Vec<_>>()
.into(),
)
}
}

impl PlotPoints {
pub fn new(points: Vec<[f64; 2]>) -> Self {
Self::from_iter(points)
pub fn new(points: Arc<[PlotPoint]>) -> Self {
Self::Owned(points)
}

pub fn points(&self) -> &[PlotPoint] {
match self {
Self::Owned(points) => points.as_slice(),
Self::Owned(points) => points,
Self::Generator(_) => &[],
}
}
Expand Down Expand Up @@ -247,7 +278,8 @@ impl PlotPoints {
let t = start + i as f64 * increment;
function(t).into()
})
.collect()
.collect::<Vec<_>>()
.into()
}

/// From a series of y-values.
Expand Down Expand Up @@ -307,7 +339,7 @@ impl PlotPoints {
match self {
Self::Owned(points) => {
let mut bounds = PlotBounds::NOTHING;
for point in points {
for point in points.iter() {
bounds.extend_with(point);
}
bounds
Expand Down
Loading