1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 13:52:55 +03:00

use rangeset to streamline line fetches

This commit is contained in:
Wez Furlong 2020-01-05 01:14:38 -08:00
parent 01f3adc2cb
commit cd2b57cc51
4 changed files with 29 additions and 29 deletions

View File

@ -83,6 +83,12 @@ pub fn range_union<T: Integer>(r1: Range<T>, r2: Range<T>) -> Range<T> {
} }
} }
impl<T: Integer + Copy + Debug> Into<Vec<Range<T>>> for RangeSet<T> {
fn into(self) -> Vec<Range<T>> {
self.ranges
}
}
impl<T: Integer + Copy + Debug> RangeSet<T> { impl<T: Integer + Copy + Debug> RangeSet<T> {
/// Create a new set /// Create a new set
pub fn new() -> Self { pub fn new() -> Self {

View File

@ -428,14 +428,13 @@ pub struct GetTabRenderChangesResponse {
#[derive(Deserialize, Serialize, PartialEq, Debug)] #[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct GetLines { pub struct GetLines {
pub tab_id: TabId, pub tab_id: TabId,
pub lines: Range<StableRowIndex>, pub lines: Vec<Range<StableRowIndex>>,
} }
#[derive(Deserialize, Serialize, PartialEq, Debug)] #[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct GetLinesResponse { pub struct GetLinesResponse {
pub tab_id: TabId, pub tab_id: TabId,
pub first_row: StableRowIndex, pub lines: Vec<(StableRowIndex, Line)>,
pub lines: Vec<Line>,
} }
#[cfg(test)] #[cfg(test)]

View File

@ -708,11 +708,18 @@ impl<S: ReadAndWrite> ClientSession<S> {
.ok_or_else(|| anyhow!("no such tab {}", tab_id))?; .ok_or_else(|| anyhow!("no such tab {}", tab_id))?;
let mut renderer = tab.renderer(); let mut renderer = tab.renderer();
let (first_row, lines) = renderer.get_lines(lines); let mut lines_and_indices = vec![];
for range in lines {
let (first_row, lines) = renderer.get_lines(range);
for (idx, line) in lines.into_iter().enumerate() {
let stable_row = first_row + idx as StableRowIndex;
lines_and_indices.push((stable_row, line));
}
}
Ok(Pdu::GetLinesResponse(GetLinesResponse { Ok(Pdu::GetLinesResponse(GetLinesResponse {
tab_id, tab_id,
first_row, lines: lines_and_indices,
lines,
})) }))
}) })
} }

View File

@ -339,7 +339,7 @@ impl RenderableInner {
fn apply_changes_to_surface(&mut self, delta: GetTabRenderChangesResponse) { fn apply_changes_to_surface(&mut self, delta: GetTabRenderChangesResponse) {
self.poll_interval = BASE_POLL_INTERVAL; self.poll_interval = BASE_POLL_INTERVAL;
let mut to_fetch = delta.dimensions.physical_top..delta.dimensions.physical_top; let mut to_fetch = RangeSet::new();
for r in delta.dirty_lines { for r in delta.dirty_lines {
log::error!("apply changes: marking {:?} dirty", r); log::error!("apply changes: marking {:?} dirty", r);
self.dirty_rows.add_range(r.clone()); self.dirty_rows.add_range(r.clone());
@ -349,8 +349,7 @@ impl RenderableInner {
// If it is outside that region, remove it from our cache // If it is outside that region, remove it from our cache
// so that we'll fetch it on demand later. // so that we'll fetch it on demand later.
if idx >= delta.dimensions.physical_top { if idx >= delta.dimensions.physical_top {
to_fetch.start = to_fetch.start.min(idx); to_fetch.add(idx);
to_fetch.end = to_fetch.end.max(idx + 1);
} else { } else {
self.lines.pop(&idx); self.lines.pop(&idx);
} }
@ -367,10 +366,7 @@ impl RenderableInner {
to_fetch to_fetch
); );
to_fetch = range_union( to_fetch.add(delta.cursor_position.y);
to_fetch.clone(),
delta.cursor_position.y..delta.cursor_position.y + 1,
);
} }
self.cursor_position = delta.cursor_position; self.cursor_position = delta.cursor_position;
self.dimensions = delta.dimensions; self.dimensions = delta.dimensions;
@ -379,20 +375,13 @@ impl RenderableInner {
self.fetch_lines(to_fetch); self.fetch_lines(to_fetch);
} }
fn fetch_lines(&mut self, to_fetch: Range<StableRowIndex>) { fn fetch_lines(&mut self, mut to_fetch: RangeSet<StableRowIndex>) {
if to_fetch.start == to_fetch.end { to_fetch.remove_set(&self.fetch_pending);
if to_fetch.is_empty() {
return; return;
} }
let mut fetch_pending = RangeSet::new(); self.fetch_pending.add_set(&to_fetch);
fetch_pending.add_range(to_fetch.clone());
fetch_pending.remove_set(&self.fetch_pending);
if fetch_pending.is_empty() {
return;
}
self.fetch_pending.add_range(to_fetch.clone());
let local_tab_id = self.local_tab_id; let local_tab_id = self.local_tab_id;
log::error!( log::error!(
@ -404,7 +393,7 @@ impl RenderableInner {
.client .client
.get_lines(GetLines { .get_lines(GetLines {
tab_id: self.remote_tab_id, tab_id: self.remote_tab_id,
lines: to_fetch, lines: to_fetch.into(),
}) })
.then(move |result| { .then(move |result| {
match result { match result {
@ -418,8 +407,7 @@ impl RenderableInner {
let renderable = client_tab.renderable.borrow_mut(); let renderable = client_tab.renderable.borrow_mut();
let mut inner = renderable.inner.borrow_mut(); let mut inner = renderable.inner.borrow_mut();
log::error!("got {} lines", result.lines.len()); log::error!("got {} lines", result.lines.len());
for (idx, line) in result.lines.into_iter().enumerate() { for (stable_row, line) in result.lines.into_iter() {
let stable_row = result.first_row + idx as StableRowIndex;
inner.lines.put(stable_row, line); inner.lines.put(stable_row, line);
inner.dirty_rows.add(stable_row); inner.dirty_rows.add(stable_row);
inner.fetch_pending.remove(stable_row); inner.fetch_pending.remove(stable_row);
@ -479,7 +467,7 @@ impl Renderable for RenderableState {
fn get_lines(&mut self, lines: Range<StableRowIndex>) -> (StableRowIndex, Vec<Line>) { fn get_lines(&mut self, lines: Range<StableRowIndex>) -> (StableRowIndex, Vec<Line>) {
let mut inner = self.inner.borrow_mut(); let mut inner = self.inner.borrow_mut();
let mut result = vec![]; let mut result = vec![];
let mut to_fetch = 0..0; let mut to_fetch = RangeSet::new();
for idx in lines.clone() { for idx in lines.clone() {
match inner.lines.get(&idx) { match inner.lines.get(&idx) {
Some(line) => { Some(line) => {
@ -488,7 +476,7 @@ impl Renderable for RenderableState {
inner.dirty_rows.remove(idx); inner.dirty_rows.remove(idx);
} }
None => { None => {
to_fetch = range_union(to_fetch, idx..idx + 1); to_fetch.add(idx);
result.push(Line::with_width(inner.dimensions.cols)); result.push(Line::with_width(inner.dimensions.cols));
} }
} }