From 620f83842cafc77d7da0915d7fbac6cac484d9a0 Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Sun, 11 Feb 2018 10:07:33 -0800 Subject: Minor improvements --- src/grid/mod.rs | 18 +++++++++--------- src/grid/storage.rs | 5 +++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 8e30915c..0c84530a 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -179,26 +179,26 @@ impl Grid { self.swap_lines(line, line - positions); } - for i in IndexRange(Line(0)..positions) { - self.raw[*(region.start - i - 1)].reset(&self.template_row); + for line in IndexRange(region.start .. (region.start + positions)) { + self.raw[*line].reset(&self.template_row); } } } + /// scroll_up moves lines at the bottom towards the top + /// + /// This is the performance-sensitive part of scrolling. #[inline] pub fn scroll_up(&mut self, region: &Range, positions: index::Line) { if region.start == Line(0) { // Rotate the entire line buffer. If there's a scrolling region // active, the bottom lines are restored in the next step. - self.raw.rotate(*positions as isize); + self.raw.rotate_up(*positions); // Now, restore any lines outside the scroll region - let mut i = 0; - for _ in IndexRange(region.end .. self.num_lines()) { - let idx = *self.num_lines() - i - 1; + for idx in (*region.end .. *self.num_lines()).rev() { // First do the swap self.raw.swap(idx, idx - *positions); - i += 1; } // Finally, reset recycled lines @@ -214,8 +214,8 @@ impl Grid { } // Clear reused lines - for i in IndexRange(Line(0)..positions) { - self.raw[*(region.start - i - 1)].reset(&self.template_row); + for line in IndexRange((region.end - positions) .. region.end) { + self.raw[*line].reset(&self.template_row); } } } diff --git a/src/grid/storage.rs b/src/grid/storage.rs index f5c2d87e..49f3d26c 100644 --- a/src/grid/storage.rs +++ b/src/grid/storage.rs @@ -68,6 +68,11 @@ impl Storage { assert!(count.abs() as usize <= len); self.zero += (count + len as isize) as usize % len; } + + // Fast path + pub fn rotate_up(&mut self, count: usize) { + self.zero = (self.zero + count) % self.len(); + } } impl Index for Storage { -- cgit v1.2.3-54-g00ecf