display_map.rs

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