vercel / turborepo

Build system optimized for JavaScript and TypeScript, written in Rust
https://turbo.build/repo/docs
MIT License
26.32k stars 1.82k forks source link

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

Open chris-olszewski opened 1 month ago

chris-olszewski commented 1 month ago

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:

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[bot] commented 1 month ago

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