From 4b1a3b1e929bbf580de7106a688043bb44523a7f Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Fri, 18 May 2018 20:44:54 -0700 Subject: [PATCH] Optimize Storage::swap_lines Saves a few cycles in a *very* hot function. --- src/grid/storage.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/grid/storage.rs b/src/grid/storage.rs index 885cb94c..cadd4e29 100644 --- a/src/grid/storage.rs +++ b/src/grid/storage.rs @@ -176,13 +176,10 @@ impl Storage { (requested + self.zero) % self.inner.len() } - fn compute_line_index(&self, requested: Line) -> usize { - ((self.inner.len() + self.zero + *self.visible_lines) - *requested) % self.inner.len() - } - pub fn swap_lines(&mut self, a: Line, b: Line) { - let a = self.compute_line_index(a); - let b = self.compute_line_index(b); + let offset = self.inner.len() + self.zero + *self.visible_lines; + let a = (offset - *a) % self.inner.len(); + let b = (offset - *b) % self.inner.len(); self.inner.swap(a, b); } @@ -191,9 +188,10 @@ impl Storage { } pub fn rotate(&mut self, count: isize) { + debug_assert!(count.abs() as usize <= self.inner.len()); + let len = self.inner.len(); - assert!(count.abs() as usize <= len); - self.zero += (count + len as isize) as usize % len; + self.zero = (self.zero as isize + count + len as isize) as usize % len; } // Fast path