aboutsummaryrefslogtreecommitdiff
path: root/src/grid
diff options
context:
space:
mode:
authorChristian Duerr <contact@christianduerr.com>2018-05-15 22:36:14 +0200
committerJoe Wilm <jwilm@users.noreply.github.com>2018-05-29 21:00:21 -0700
commitf044828755e456b3fc05eabd977d8b5fd95d486f (patch)
treed278963a1835ed5dfaa9f5c40fb9b5d89a2e1e83 /src/grid
parent973c30eb50f720d02c63ffa1f23f44d801eb79d1 (diff)
downloadalacritty-f044828755e456b3fc05eabd977d8b5fd95d486f.tar.gz
alacritty-f044828755e456b3fc05eabd977d8b5fd95d486f.zip
Truncate invisible lines before storing ref-tests
Because there is no good way to store invisible lines in a backwards- and forwards-compatible way, they buffer now gets truncated before dumping the state of a grid when creating a ref-test. This involved a few workaround of which a few required adding additional methods which are only used in ref-tests, these should be minimal though. Since this required the creation of a truncation method anyways, some logic has been added which automatically truncates the invisible buffer when there are more than X (set to 100) invisible lines. This should not impact performance because it rarely occurs, but it could save a bit of memory when the history size is shrunk during runtime (see #1293). This also adds an optional `config.json` file to the ref-test output where it is possible to manually specify variables which should override config defaults, this has been used only for history_size so far. Creating a new ref-test does also still work, so there was no regression here, if history size is altered, the config.json just has to be created manually with the content `{"history_size":HIST_SIZE}`, where `HIST_SIZE` is the desired history size.
Diffstat (limited to 'src/grid')
-rw-r--r--src/grid/mod.rs5
-rw-r--r--src/grid/storage.rs102
2 files changed, 107 insertions, 0 deletions
diff --git a/src/grid/mod.rs b/src/grid/mod.rs
index 803f1e6e..0eea4201 100644
--- a/src/grid/mod.rs
+++ b/src/grid/mod.rs
@@ -407,6 +407,11 @@ impl<T> Grid<T> {
self.raw.len()
}
+ /// This is used only for truncating before saving ref-tests
+ pub fn truncate(&mut self) {
+ self.raw.truncate();
+ }
+
pub fn iter_from(&self, point: Point<usize>) -> GridIterator<T> {
GridIterator {
grid: self,
diff --git a/src/grid/storage.rs b/src/grid/storage.rs
index f59b01b7..885cb94c 100644
--- a/src/grid/storage.rs
+++ b/src/grid/storage.rs
@@ -15,6 +15,9 @@ use std::ops::{Index, IndexMut};
use index::Line;
+/// Maximum number of invisible lines before buffer is resized
+const TRUNCATE_STEP: usize = 100;
+
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Storage<T> {
inner: Vec<T>,
@@ -134,6 +137,32 @@ impl<T> Storage<T> {
// Update visible lines
self.visible_lines = next - 1;
+
+ // Free memory
+ if self.inner.len() > self.len() + TRUNCATE_STEP {
+ self.truncate();
+ }
+ }
+
+ /// Truncate the invisible elements from the raw buffer
+ pub fn truncate(&mut self) {
+ // Calculate shrinkage/offset for indexing
+ let offset = self.zero % self.inner.len();
+ let shrinkage = self.inner.len() - self.len;
+ let shrinkage_start = ::std::cmp::min(offset, shrinkage);
+
+ // Create two vectors with correct ordering
+ let mut split = self.inner.split_off(offset);
+
+ // Truncate the buffers
+ let len = self.inner.len();
+ let split_len = split.len();
+ self.inner.truncate(len - shrinkage_start);
+ split.truncate(split_len - (shrinkage - shrinkage_start));
+
+ // Merge buffers again and reset zero
+ self.zero = self.inner.len();
+ self.inner.append(&mut split);
}
#[inline]
@@ -411,3 +440,76 @@ fn shrink_before_and_after_zero() {
assert_eq!(storage.zero, expected.zero);
assert_eq!(storage.len, expected.len);
}
+
+/// Check that when truncating all hidden lines are removed from the raw buffer
+///
+/// Before:
+/// 0: 4 <- Hidden
+/// 1: 5 <- Hidden
+/// 2: 0 <- Zero
+/// 3: 1
+/// 4: 2 <- Hidden
+/// 5: 3 <- Hidden
+/// After:
+/// 0: 0 <- Zero
+/// 1: 1
+#[test]
+fn truncate_invisible_lines() {
+ // Setup storage area
+ let mut storage = Storage {
+ inner: vec!["4", "5", "0", "1", "2", "3"],
+ zero: 2,
+ visible_lines: Line(1),
+ len: 2,
+ };
+
+ // Truncate buffer
+ storage.truncate();
+
+ // Make sure the result is correct
+ let expected = Storage {
+ inner: vec!["0", "1"],
+ zero: 0,
+ visible_lines: Line(1),
+ len: 2,
+ };
+ assert_eq!(storage.visible_lines, expected.visible_lines);
+ assert_eq!(storage.inner, expected.inner);
+ assert_eq!(storage.zero, expected.zero);
+ assert_eq!(storage.len, expected.len);
+}
+
+/// Truncate buffer only at the beginning
+///
+/// Before:
+/// 0: 1
+/// 1: 2 <- Hidden
+/// 2: 0 <- Zero
+/// After:
+/// 0: 1
+/// 0: 0 <- Zero
+#[test]
+fn truncate_invisible_lines_beginning() {
+ // Setup storage area
+ let mut storage = Storage {
+ inner: vec!["1", "2", "0"],
+ zero: 2,
+ visible_lines: Line(1),
+ len: 2,
+ };
+
+ // Truncate buffer
+ storage.truncate();
+
+ // Make sure the result is correct
+ let expected = Storage {
+ inner: vec!["1", "0"],
+ zero: 1,
+ visible_lines: Line(1),
+ len: 2,
+ };
+ assert_eq!(storage.visible_lines, expected.visible_lines);
+ assert_eq!(storage.inner, expected.inner);
+ assert_eq!(storage.zero, expected.zero);
+ assert_eq!(storage.len, expected.len);
+}