BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-base / benches / text_view_scroll.rs

text_view_scroll.rs

eef6c0a4 History
text_view_scroll.rs 4.4 KiB Download Raw
1//! Scrolling a long Markdown `TextView`: one frame per iteration, the way a
2//! chat scrolls. Every frame re-renders and re-lays out the visible
3//! paragraphs, so this is where the per-paragraph text costs (shaping-cache
4//! lookups, line wrappers, measurement) show up.
5//!
6//! Run with `cargo bench -p gpui-base --bench text_view_scroll`.
7
8use std::fmt::Write as _;
9
10use gpui::{
11 AppContext as _, BenchAppContext, Context, Entity, InteractiveElement as _, IntoElement,
12 ParentElement as _, Render, ScrollHandle, StatefulInteractiveElement as _, Styled as _, Window,
13 div, point, px,
14};
15use gpui_base::text::{TextView, TextViewState};
16
17/// A long assistant answer: headings, emphasis, links, lists, a code block,
18/// and plenty of prose paragraphs.
19fn markdown() -> String {
20 markdown_with(false)
21}
22
23/// The same answer with inline code in every paragraph and list item, the
24/// way a coding assistant's answers name identifiers and paths: those
25/// paragraphs are laid out by `InlineFlow` instead of one `Inline`.
26fn markdown_with_inline_code() -> String {
27 markdown_with(true)
28}
29
30fn markdown_with(inline_code: bool) -> String {
31 let mut out = String::new();
32 let (c, s) = if inline_code { ("`", "`") } else { ("", "") };
33 for section in 0..24 {
34 let _ = writeln!(out, "## Section {section}: what the data shows\n");
35 for paragraph in 0..4 {
36 let _ = writeln!(
37 out,
38 "Paragraph {paragraph} of section {section}. The **dip window** is {c}08-18{s} → {c}08-26{s} \
39 (trough $208.48 on 08-24), followed by a post-earnings jump. Let me confirm the \
40 earnings date and date the specific news events in that window, then weave in \
41 [the filing](https://example.com/filing/{section}) and the *guidance* update so \
42 the numbers line up with what the market priced in before the open.\n"
43 );
44 }
45 let _ = writeln!(
46 out,
47 "- FOMC 09-16: +25bp to {c}3.75–4.00%{s}, 12-0. First hike since July 2023."
48 );
49 let _ = writeln!(
50 out,
51 "- Dot plot: one more hike implied this year; 2026 projection 4.1%."
52 );
53 let _ = writeln!(out, "- XLE +45.2% YTD, the standout winner; XLU flat.\n");
54 if section % 6 == 0 {
55 let _ = writeln!(
56 out,
57 "```toml\n[dependencies]\ngpui-kit = \"0.6\"\nserde = {{ version = \"1\", features = [\"derive\"] }}\n```\n"
58 );
59 }
60 }
61 out
62}
63
64struct Chat {
65 /// The parsed answer, held the way a chat holds a streamed message.
66 answer: Entity<TextViewState>,
67 scroll: ScrollHandle,
68}
69
70impl Render for Chat {
71 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
72 div().size_full().child(
73 div()
74 .id("viewport")
75 .size_full()
76 .overflow_y_scroll()
77 .track_scroll(&self.scroll)
78 .child(
79 div()
80 .w_full()
81 .px(px(20.))
82 .child(TextView::new(&self.answer)),
83 ),
84 )
85 }
86}
87
88#[gpui::bench]
89fn text_view_scroll(cx: &mut BenchAppContext) {
90 scroll_answer(markdown(), cx);
91}
92
93#[gpui::bench]
94fn text_view_scroll_inline_code(cx: &mut BenchAppContext) {
95 scroll_answer(markdown_with_inline_code(), cx);
96}
97
98fn scroll_answer(markdown: String, cx: &mut BenchAppContext) {
99 cx.update(gpui_base::init);
100 let mut window = cx.add_empty_window();
101 let chat = window.update(|window, cx| {
102 window.replace_root(cx, |_, cx| Chat {
103 answer: cx.new(|cx| TextViewState::markdown(&markdown, cx)),
104 scroll: ScrollHandle::new(),
105 })
106 });
107 // The Markdown parse is a background task; scroll only once it is in.
108 cx.run_until_idle();
109
110 // A steady swipe: 40px a frame, wrapping back to the top at the end, so
111 // every frame moves the visible paragraphs and reuses the shaped text of
112 // the ones still on screen.
113 let mut offset = px(0.);
114 cx.bench_renderer(chat, move |chat, _, cx| {
115 let max = chat.scroll.max_offset().y;
116 offset += px(40.);
117 if offset > max {
118 offset = px(0.);
119 }
120 chat.scroll.set_offset(point(px(0.), -offset));
121 cx.notify();
122 });
123}
124
125gpui::bench_group!(benches, text_view_scroll, text_view_scroll_inline_code);
126gpui::bench_main!(benches);