diff options
author | Christian Duerr <chrisduerr@users.noreply.github.com> | 2018-09-24 18:40:09 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-24 18:40:09 +0000 |
commit | 3d7e88e8a975f40996aaa71d951842db6f2fccbb (patch) | |
tree | c49ee457634a613bf84917fb0a8b89db18a9aa33 /src/grid/mod.rs | |
parent | a752066bfa281f236b8d171cbc8f6e5a5a940297 (diff) | |
download | r-alacritty-3d7e88e8a975f40996aaa71d951842db6f2fccbb.tar.gz r-alacritty-3d7e88e8a975f40996aaa71d951842db6f2fccbb.tar.bz2 r-alacritty-3d7e88e8a975f40996aaa71d951842db6f2fccbb.zip |
Dynamically initialize grid storage
Previously Alacritty has initialized all lines in the buffer as soon as
it is started. This had the effect that terminals which aren't making
use of the scrollback buffer yet, would still consume large amounts of
memory, potentially even freezing the system at startup.
To resolve this problem, the grid is now dynamically resized in chunks
of `1000` rows. The initial size is just the visible area itself, then
every time lines are written to the terminal emulator, the grid storage
is grown when required.
With the worst-case scenario of having 100_000 lines scrollback
configured, this change improves startup performance at the cost of
scrolling performance.
On my machine the startup changes from ~0.3 to ~0.2 seconds.
The scrolling performance with large throughput is not affected, however
it is slowed down when the number of lines scrolled are close to the
100_000 configured as scrollback. The most taxing benchmark I've found
for this was running `yes | dd count=500 > 500.txt` (note the relatively
small file size). This will cause a slowdown on the first run from 0.05s
to 0.15s. While this is significant, it lines up with the time saved at
startup.
This fixes #1236.
Diffstat (limited to 'src/grid/mod.rs')
-rw-r--r-- | src/grid/mod.rs | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 680aa7bd..b8feb421 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -29,6 +29,8 @@ mod tests; mod storage; use self::storage::Storage; +const MIN_INIT_SIZE: usize = 1_000; + /// Bidirection iterator pub trait BidirectionalIterator: Iterator { fn prev(&mut self) -> Option<Self::Item>; @@ -92,6 +94,9 @@ pub struct Grid<T> { /// Selected region #[serde(skip)] pub selection: Option<Selection>, + + #[serde(default)] + max_scroll_limit: usize, } pub struct GridIterator<'a, T: 'a> { @@ -113,7 +118,7 @@ pub enum Scroll { impl<T: Copy + Clone> Grid<T> { pub fn new(lines: index::Line, cols: index::Column, scrollback: usize, template: T) -> Grid<T> { - let raw = Storage::with_capacity(*lines + scrollback, lines, Row::new(cols, &template)); + let raw = Storage::with_capacity(lines, Row::new(cols, &template)); Grid { raw, cols, @@ -121,6 +126,7 @@ impl<T: Copy + Clone> Grid<T> { display_offset: 0, scroll_limit: 0, selection: None, + max_scroll_limit: scrollback, } } @@ -206,11 +212,19 @@ impl<T: Copy + Clone> Grid<T> { } } - fn increase_scroll_limit(&mut self, count: usize) { - self.scroll_limit = min( - self.scroll_limit + count, - self.raw.len().saturating_sub(*self.lines), - ); + fn increase_scroll_limit(&mut self, count: usize, template: &T) + { + self.scroll_limit = min(self.scroll_limit + count, self.max_scroll_limit); + + // Initialize new lines when the history buffer is smaller than the scroll limit + let history_size = self.raw.len().saturating_sub(*self.lines); + if history_size < self.scroll_limit { + let new = min( + max(self.scroll_limit - history_size, MIN_INIT_SIZE), + self.max_scroll_limit - history_size, + ); + self.raw.initialize(new, Row::new(self.cols, template)); + } } fn decrease_scroll_limit(&mut self, count: usize) { @@ -356,7 +370,7 @@ impl<T: Copy + Clone> Grid<T> { ); } - self.increase_scroll_limit(*positions); + self.increase_scroll_limit(*positions, template); // Rotate the entire line buffer. If there's a scrolling region // active, the bottom lines are restored in the next step. |