From 0853fafe4883b5977849795027f154bae37c139c Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Tue, 1 May 2018 20:07:59 +0200 Subject: [PATCH] Remove `push` from `Storage` Since every line is allocated at startup anyways, the `push` method on `Storage` has been removed and instead of pushing to the vector the initialization has been moved to the `with_capacity` method. This has the advantage that we don't need to keep track of the `len` in push (like adding one), but we just need to worry about growing/shrinking the visible area. --- src/grid/mod.rs | 12 +----------- src/grid/storage.rs | 18 +++++++++++------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 13e6cc00..740c036a 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -114,17 +114,7 @@ pub enum Scroll { impl Grid { pub fn new(lines: index::Line, cols: index::Column, scrollback: usize, template: T) -> Grid { - let mut raw = Storage::with_capacity(*lines + scrollback, lines); - - // Allocate all lines in the buffer, including scrollback history - // - // TODO (jwilm) Allocating each line at this point is expensive and - // delays startup. A nice solution might be having `Row` delay - // allocation until it's actually used. - for _ in 0..raw.len() { - raw.push(Row::new(cols, &template)); - } - + let raw = Storage::with_capacity(*lines + scrollback, lines, Row::new(cols, &template)); Grid { raw, cols, diff --git a/src/grid/storage.rs b/src/grid/storage.rs index 63b30831..ba9a2d41 100644 --- a/src/grid/storage.rs +++ b/src/grid/storage.rs @@ -36,9 +36,18 @@ impl ::std::cmp::PartialEq for Storage { impl Storage { #[inline] - pub fn with_capacity(cap: usize, lines: Line) -> Storage { + pub fn with_capacity(cap: usize, lines: Line, template: T) -> Storage + where + T: Clone, + { + // Allocate all lines in the buffer, including scrollback history + // + // TODO (jwilm) Allocating each line at this point is expensive and + // delays startup. A nice solution might be having `Row` delay + // allocation until it's actually used. + let inner = vec![template; cap]; Storage { - inner: Vec::with_capacity(cap), + inner, zero: 0, visible_lines: lines - 1, len: cap, @@ -89,11 +98,6 @@ impl Storage { self.visible_lines = next - 1; } - #[inline] - pub fn push(&mut self, item: T) { - self.inner.push(item) - } - #[inline] pub fn len(&self) -> usize { self.len