From 2ca22332345a2cf7b1341615e59adb516fb6c386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hubert=20G=C5=82uchowski?= Date: Wed, 11 Dec 2024 18:42:40 +0100 Subject: [PATCH] Add benchmark --- Cargo.lock | 1 + crates/egui_demo_lib/Cargo.toml | 1 + crates/egui_demo_lib/benches/benchmark.rs | 32 +++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index a2e858d4b411..aa57f0754625 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1313,6 +1313,7 @@ dependencies = [ "egui_demo_lib", "egui_extras", "egui_kittest", + "rand", "serde", "unicode_names2", "wgpu", diff --git a/crates/egui_demo_lib/Cargo.toml b/crates/egui_demo_lib/Cargo.toml index f8f1f47f686b..fc59280f2329 100644 --- a/crates/egui_demo_lib/Cargo.toml +++ b/crates/egui_demo_lib/Cargo.toml @@ -58,6 +58,7 @@ serde = { workspace = true, optional = true } # when running tests we always want to use the `chrono` feature egui_demo_lib = { workspace = true, features = ["chrono"] } +rand = "0.8" criterion.workspace = true egui_kittest = { workspace = true, features = ["wgpu", "snapshot"] } wgpu = { workspace = true, features = ["metal"] } diff --git a/crates/egui_demo_lib/benches/benchmark.rs b/crates/egui_demo_lib/benches/benchmark.rs index d3820603d5cc..dcff163866b4 100644 --- a/crates/egui_demo_lib/benches/benchmark.rs +++ b/crates/egui_demo_lib/benches/benchmark.rs @@ -1,7 +1,10 @@ +use std::fmt::Write as _; + use criterion::{criterion_group, criterion_main, Criterion}; use egui::epaint::TextShape; use egui_demo_lib::LOREM_IPSUM_LONG; +use rand::Rng as _; pub fn criterion_benchmark(c: &mut Criterion) { use egui::RawInput; @@ -122,6 +125,35 @@ pub fn criterion_benchmark(c: &mut Criterion) { }); }); + c.bench_function("text_layout_cached_with_modify", |b| { + const MAX_REMOVED_BYTES: usize = 5000; + + let mut string = String::new(); + // 2000 lines * 200 bytes * ~3 characters = 1.2MB + string.reserve(2000 * 200 * 3 + 2000); + for _ in 0..2000 { + for i in 0..200u8 { + write!(string, "{i:02X} ").unwrap(); + } + string.push('\n'); + } + + let mut rng = rand::thread_rng(); + let mut temp_string = String::with_capacity(string.len()); + b.iter(|| { + fonts.begin_pass(pixels_per_point, max_texture_side); + temp_string.clear(); + let modified_start = rng.gen_range(0..string.len()); + let max_end = (modified_start + MAX_REMOVED_BYTES).min(string.len()); + let modified_end = rng.gen_range(modified_start..max_end); + + temp_string.push_str(&string[..modified_start]); + temp_string.push_str(&string[modified_end..]); + + fonts.layout(temp_string.clone(), font_id.clone(), text_color, wrap_width); + }); + }); + let galley = fonts.layout(LOREM_IPSUM_LONG.to_owned(), font_id, text_color, wrap_width); let font_image_size = fonts.font_image_size(); let prepared_discs = fonts.texture_atlas().lock().prepared_discs();