display_map.rs

  1use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
  2use editor::MultiBuffer;
  3use gpui::TestDispatcher;
  4use itertools::Itertools;
  5use rand::{Rng, SeedableRng, rngs::StdRng};
  6use std::num::NonZeroU32;
  7use text::Bias;
  8use util::RandomCharIter;
  9
 10fn to_tab_point_benchmark(c: &mut Criterion) {
 11    let rng = StdRng::seed_from_u64(1);
 12    let dispatcher = TestDispatcher::new(rng);
 13    let cx = gpui::TestAppContext::build(dispatcher, None);
 14
 15    let create_tab_map = |length: usize| {
 16        let mut rng = StdRng::seed_from_u64(1);
 17        let text = RandomCharIter::new(&mut rng)
 18            .take(length)
 19            .collect::<String>();
 20        let buffer = cx.update(|cx| MultiBuffer::build_simple(&text, cx));
 21
 22        let buffer_snapshot = cx.read(|cx| buffer.read(cx).snapshot(cx));
 23        use editor::display_map::*;
 24        let (_, inlay_snapshot) = InlayMap::new(buffer_snapshot);
 25        let (_, fold_snapshot) = FoldMap::new(inlay_snapshot.clone());
 26        let fold_point = fold_snapshot.to_fold_point(
 27            inlay_snapshot.to_point(InlayOffset(rng.random_range(0..length))),
 28            Bias::Left,
 29        );
 30        let (_, snapshot) = TabMap::new(fold_snapshot, NonZeroU32::new(4).unwrap());
 31
 32        (length, snapshot, fold_point)
 33    };
 34
 35    let inputs = [1024].into_iter().map(create_tab_map).collect_vec();
 36
 37    let mut group = c.benchmark_group("To tab point");
 38
 39    for (batch_size, snapshot, fold_point) in inputs {
 40        group.bench_with_input(
 41            BenchmarkId::new("to_tab_point", batch_size),
 42            &snapshot,
 43            |bench, snapshot| {
 44                bench.iter(|| {
 45                    snapshot.to_tab_point(fold_point);
 46                });
 47            },
 48        );
 49    }
 50
 51    group.finish();
 52}
 53
 54fn to_fold_point_benchmark(c: &mut Criterion) {
 55    let rng = StdRng::seed_from_u64(1);
 56    let dispatcher = TestDispatcher::new(rng);
 57    let cx = gpui::TestAppContext::build(dispatcher, None);
 58
 59    let create_tab_map = |length: usize| {
 60        let mut rng = StdRng::seed_from_u64(1);
 61        let text = RandomCharIter::new(&mut rng)
 62            .take(length)
 63            .collect::<String>();
 64        let buffer = cx.update(|cx| MultiBuffer::build_simple(&text, cx));
 65
 66        let buffer_snapshot = cx.read(|cx| buffer.read(cx).snapshot(cx));
 67        use editor::display_map::*;
 68        let (_, inlay_snapshot) = InlayMap::new(buffer_snapshot);
 69        let (_, fold_snapshot) = FoldMap::new(inlay_snapshot.clone());
 70
 71        let fold_point = fold_snapshot.to_fold_point(
 72            inlay_snapshot.to_point(InlayOffset(rng.random_range(0..length))),
 73            Bias::Left,
 74        );
 75
 76        let (_, snapshot) = TabMap::new(fold_snapshot, NonZeroU32::new(4).unwrap());
 77        let tab_point = snapshot.to_tab_point(fold_point);
 78
 79        (length, snapshot, tab_point)
 80    };
 81
 82    let inputs = [1024].into_iter().map(create_tab_map).collect_vec();
 83
 84    let mut group = c.benchmark_group("To fold point");
 85
 86    for (batch_size, snapshot, tab_point) in inputs {
 87        group.bench_with_input(
 88            BenchmarkId::new("to_fold_point", batch_size),
 89            &snapshot,
 90            |bench, snapshot| {
 91                bench.iter(|| {
 92                    snapshot.to_fold_point(tab_point, Bias::Left);
 93                });
 94            },
 95        );
 96    }
 97
 98    group.finish();
 99}
100
101criterion_group!(benches, to_tab_point_benchmark, to_fold_point_benchmark);
102criterion_main!(benches);