Make use of `unlikely` intrinsic

There's some bounds checks we do that panic if the condition is ever
true.
This commit is contained in:
Joe Wilm 2016-09-18 18:17:33 -07:00
parent b6f7b39c0d
commit 7dd176fea8
3 changed files with 16 additions and 11 deletions

View File

@ -145,17 +145,19 @@ impl<T> Grid<T> {
/// better error messages by doing the bounds checking ourselves.
#[inline]
pub fn swap_lines(&mut self, src: index::Line, dst: index::Line) {
// check that src/dst are in bounds. Since index::Line newtypes usize,
// we can assume values are positive.
if src >= self.lines {
panic!("swap_lines src out of bounds; len={}, src={}", self.raw.len(), src);
}
if dst >= self.lines {
panic!("swap_lines dst out of bounds; len={}, dst={}", self.raw.len(), dst);
}
use std::intrinsics::unlikely;
unsafe {
// check that src/dst are in bounds. Since index::Line newtypes usize,
// we can assume values are positive.
if unlikely(src >= self.lines) {
panic!("swap_lines src out of bounds; len={}, src={}", self.raw.len(), src);
}
if unlikely(dst >= self.lines) {
panic!("swap_lines dst out of bounds; len={}, dst={}", self.raw.len(), dst);
}
let src: *mut _ = self.raw.get_unchecked_mut(src.0);
let dst: *mut _ = self.raw.get_unchecked_mut(dst.0);

View File

@ -21,6 +21,7 @@
#![feature(step_trait)]
#![feature(custom_derive, plugin)]
#![plugin(serde_macros)]
#![feature(core_intrinsics)]
extern crate cgmath;
extern crate errno;

View File

@ -467,8 +467,10 @@ impl ansi::Handler for Term {
self.cursor.col = Column(0);
}
if self.cursor.line == self.grid.num_lines() {
panic!("cursor fell off grid");
unsafe {
if ::std::intrinsics::unlikely(self.cursor.line == self.grid.num_lines()) {
panic!("cursor fell off grid");
}
}
let cell = &mut self.grid[&self.cursor];