Skip to content

Commit

Permalink
performance(terminal): improvements to reflow performance by removing…
Browse files Browse the repository at this point in the history
… O(n^2) behavior (zellij-org#3045)

* refactor: Simplify transfer_rows_from_viewport_to_lines_above

next_lines is always consolidated to a single Row, which immediately
gets removed - we can remove some dead code as a result

* perf: Batch remove rows from the viewport for performance

Given a 1MB line catted into the terminal, a toggle-fullscreen +
toggle-fullscreen + close-pane + `run true` goes from ~9s to ~3s

* perf: Optimize Row::drain_until by splitting chars in one step

Given a 10MB line catted into the terminal, a toggle-fullscreen +
toggle-fullscreen + close-pane + `run true` goes from ~23s to ~20s
  • Loading branch information
aidanhs authored and xuanyuan300 committed Jan 23, 2024
1 parent 45eca37 commit 6283a48
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions zellij-server/src/panes/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,37 +166,24 @@ fn transfer_rows_from_viewport_to_lines_above(
count: usize,
max_viewport_width: usize,
) -> isize {
let mut next_lines: Vec<Row> = vec![];
let mut transferred_rows_count: isize = 0;
for _ in 0..count {
if next_lines.is_empty() {
if !viewport.is_empty() {
let next_line = viewport.remove(0);
transferred_rows_count +=
calculate_row_display_height(next_line.width(), max_viewport_width) as isize;
if !next_line.is_canonical {
let mut bottom_canonical_row_and_wraps_in_dst =
get_lines_above_bottom_canonical_row_and_wraps(lines_above);
next_lines.append(&mut bottom_canonical_row_and_wraps_in_dst);
}
next_lines.push(next_line);
next_lines = vec![Row::from_rows(next_lines)];
} else {
break; // no more rows
}
}
let dropped_line_width = bounded_push(lines_above, sixel_grid, next_lines.remove(0));
let drained_lines = std::cmp::min(count, viewport.len());
for next_line in viewport.drain(..drained_lines) {
let mut next_lines: Vec<Row> = vec![];
transferred_rows_count +=
calculate_row_display_height(next_line.width(), max_viewport_width) as isize;
if !next_line.is_canonical {
let mut bottom_canonical_row_and_wraps_in_dst =
get_lines_above_bottom_canonical_row_and_wraps(lines_above);
next_lines.append(&mut bottom_canonical_row_and_wraps_in_dst);
}
next_lines.push(next_line);
let dropped_line_width = bounded_push(lines_above, sixel_grid, Row::from_rows(next_lines));
if let Some(width) = dropped_line_width {
transferred_rows_count -=
calculate_row_display_height(width, max_viewport_width) as isize;
}
}
if !next_lines.is_empty() {
let excess_rows = Row::from_rows(next_lines).split_to_rows_of_length(max_viewport_width);
for row in excess_rows {
viewport.insert(0, row);
}
}
transferred_rows_count
}

Expand Down Expand Up @@ -3379,19 +3366,20 @@ impl Row {
self.width = None;
}
pub fn drain_until(&mut self, x: usize) -> VecDeque<TerminalCharacter> {
let mut drained_part: VecDeque<TerminalCharacter> = VecDeque::new();
let mut drained_part_len = 0;
while let Some(next_character) = self.columns.remove(0) {
let mut split_pos = 0;
for next_character in self.columns.iter() {
// drained_part_len == 0 here is so that if the grid is resized
// to a size of 1, we won't drop wide characters
if drained_part_len + next_character.width <= x || drained_part_len == 0 {
drained_part.push_back(next_character);
drained_part_len += next_character.width;
split_pos += 1
} else {
self.columns.push_front(next_character); // put it back
break;
}
}
// Can't use split_off because it doesn't reduce capacity, causing OOM with long lines
let drained_part = self.columns.drain(..split_pos).collect();
self.width = None;
drained_part
}
Expand Down

0 comments on commit 6283a48

Please sign in to comment.