Skip to content

Commit 2ca2233

Browse files
committed
Add benchmark
1 parent c094ee8 commit 2ca2233

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,7 @@ dependencies = [
13131313
"egui_demo_lib",
13141314
"egui_extras",
13151315
"egui_kittest",
1316+
"rand",
13161317
"serde",
13171318
"unicode_names2",
13181319
"wgpu",

crates/egui_demo_lib/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ serde = { workspace = true, optional = true }
5858
# when running tests we always want to use the `chrono` feature
5959
egui_demo_lib = { workspace = true, features = ["chrono"] }
6060

61+
rand = "0.8"
6162
criterion.workspace = true
6263
egui_kittest = { workspace = true, features = ["wgpu", "snapshot"] }
6364
wgpu = { workspace = true, features = ["metal"] }

crates/egui_demo_lib/benches/benchmark.rs

+32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
use std::fmt::Write as _;
2+
13
use criterion::{criterion_group, criterion_main, Criterion};
24

35
use egui::epaint::TextShape;
46
use egui_demo_lib::LOREM_IPSUM_LONG;
7+
use rand::Rng as _;
58

69
pub fn criterion_benchmark(c: &mut Criterion) {
710
use egui::RawInput;
@@ -122,6 +125,35 @@ pub fn criterion_benchmark(c: &mut Criterion) {
122125
});
123126
});
124127

128+
c.bench_function("text_layout_cached_with_modify", |b| {
129+
const MAX_REMOVED_BYTES: usize = 5000;
130+
131+
let mut string = String::new();
132+
// 2000 lines * 200 bytes * ~3 characters = 1.2MB
133+
string.reserve(2000 * 200 * 3 + 2000);
134+
for _ in 0..2000 {
135+
for i in 0..200u8 {
136+
write!(string, "{i:02X} ").unwrap();
137+
}
138+
string.push('\n');
139+
}
140+
141+
let mut rng = rand::thread_rng();
142+
let mut temp_string = String::with_capacity(string.len());
143+
b.iter(|| {
144+
fonts.begin_pass(pixels_per_point, max_texture_side);
145+
temp_string.clear();
146+
let modified_start = rng.gen_range(0..string.len());
147+
let max_end = (modified_start + MAX_REMOVED_BYTES).min(string.len());
148+
let modified_end = rng.gen_range(modified_start..max_end);
149+
150+
temp_string.push_str(&string[..modified_start]);
151+
temp_string.push_str(&string[modified_end..]);
152+
153+
fonts.layout(temp_string.clone(), font_id.clone(), text_color, wrap_width);
154+
});
155+
});
156+
125157
let galley = fonts.layout(LOREM_IPSUM_LONG.to_owned(), font_id, text_color, wrap_width);
126158
let font_image_size = fonts.font_image_size();
127159
let prepared_discs = fonts.texture_atlas().lock().prepared_discs();

0 commit comments

Comments
 (0)