From 1c415bd8fe109d2afba891b827e1bf99b3a2c997 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 8 Feb 2021 22:53:31 +0100 Subject: [PATCH] Add benchmark for text layout and tesselation --- egui_demo_lib/benches/benchmark.rs | 37 +++++++++++++++++++++++++++++- epaint/src/lib.rs | 2 +- epaint/src/mesh.rs | 7 ++++++ epaint/src/text/font.rs | 4 ++-- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/egui_demo_lib/benches/benchmark.rs b/egui_demo_lib/benches/benchmark.rs index 7a9f9844..72d77373 100644 --- a/egui_demo_lib/benches/benchmark.rs +++ b/egui_demo_lib/benches/benchmark.rs @@ -1,5 +1,7 @@ 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(); @@ -47,12 +49,45 @@ pub fn criterion_benchmark(c: &mut Criterion) { egui::CentralPanel::default().show(&ctx, |ui| { c.bench_function("label", |b| { b.iter(|| { - ui.label(egui_demo_lib::LOREM_IPSUM_LONG); + ui.label(LOREM_IPSUM_LONG); }) }); }); let _ = ctx.end_frame(); } + + { + let pixels_per_point = 1.0; + let wrap_width = 512.0; + let text_style = egui::TextStyle::Body; + let fonts = egui::paint::text::Fonts::from_definitions( + pixels_per_point, + egui::FontDefinitions::default(), + ); + let font = &fonts[text_style]; + c.bench_function("text layout", |b| { + b.iter(|| font.layout_multiline(LOREM_IPSUM_LONG.to_owned(), wrap_width)) + }); + + let galley = font.layout_multiline(LOREM_IPSUM_LONG.to_owned(), wrap_width); + let mut tesselator = egui::paint::Tessellator::from_options(Default::default()); + let mut mesh = egui::paint::Mesh::default(); + c.bench_function("tesselate text", |b| { + b.iter(|| { + let fake_italics = false; + tesselator.tessellate_text( + &fonts, + egui::Pos2::ZERO, + &galley, + text_style, + egui::Color32::WHITE, + fake_italics, + &mut mesh, + ); + mesh.clear(); + }) + }); + } } criterion_group!(benches, criterion_benchmark); diff --git a/epaint/src/lib.rs b/epaint/src/lib.rs index ef7fb35b..4fb844fd 100644 --- a/epaint/src/lib.rs +++ b/epaint/src/lib.rs @@ -63,7 +63,7 @@ pub use { shape::Shape, stats::PaintStats, stroke::Stroke, - tessellator::TessellationOptions, + tessellator::{TessellationOptions, Tessellator}, text::{Galley, TextStyle}, texture_atlas::{Texture, TextureAtlas}, }; diff --git a/epaint/src/mesh.rs b/epaint/src/mesh.rs index c532556e..d9b2441e 100644 --- a/epaint/src/mesh.rs +++ b/epaint/src/mesh.rs @@ -45,6 +45,13 @@ impl Mesh { } } + /// Restore to default state, but without freeing memory. + pub fn clear(&mut self) { + self.indices.clear(); + self.vertices.clear(); + self.vertices = Default::default(); + } + pub fn bytes_used(&self) -> usize { std::mem::size_of::() + self.vertices.len() * std::mem::size_of::() diff --git a/epaint/src/text/font.rs b/epaint/src/text/font.rs index efc50bcc..2ec97543 100644 --- a/epaint/src/text/font.rs +++ b/epaint/src/text/font.rs @@ -218,8 +218,8 @@ impl Font { /// `\n` will (intentionally) show up as the replacement character. fn glyph_info(&self, c: char) -> (FontIndex, GlyphInfo) { { - if let Some(glyph_info) = self.glyph_info_cache.read().get(&c) { - return *glyph_info; + if let Some(font_index_glyph_info) = self.glyph_info_cache.read().get(&c) { + return *font_index_glyph_info; } }