Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T05:06:11.760Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

display_map.rs

215 lines · 7.1 KB · rust
1use criterion::{BenchmarkId, Criterion, black_box, criterion_group, criterion_main};
2use editor::{MultiBuffer, display_map::*};
3use gpui::{AppContext as _, HighlightStyle, Hsla, TestDispatcher, font, px};
4use itertools::Itertools;
5use multi_buffer::MultiBufferOffset;
6use project::project_settings::DiagnosticSeverity;
7use rand::{Rng, SeedableRng, rngs::StdRng};
8use settings::SettingsStore;
9use std::{num::NonZeroU32, time::Duration};
10use text::Bias;
11use util::RandomCharIter;
12
13fn to_tab_point_benchmark(c: &mut Criterion) {
14    let dispatcher = TestDispatcher::new(1);
15    let cx = gpui::TestAppContext::build(dispatcher, None);
16
17    let create_tab_map = |length: usize| {
18        let mut rng = StdRng::seed_from_u64(1);
19        let text = RandomCharIter::new(&mut rng)
20            .take(length)
21            .collect::<String>();
22        let buffer = cx.update(|cx| MultiBuffer::build_simple(&text, cx));
23
24        let buffer_snapshot = cx.read(|cx| buffer.read(cx).snapshot(cx));
25        use editor::display_map::*;
26        let (_, inlay_snapshot) = InlayMap::new(buffer_snapshot);
27        let (_, fold_snapshot) = FoldMap::new(inlay_snapshot.clone());
28        let fold_point = fold_snapshot.to_fold_point(
29            inlay_snapshot.to_point(InlayOffset(
30                rng.random_range(MultiBufferOffset(0)..MultiBufferOffset(length)),
31            )),
32            Bias::Left,
33        );
34        let (_, snapshot) = TabMap::new(fold_snapshot, NonZeroU32::new(4).unwrap());
35
36        (length, snapshot, fold_point)
37    };
38
39    let inputs = [1024].into_iter().map(create_tab_map).collect_vec();
40
41    let mut group = c.benchmark_group("To tab point");
42
43    for (batch_size, snapshot, fold_point) in inputs {
44        group.bench_with_input(
45            BenchmarkId::new("to_tab_point", batch_size),
46            &snapshot,
47            |bench, snapshot| {
48                bench.iter(|| {
49                    snapshot.fold_point_to_tab_point(fold_point);
50                });
51            },
52        );
53    }
54
55    group.finish();
56}
57
58fn to_fold_point_benchmark(c: &mut Criterion) {
59    let dispatcher = TestDispatcher::new(1);
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
106fn create_highlight_endpoints_benchmark(c: &mut Criterion) {
107    const LINE_COUNT: usize = 20_000;
108    const LINE_VIEW_PORT_COUNT: usize = 100;
109    const HIGHLIGHTS_PER_LINE: usize = 4;
110
111    let dispatcher = TestDispatcher::new(1);
112    let mut cx = gpui::TestAppContext::build(dispatcher, None);
113    cx.update(|cx| {
114        let store = SettingsStore::test(cx);
115        cx.set_global(store);
116        editor::init(cx);
117    });
118
119    let mut text = String::new();
120    let mut highlight_ranges = Vec::with_capacity(LINE_COUNT * HIGHLIGHTS_PER_LINE);
121    for line in 0..LINE_COUNT {
122        text.push_str("fn item_");
123        text.push_str(&format!("{line:05}"));
124        text.push_str("() { ");
125
126        let start = text.len();
127        text.push_str("alpha_highlight");
128        highlight_ranges.push(MultiBufferOffset(start)..MultiBufferOffset(text.len()));
129
130        text.push_str(" + ");
131        let start = text.len();
132        text.push_str("beta_highlight");
133        highlight_ranges.push(MultiBufferOffset(start)..MultiBufferOffset(text.len()));
134
135        text.push_str(" + ");
136        let start = text.len();
137        text.push_str("gamma_highlight");
138        highlight_ranges.push(MultiBufferOffset(start)..MultiBufferOffset(text.len()));
139
140        text.push_str(" + ");
141        let start = text.len();
142        text.push_str("delta_highlight");
143        highlight_ranges.push(MultiBufferOffset(start)..MultiBufferOffset(text.len()));
144
145        text.push_str("; }\n");
146    }
147
148    let buffer = cx.update(|cx| MultiBuffer::build_simple(&text, cx));
149    let buffer_snapshot = cx.read(|cx| buffer.read(cx).snapshot(cx));
150    let highlight_ranges = highlight_ranges
151        .into_iter()
152        .map(|range| {
153            buffer_snapshot.anchor_before(range.start)..buffer_snapshot.anchor_before(range.end)
154        })
155        .collect();
156
157    let map = cx.new(|cx| {
158        DisplayMap::new(
159            buffer,
160            font("Courier"),
161            px(16.0),
162            None,
163            1,
164            1,
165            FoldPlaceholder::default(),
166            DiagnosticSeverity::Warning,
167            cx,
168        )
169    });
170    cx.update(|cx| {
171        map.update(cx, |map, cx| {
172            map.highlight_text(
173                HighlightKey::Editor,
174                highlight_ranges,
175                HighlightStyle {
176                    color: Some(Hsla::blue()),
177                    ..Default::default()
178                },
179                false,
180                cx,
181            );
182        });
183    });
184    let snapshot = cx.update(|cx| map.update(cx, |map, cx| map.snapshot(cx)));
185
186    let mut group = c.benchmark_group("Create highlight endpoints");
187    group.sample_size(10);
188    group.measurement_time(Duration::from_secs(10));
189    group.bench_with_input(
190        BenchmarkId::new("text_highlights", LINE_VIEW_PORT_COUNT),
191        &snapshot,
192        |bench, snapshot| {
193            bench.iter(|| {
194                black_box(snapshot.chunks(
195                    DisplayRow(400)..DisplayRow(400 + LINE_VIEW_PORT_COUNT as u32),
196                    language::LanguageAwareStyling {
197                        tree_sitter: false,
198                        diagnostics: false,
199                    },
200                    Default::default(),
201                ));
202            });
203        },
204    );
205    group.finish();
206}
207
208criterion_group!(
209    benches,
210    to_tab_point_benchmark,
211    to_fold_point_benchmark,
212    create_highlight_endpoints_benchmark
213);
214criterion_main!(benches);
215
Served at tenant.openagents/omega Member data and write actions are omitted.