egui/egui_demo_lib/benches/benchmark.rs
Emil Ernerfeldt de1a1ba9b2
New text layout (#682)
This PR introduces a completely rewritten text layout engine which is simpler and more powerful. It allows mixing different text styles (heading, body, etc) and formats (color, underlining, strikethrough, …) in the same layout pass, and baked into the same `Galley`.

This opens up the door to having a syntax-highlighed code editor, or a WYSIWYG markdown editor.

One major change is the color is now baked in at layout time. However, many widgets changes text color on hovered. But we need to do the text layout before we know if it is hovered. Therefor the painter has an option to override the text color of a galley.


## Performance
Text layout alone is about 20% slower, but a lot of that is because more tessellation is done upfront. Text tessellation is now a lot faster, but text layout + tessellation still lands at a net loss of 5-10% in performance. There are however a few tricks to speed it up (like using `smallvec`) which I am saving for later. Text layout is also cached, meaning that in most cases (when all text isn't changing each frame) text tessellation is actually more important (and that's more than 2x faster!).

Sadly, the actual text cache lookup is significantly slower (300ns -> 600ns). That's because the `TextLayoutJob` is a lot bigger (it has more options, like underlining, fonts etc), so it is slower to hash and compare. I have an idea how to speed this up, but I need to do some other work before I can implement that.

All in all, the performance impact on `demo_with_tesselate__realistic` is about 5-6% in the red. Not great; not terrible. The benefits are worth it, but I also think with some work I can get that down significantly, hopefully down to the old levels.
2021-09-03 18:18:00 +02:00

113 lines
3.6 KiB
Rust

use criterion::{criterion_group, criterion_main, Criterion};
use egui_demo_lib::LOREM_IPSUM_LONG;
pub fn criterion_benchmark(c: &mut Criterion) {
let raw_input = egui::RawInput::default();
{
let mut ctx = egui::CtxRef::default();
let mut demo_windows = egui_demo_lib::DemoWindows::default();
// The most end-to-end benchmark.
c.bench_function("demo_with_tesselate__realistic", |b| {
b.iter(|| {
ctx.begin_frame(raw_input.clone());
demo_windows.ui(&ctx);
let (_, shapes) = ctx.end_frame();
ctx.tessellate(shapes)
})
});
c.bench_function("demo_no_tesselate", |b| {
b.iter(|| {
ctx.begin_frame(raw_input.clone());
demo_windows.ui(&ctx);
ctx.end_frame()
})
});
ctx.begin_frame(raw_input.clone());
demo_windows.ui(&ctx);
let (_, shapes) = ctx.end_frame();
c.bench_function("demo_only_tessellate", |b| {
b.iter(|| ctx.tessellate(shapes.clone()))
});
}
if false {
let mut ctx = egui::CtxRef::default();
ctx.memory().set_everything_is_visible(true); // give us everything
let mut demo_windows = egui_demo_lib::DemoWindows::default();
c.bench_function("demo_full_no_tesselate", |b| {
b.iter(|| {
ctx.begin_frame(raw_input.clone());
demo_windows.ui(&ctx);
ctx.end_frame()
})
});
}
{
let mut ctx = egui::CtxRef::default();
ctx.begin_frame(raw_input);
let mut ui = egui::Ui::__test();
c.bench_function("label &str", |b| {
b.iter(|| {
ui.label("the quick brown fox jumps over the lazy dog");
})
});
c.bench_function("label format!", |b| {
b.iter(|| {
ui.label("the quick brown fox jumps over the lazy dog".to_owned());
})
});
}
{
let pixels_per_point = 1.0;
let wrap_width = 512.0;
let text_style = egui::TextStyle::Body;
let color = egui::Color32::WHITE;
let fonts = egui::epaint::text::Fonts::from_definitions(
pixels_per_point,
egui::FontDefinitions::default(),
);
c.bench_function("text_layout_uncached", |b| {
b.iter(|| {
use egui::epaint::text::{layout, LayoutJob};
let job = LayoutJob::simple(
LOREM_IPSUM_LONG.to_owned(),
egui::TextStyle::Body,
color,
wrap_width,
);
layout(&fonts, job.into())
})
});
c.bench_function("text_layout_cached", |b| {
b.iter(|| fonts.layout(LOREM_IPSUM_LONG.to_owned(), text_style, color, wrap_width))
});
let galley = fonts.layout(LOREM_IPSUM_LONG.to_owned(), text_style, color, wrap_width);
let mut tessellator = egui::epaint::Tessellator::from_options(Default::default());
let mut mesh = egui::epaint::Mesh::default();
c.bench_function("tessellate_text", |b| {
b.iter(|| {
tessellator.tessellate_text(
fonts.texture().size(),
egui::Pos2::ZERO,
&galley,
Default::default(),
None,
&mut mesh,
);
mesh.clear();
})
});
}
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);