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