make semantic zones more cacheable
This commit decomposes the main get_semantic_zones method into two parts: * A per-line portion, where the line ranges are cached (invalidated on change) * The overall screen portion, where the line ranges are merged This changes the overall complexity of computing zones from O(width * scrollback-height) To an incremental: O((width * number of changed lines since last query) + scrollback-height) You can see some samples of elapsed time below; those show the times for running both the old and the new implementation on the same data. The number of lines/zones in the scrollback increases with each call and you can see that the new implementation is a bit faster anyway at low volumes but is significantly faster as the number of lines/zones increases, because the amount of work is reduced. ``` get_semantic_zones: 71.708µs get_semantic_zones_new: 59.041µs get_semantic_zones: 71.166µs get_semantic_zones_new: 9.166µs get_semantic_zones: 44.291µs get_semantic_zones_new: 4.208µs get_semantic_zones: 69.791µs get_semantic_zones_new: 10.291µs get_semantic_zones: 59.375µs get_semantic_zones_new: 7.958µs get_semantic_zones: 52.5µs get_semantic_zones_new: 4.5µs get_semantic_zones: 91.791µs get_semantic_zones_new: 20.916µs get_semantic_zones: 229.916µs get_semantic_zones_new: 109.208µs get_semantic_zones: 224.125µs get_semantic_zones_new: 15.208µs get_semantic_zones: 291.791µs get_semantic_zones_new: 11.833µs get_semantic_zones: 238.875µs get_semantic_zones_new: 12.625µs get_semantic_zones: 468.458µs get_semantic_zones_new: 126.583µs get_semantic_zones: 460.5µs get_semantic_zones_new: 25.666µs get_semantic_zones: 358.291µs get_semantic_zones_new: 19.541µs get_semantic_zones: 436.833µs get_semantic_zones_new: 17.875µs get_semantic_zones: 313.166µs get_semantic_zones_new: 15.25µs get_semantic_zones: 333.958µs get_semantic_zones_new: 16.541µs get_semantic_zones: 364.666µs get_semantic_zones_new: 14.041µs ```
This commit is contained in:
1 parent
ebef7dc659
commit
e188406dfc
3 files changed
+115
-35
No files matched your search
@@ -439,7 +439,7 @@ impl Pane for LocalPane {
|
||||
}
|
||||
|
||||
fn get_semantic_zones(&self) -> anyhow::Result<Vec<SemanticZone>> {
|
||||
let term = self.terminal.borrow();
|
||||
let mut term = self.terminal.borrow_mut();
|
||||
term.get_semantic_zones()
|
||||
}
|
||||
|
||||
|
||||
@@ -2389,35 +2389,20 @@ impl TerminalState {
|
||||
///
|
||||
/// By default, all screen data is of type Output. The shell needs to
|
||||
/// employ OSC 133 escapes to markup its output.
|
||||
pub fn get_semantic_zones(&self) -> anyhow::Result<Vec<SemanticZone>> {
|
||||
let screen = self.screen();
|
||||
pub fn get_semantic_zones(&mut self) -> anyhow::Result<Vec<SemanticZone>> {
|
||||
let screen = self.screen_mut();
|
||||
|
||||
let mut last_cell: Option<&Cell> = None;
|
||||
let mut current_zone = None;
|
||||
let mut current_zone: Option<SemanticZone> = None;
|
||||
let mut zones = vec![];
|
||||
let blank_cell = Cell::blank();
|
||||
|
||||
for (idx, line) in screen.lines.iter().enumerate() {
|
||||
let stable_row = screen.phys_to_stable_row_index(idx);
|
||||
let first_stable_row = screen.phys_to_stable_row_index(0);
|
||||
for (idx, line) in screen.lines.iter_mut().enumerate() {
|
||||
let stable_row = first_stable_row + idx as StableRowIndex;
|
||||
|
||||
// Rows may have trailing space+Output cells interleaved
|
||||
// with other zones as a result of clear-to-eol and
|
||||
// clear-to-end-of-screen sequences. We don't want
|
||||
// those to affect the zones that we compute here
|
||||
let last_non_blank = line
|
||||
.cells()
|
||||
.iter()
|
||||
.rposition(|cell| *cell != blank_cell)
|
||||
.unwrap_or(line.cells().len());
|
||||
|
||||
for (grapheme_idx, cell) in line.visible_cells() {
|
||||
if grapheme_idx > last_non_blank {
|
||||
break;
|
||||
}
|
||||
let semantic_type = cell.attrs().semantic_type();
|
||||
let new_zone = match last_cell {
|
||||
for zone_range in line.semantic_zone_ranges() {
|
||||
let new_zone = match current_zone.as_ref() {
|
||||
None => true,
|
||||
Some(c) => c.attrs().semantic_type() != semantic_type,
|
||||
Some(zone) => zone.semantic_type != zone_range.semantic_type,
|
||||
};
|
||||
|
||||
if new_zone {
|
||||
@@ -2426,20 +2411,18 @@ impl TerminalState {
|
||||
}
|
||||
|
||||
current_zone.replace(SemanticZone {
|
||||
start_x: grapheme_idx as _,
|
||||
start_x: zone_range.range.start as usize,
|
||||
start_y: stable_row,
|
||||
end_x: grapheme_idx as _,
|
||||
end_x: zone_range.range.end as usize,
|
||||
end_y: stable_row,
|
||||
semantic_type: semantic_type,
|
||||
semantic_type: zone_range.semantic_type,
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(zone) = current_zone.as_mut() {
|
||||
zone.end_x = grapheme_idx as _;
|
||||
zone.end_x = zone_range.range.end as usize;
|
||||
zone.end_y = stable_row;
|
||||
}
|
||||
|
||||
last_cell.replace(cell);
|
||||
}
|
||||
}
|
||||
if let Some(zone) = current_zone.take() {
|
||||
|
||||
+101
-4
@@ -1,4 +1,4 @@
|
||||
use crate::cell::{Cell, CellAttributes};
|
||||
use crate::cell::{Cell, CellAttributes, SemanticType};
|
||||
use crate::cellcluster::CellCluster;
|
||||
use crate::hyperlink::Rule;
|
||||
use crate::surface::{Change, SequenceNo, SEQ_ZERO};
|
||||
@@ -45,10 +45,18 @@ bitflags! {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ZoneRange {
|
||||
pub semantic_type: SemanticType,
|
||||
pub range: Range<u16>,
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Line {
|
||||
cells: Vec<Cell>,
|
||||
zones: Vec<ZoneRange>,
|
||||
seqno: SequenceNo,
|
||||
bits: LineBits,
|
||||
}
|
||||
@@ -64,19 +72,34 @@ impl Line {
|
||||
let mut cells = Vec::with_capacity(width);
|
||||
cells.resize(width, cell.clone());
|
||||
let bits = LineBits::NONE;
|
||||
Self { bits, cells, seqno }
|
||||
Self {
|
||||
bits,
|
||||
cells,
|
||||
seqno,
|
||||
zones: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_cells(cells: Vec<Cell>, seqno: SequenceNo) -> Self {
|
||||
let bits = LineBits::NONE;
|
||||
Self { bits, cells, seqno }
|
||||
Self {
|
||||
bits,
|
||||
cells,
|
||||
seqno,
|
||||
zones: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_width(width: usize, seqno: SequenceNo) -> Self {
|
||||
let mut cells = Vec::with_capacity(width);
|
||||
cells.resize_with(width, Cell::blank);
|
||||
let bits = LineBits::NONE;
|
||||
Self { bits, cells, seqno }
|
||||
Self {
|
||||
bits,
|
||||
cells,
|
||||
seqno,
|
||||
zones: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_text(s: &str, attrs: &CellAttributes, seqno: SequenceNo) -> Line {
|
||||
@@ -95,6 +118,7 @@ impl Line {
|
||||
cells,
|
||||
bits: LineBits::NONE,
|
||||
seqno,
|
||||
zones: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,12 +147,14 @@ impl Line {
|
||||
.resize_with(width, || Cell::blank_with_attrs(blank_attr.clone()));
|
||||
self.cells.shrink_to_fit();
|
||||
self.update_last_change_seqno(seqno);
|
||||
self.invalidate_zones();
|
||||
self.bits = LineBits::NONE;
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, width: usize, seqno: SequenceNo) {
|
||||
self.cells.resize_with(width, Cell::blank);
|
||||
self.update_last_change_seqno(seqno);
|
||||
self.invalidate_zones();
|
||||
}
|
||||
|
||||
/// Wrap the line so that it fits within the provided width.
|
||||
@@ -145,6 +171,7 @@ impl Line {
|
||||
cells: chunk.to_vec(),
|
||||
bits: LineBits::NONE,
|
||||
seqno: seqno,
|
||||
zones: vec![],
|
||||
};
|
||||
if line.cells.len() == width {
|
||||
// Ensure that we don't forget that we wrapped
|
||||
@@ -264,6 +291,68 @@ impl Line {
|
||||
self.update_last_change_seqno(seqno);
|
||||
}
|
||||
|
||||
fn invalidate_zones(&mut self) {
|
||||
self.zones.clear();
|
||||
}
|
||||
|
||||
fn compute_zones(&mut self) {
|
||||
let blank_cell = Cell::blank();
|
||||
let mut last_cell: Option<&Cell> = None;
|
||||
let mut current_zone: Option<ZoneRange> = None;
|
||||
let mut zones = vec![];
|
||||
|
||||
// Rows may have trailing space+Output cells interleaved
|
||||
// with other zones as a result of clear-to-eol and
|
||||
// clear-to-end-of-screen sequences. We don't want
|
||||
// those to affect the zones that we compute here
|
||||
let last_non_blank = self
|
||||
.cells()
|
||||
.iter()
|
||||
.rposition(|cell| *cell != blank_cell)
|
||||
.unwrap_or(self.cells().len());
|
||||
|
||||
for (grapheme_idx, cell) in self.visible_cells() {
|
||||
if grapheme_idx > last_non_blank {
|
||||
break;
|
||||
}
|
||||
let grapheme_idx = grapheme_idx as u16;
|
||||
let semantic_type = cell.attrs().semantic_type();
|
||||
let new_zone = match last_cell {
|
||||
None => true,
|
||||
Some(c) => c.attrs().semantic_type() != semantic_type,
|
||||
};
|
||||
|
||||
if new_zone {
|
||||
if let Some(zone) = current_zone.take() {
|
||||
zones.push(zone);
|
||||
}
|
||||
|
||||
current_zone.replace(ZoneRange {
|
||||
range: grapheme_idx..grapheme_idx + 1,
|
||||
semantic_type,
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(zone) = current_zone.as_mut() {
|
||||
zone.range.end = grapheme_idx;
|
||||
}
|
||||
|
||||
last_cell.replace(cell);
|
||||
}
|
||||
|
||||
if let Some(zone) = current_zone.take() {
|
||||
zones.push(zone);
|
||||
}
|
||||
self.zones = zones;
|
||||
}
|
||||
|
||||
pub fn semantic_zone_ranges(&mut self) -> &[ZoneRange] {
|
||||
if self.zones.is_empty() {
|
||||
self.compute_zones();
|
||||
}
|
||||
&self.zones
|
||||
}
|
||||
|
||||
/// If we have any cells with an implicit hyperlink, remove the hyperlink
|
||||
/// from the cell attributes but leave the remainder of the attributes alone.
|
||||
pub fn invalidate_implicit_hyperlinks(&mut self, seqno: SequenceNo) {
|
||||
@@ -368,6 +457,7 @@ impl Line {
|
||||
bits: self.bits,
|
||||
cells,
|
||||
seqno,
|
||||
zones: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,6 +561,7 @@ impl Line {
|
||||
}
|
||||
|
||||
self.invalidate_implicit_hyperlinks(seqno);
|
||||
self.invalidate_zones();
|
||||
self.update_last_change_seqno(seqno);
|
||||
if cell.attrs().hyperlink().is_some() {
|
||||
self.bits |= LineBits::HAS_HYPERLINK;
|
||||
@@ -543,6 +634,7 @@ impl Line {
|
||||
|
||||
self.cells.insert(x, cell);
|
||||
self.update_last_change_seqno(seqno);
|
||||
self.invalidate_zones();
|
||||
}
|
||||
|
||||
pub fn erase_cell(&mut self, x: usize, seqno: SequenceNo) {
|
||||
@@ -555,6 +647,7 @@ impl Line {
|
||||
self.cells.remove(x);
|
||||
self.cells.push(Cell::default());
|
||||
self.update_last_change_seqno(seqno);
|
||||
self.invalidate_zones();
|
||||
}
|
||||
|
||||
pub fn remove_cell(&mut self, x: usize, seqno: SequenceNo) {
|
||||
@@ -566,6 +659,7 @@ impl Line {
|
||||
self.invalidate_grapheme_at_or_before(x);
|
||||
self.cells.remove(x);
|
||||
self.update_last_change_seqno(seqno);
|
||||
self.invalidate_zones();
|
||||
}
|
||||
|
||||
pub fn erase_cell_with_margin(
|
||||
@@ -587,6 +681,7 @@ impl Line {
|
||||
.insert(right_margin - 1, Cell::blank_with_attrs(blank_attr));
|
||||
}
|
||||
self.update_last_change_seqno(seqno);
|
||||
self.invalidate_zones();
|
||||
}
|
||||
|
||||
pub fn prune_trailing_blanks(&mut self, seqno: SequenceNo) {
|
||||
@@ -598,6 +693,7 @@ impl Line {
|
||||
{
|
||||
self.cells.resize_with(end_idx + 1, Cell::blank);
|
||||
self.update_last_change_seqno(seqno);
|
||||
self.invalidate_zones();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -672,6 +768,7 @@ impl Line {
|
||||
pub fn append_line(&mut self, mut other: Line, seqno: SequenceNo) {
|
||||
self.cells.append(&mut other.cells);
|
||||
self.update_last_change_seqno(seqno);
|
||||
self.invalidate_zones();
|
||||
}
|
||||
|
||||
/// mutable access the cell data, but the caller must take care
|
||||
|
||||
Reference in new issue
Block a user