Skip to content

Commit

Permalink
Add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
afishhh committed Dec 11, 2024
1 parent c094ee8 commit 3e1ed18
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,7 @@ dependencies = [
"egui_demo_lib",
"egui_extras",
"egui_kittest",
"rand",
"serde",
"unicode_names2",
"wgpu",
Expand Down
1 change: 1 addition & 0 deletions crates/egui_demo_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
31 changes: 31 additions & 0 deletions crates/egui_demo_lib/benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -122,6 +125,34 @@ 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();
b.iter(|| {
fonts.begin_pass(pixels_per_point, max_texture_side);
let mut temp_string = String::with_capacity(string.len());
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, 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();
Expand Down

0 comments on commit 3e1ed18

Please sign in to comment.