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 (_, filter_snapshot) = FilterMap::new(None, buffer_snapshot);
25 let (_, inlay_snapshot) = InlayMap::new(filter_snapshot);
26 let (_, fold_snapshot) = FoldMap::new(inlay_snapshot.clone());
27 let fold_point = fold_snapshot.to_fold_point(
28 inlay_snapshot.to_point(InlayOffset(rng.random_range(0..length))),
29 Bias::Left,
30 );
31 let (_, snapshot) = TabMap::new(fold_snapshot, NonZeroU32::new(4).unwrap());
32
33 (length, snapshot, fold_point)
34 };
35
36 let inputs = [1024].into_iter().map(create_tab_map).collect_vec();
37
38 let mut group = c.benchmark_group("To tab point");
39
40 for (batch_size, snapshot, fold_point) in inputs {
41 group.bench_with_input(
42 BenchmarkId::new("to_tab_point", batch_size),
43 &snapshot,
44 |bench, snapshot| {
45 bench.iter(|| {
46 snapshot.to_tab_point(fold_point);
47 });
48 },
49 );
50 }
51
52 group.finish();
53}
54
55fn to_fold_point_benchmark(c: &mut Criterion) {
56 let rng = StdRng::seed_from_u64(1);
57 let dispatcher = TestDispatcher::new(rng);
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 (_, filter_snapshot) = FilterMap::new(None, buffer_snapshot);
70 let (_, inlay_snapshot) = InlayMap::new(filter_snapshot);
71 let (_, fold_snapshot) = FoldMap::new(inlay_snapshot.clone());
72
73 let fold_point = fold_snapshot.to_fold_point(
74 inlay_snapshot.to_point(InlayOffset(rng.random_range(0..length))),
75 Bias::Left,
76 );
77
78 let (_, snapshot) = TabMap::new(fold_snapshot, NonZeroU32::new(4).unwrap());
79 let tab_point = snapshot.to_tab_point(fold_point);
80
81 (length, snapshot, tab_point)
82 };
83
84 let inputs = [1024].into_iter().map(create_tab_map).collect_vec();
85
86 let mut group = c.benchmark_group("To fold point");
87
88 for (batch_size, snapshot, tab_point) in inputs {
89 group.bench_with_input(
90 BenchmarkId::new("to_fold_point", batch_size),
91 &snapshot,
92 |bench, snapshot| {
93 bench.iter(|| {
94 snapshot.to_fold_point(tab_point, Bias::Left);
95 });
96 },
97 );
98 }
99
100 group.finish();
101}
102
103criterion_group!(benches, to_tab_point_benchmark, to_fold_point_benchmark);
104criterion_main!(benches);