Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(vt100) increase render performance for tui_term codepath #9123

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

chris-olszewski
Copy link
Member

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

Copy link

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 Oct 3, 2024 7:12pm
examples-designsystem-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 3, 2024 7:12pm
examples-gatsby-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 3, 2024 7:12pm
examples-kitchensink-blog ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 3, 2024 7:12pm
examples-native-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 3, 2024 7:12pm
examples-nonmonorepo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 3, 2024 7:12pm
examples-svelte-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 3, 2024 7:12pm
examples-tailwind-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 3, 2024 7:12pm
examples-vite-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 3, 2024 7:12pm

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.

3 participants