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

Faster dashed line generation #1027

Merged
merged 2 commits into from Jan 6, 2022
Merged

Conversation

lampsitter
Copy link
Contributor

The Shape::dashed_line was quite hot in my program, so I decided to optimize it.
In addition to the original function I added an alternative function, which allows reuse of an existing Vec.

Old:

running 1 test
test single ... bench:      81,939 ns/iter (+/- 7,938)

New:

running 2 tests
test single ... bench:      19,690 ns/iter (+/- 1,336)
test many   ... bench:      10,081 ns/iter (+/- 347)

Benchmark code:

#![feature(test)]

extern crate test;
use eframe::egui::{Shape, Stroke};
use test::Bencher;

#[bench]
fn single(b: &mut Bencher) {
    b.iter(|| {
        let mut v = Vec::new();
        use eframe::egui;

        let line = [
            egui::pos2(0.0, 0.0),
            egui::pos2(50.0, 0.0),
            egui::pos2(100.0, 1.0),
        ];

        for _ in 0..100 {
            v.extend(Shape::dashed_line(
                &line,
                Stroke::new(1.5, eframe::egui::Color32::RED),
                10.0,
                2.5,
            ));
        }

        test::black_box(v);
    });
}

// Must be commented out when run against master
#[bench]
fn many(b: &mut Bencher) {
    b.iter(|| {
        let mut v = Vec::new();
        use eframe::egui;

        let line = [
            egui::pos2(0.0, 0.0),
            egui::pos2(50.0, 0.0),
            egui::pos2(100.0, 1.0),
        ];

        for _ in 0..100 {
            Shape::dashed_line_many(
                &line,
                Stroke::new(1.5, eframe::egui::Color32::RED),
                10.0,
                2.5,
                &mut v,
            );
        }

        test::black_box(v);
    });
}

@lampsitter lampsitter marked this pull request as ready for review January 1, 2022 16:09
Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Feel free to also add the benchmark, perhaps to a new epaint/benches/benchmark.rs file?

epaint/benches/benchmark.rs Outdated Show resolved Hide resolved
@emilk emilk merged commit d31f7d6 into emilk:master Jan 6, 2022
@emilk
Copy link
Owner

emilk commented Jan 6, 2022

thanks!

@lampsitter lampsitter deleted the faster-dashed-lines branch January 6, 2022 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants