-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(vt100): increase render performance for
tui_term
codepath (#9123)
### Description The crate `tui_term` renders the `vt100::Screen` by [fetching each cell](https://github.com/a-kenji/tui-term/blob/development/src/state.rs#L27) and then filling the `ratatui::buffer:Cell` [with the vt100 cell's contents](https://github.com/a-kenji/tui-term/blob/development/src/vt100_imp.rs#L37). This leads to the following functions being called once for each cell on the users terminal: - [`Cell::contents`](https://github.com/vercel/turborepo/blob/main/crates/turborepo-vt100/src/cell.rs#L90) 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`](https://doc.rust-lang.org/src/alloc/string.rs.html#1358) - [`Grid::visible_row`](https://github.com/vercel/turborepo/blob/main/crates/turborepo-vt100/src/grid.rs#L200) 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. ```rust 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 <img width="1329" alt="Screenshot 2024-09-06 at 4 23 47 PM" src="https://github.com/user-attachments/assets/6b5e27c6-31d6-4f71-842b-3efd37fa606e"> After <img width="1327" alt="Screenshot 2024-09-06 at 4 24 32 PM" src="https://github.com/user-attachments/assets/bf966a03-b093-48e3-8848-5d725c22b637"> `visible_cell` codepath Before <img width="1511" alt="Screenshot 2024-09-06 at 4 25 09 PM" src="https://github.com/user-attachments/assets/c0052806-98e7-4d0a-94c9-6f2aff1867e2"> After <img width="1396" alt="Screenshot 2024-09-06 at 4 25 41 PM" src="https://github.com/user-attachments/assets/f1360722-82ea-4ff1-8d5e-2035dc774ccb">
- Loading branch information
1 parent
e0a3297
commit 92d0a5e
Showing
6 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters