| 1 | use criterion::{Criterion, criterion_group, criterion_main}; |
| 2 | use gpui::{FontFallbacks, FontRun, PlatformTextSystem, font, px}; |
| 3 | use gpui_wgpu::CosmicTextSystem; |
| 4 | use std::borrow::Cow; |
| 5 | |
| 6 | const LILEX: &[u8] = include_bytes!("../../../assets/fonts/lilex/Lilex-Regular.ttf"); |
| 7 | const IBM_PLEX: &[u8] = |
| 8 | include_bytes!("../../../assets/fonts/ibm-plex-sans/IBMPlexSans-Regular.ttf"); |
| 9 | |
| 10 | // ~4 000 chars of typical ASCII code text, as a single display line. |
| 11 | // |
| 12 | // `layout_line` is handed one line at a time, already split on `\n` by its |
| 13 | // callers, so the newlines are replaced rather than kept: leaving them in would |
| 14 | // make this measure the multi-paragraph path instead of the common one, since |
| 15 | // `\n` is itself a bidi paragraph separator. |
| 16 | fn code_text() -> String { |
| 17 | concat!( |
| 18 | " fn compute_run_spans(\n", |
| 19 | " text: &str,\n", |
| 20 | " run_offset: usize,\n", |
| 21 | " run_len: usize,\n", |
| 22 | " primary: FontId,\n", |
| 23 | " fallback_chain: &[(FontId, SharedString)],\n", |
| 24 | " covers: &impl Fn(FontId, char) -> bool,\n", |
| 25 | " ) -> SmallVec<[RunSpan; 4]> {\n", |
| 26 | " let mut spans = SmallVec::new();\n", |
| 27 | " let run_end = run_offset + run_len;\n", |
| 28 | " if run_end <= run_offset { return spans; }\n", |
| 29 | " let run_text = &text[run_offset..run_end];\n", |
| 30 | " let mut span_start = run_offset;\n", |
| 31 | " let mut span_slot: Option<usize> = None;\n", |
| 32 | " for (ch_idx, ch) in run_text.char_indices() {\n", |
| 33 | " let abs = run_offset + ch_idx;\n", |
| 34 | " let next = pick_covering_slot(ch, span_slot, primary, fallback_chain, covers);\n", |
| 35 | " if next == span_slot { continue; }\n", |
| 36 | " if abs > span_start {\n", |
| 37 | " spans.push(RunSpan { start: span_start, end: abs, slot: span_slot });\n", |
| 38 | " }\n", |
| 39 | " span_start = abs;\n", |
| 40 | " span_slot = next;\n", |
| 41 | " }\n", |
| 42 | " spans\n", |
| 43 | " }\n", |
| 44 | ) |
| 45 | .repeat(8) // ~3 800 chars |
| 46 | .replace('\n', " ") |
| 47 | } |
| 48 | |
| 49 | fn bench_layout_line(c: &mut Criterion) { |
| 50 | let system = CosmicTextSystem::new_without_system_fonts("Lilex"); |
| 51 | system |
| 52 | .add_fonts(vec![Cow::Borrowed(LILEX), Cow::Borrowed(IBM_PLEX)]) |
| 53 | .unwrap(); |
| 54 | |
| 55 | let font_id_no_fallback = system.font_id(&font("Lilex")).unwrap(); |
| 56 | |
| 57 | let font_id_with_fallback = { |
| 58 | let mut f = font("Lilex"); |
| 59 | f.fallbacks = Some(FontFallbacks::from_fonts(vec!["IBM Plex Sans".to_string()])); |
| 60 | system.font_id(&f).unwrap() |
| 61 | }; |
| 62 | |
| 63 | let text = code_text(); |
| 64 | |
| 65 | // Same text, but with a bidi paragraph separator (U+001C) and RTL content |
| 66 | // forcing the per-paragraph shaping path. |
| 67 | let text_mixed_direction = text.clone() + "\u{001c}\u{05d0}\u{05d1}"; |
| 68 | assert!( |
| 69 | !text.contains('\n'), |
| 70 | "fast-path corpus must contain no separator" |
| 71 | ); |
| 72 | |
| 73 | let runs_no_fallback = vec![FontRun { |
| 74 | len: text.len(), |
| 75 | font_id: font_id_no_fallback, |
| 76 | }]; |
| 77 | let runs_with_fallback = vec![FontRun { |
| 78 | len: text.len(), |
| 79 | font_id: font_id_with_fallback, |
| 80 | }]; |
| 81 | let runs_mixed_direction = vec![FontRun { |
| 82 | len: text_mixed_direction.len(), |
| 83 | font_id: font_id_no_fallback, |
| 84 | }]; |
| 85 | |
| 86 | let mut group = c.benchmark_group("layout_line"); |
| 87 | |
| 88 | group.bench_function("no_fallback", |b| { |
| 89 | b.iter(|| system.layout_line(&text, px(14.0), &runs_no_fallback)) |
| 90 | }); |
| 91 | |
| 92 | group.bench_function("with_fallback_ascii", |b| { |
| 93 | b.iter(|| system.layout_line(&text, px(14.0), &runs_with_fallback)) |
| 94 | }); |
| 95 | |
| 96 | group.bench_function("mixed_direction_paragraphs", |b| { |
| 97 | b.iter(|| system.layout_line(&text_mixed_direction, px(14.0), &runs_mixed_direction)) |
| 98 | }); |
| 99 | |
| 100 | group.finish(); |
| 101 | } |
| 102 | |
| 103 | criterion_group!(benches, bench_layout_line); |
| 104 | criterion_main!(benches); |