这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@chris-olszewski
Copy link
Contributor

Description

The crate tui_term renders the vt100::Screen by fetching each cell and then filling the ratatui::buffer:Cell with the vt100 cell's contents.

This leads to the following functions being called once for each cell on the users terminal:

  • Cell::contents which unnecessarily allocates on every call even though there's a fixed limit to the size of the str that could be returned. This PR changes this method to return a &str that is constructed from the content bytes. The new helper function append_char is basically String::push
  • Grid::visible_row which iterates through the scrollback rows and screen rows. This PR changes this to instead index directly into the rows

Testing Instructions

Tested by profiling this basic script that render a tui_term 1000 times.

use std::io;

use ratatui::{
    prelude::CrosstermBackend,
    style::{Style, Stylize},
    widgets::{Block, Borders, Widget},
    Terminal,
};
use tui_term::widget::PseudoTerminal;

pub fn main() {
    let input = include_bytes!("turbo-build.log");
    let mut terminal =
        Terminal::new(CrosstermBackend::new(io::stdout())).unwrap();
    terminal.hide_cursor().unwrap();
    let size = terminal.size().unwrap();

    let mut parser =
        turborepo_vt100::Parser::new(size.height, size.width, 1024);
    parser.process(input);

    let screen = parser.screen();

    for _ in 0..1000 {
        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().blue());
        let term = PseudoTerminal::new(screen).block(block);
        terminal
            .draw(|frame| {
                let area = frame.size();
                term.render(area, frame.buffer_mut())
            })
            .unwrap();
    }
}

The two functions that this targets:

fill_buf_cell codepath
Before
Screenshot 2024-09-06 at 4 23 47 PM

After
Screenshot 2024-09-06 at 4 24 32 PM

visible_cell codepath
Before

Screenshot 2024-09-06 at 4 25 09 PM After Screenshot 2024-09-06 at 4 25 41 PM

@vercel
Copy link
Contributor

vercel bot commented Sep 6, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
examples-basic-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 6:53pm
examples-designsystem-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 6:53pm
examples-gatsby-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 6:53pm
examples-kitchensink-blog ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 6:53pm
examples-native-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 6:53pm
examples-nonmonorepo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 6:53pm
examples-svelte-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 6:53pm
examples-tailwind-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 6:53pm
examples-vite-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 15, 2025 6:53pm

@chris-olszewski chris-olszewski marked this pull request as ready for review January 15, 2025 18:52
@chris-olszewski chris-olszewski changed the title perf(vt100) increase render performance for tui_term codepath perf(vt100): increase render performance for tui_term codepath Jan 15, 2025
@chris-olszewski chris-olszewski merged commit 92d0a5e into main Jan 15, 2025
37 checks passed
@chris-olszewski chris-olszewski deleted the olszewski/perf_vt100_minor branch January 15, 2025 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants