-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
alloc: Reduce unnecessary Vec allocations and indirections
* Changed literal_probs array from a Vec<Vec<u16>> to a Vec2D backed by a contiguous Vec<u16>. * BitTrees in LenDecoder and DecoderState are now stored inline
- Loading branch information
Showing
5 changed files
with
218 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ mod encode; | |
|
||
pub mod error; | ||
|
||
mod util; | ||
mod xz; | ||
|
||
use std::io; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod vec2d; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
use std::ops::{Index, IndexMut}; | ||
|
||
/// A 2 dimensional matrix in row-major order backed by a contiguous `Vec` | ||
#[derive(Debug)] | ||
pub struct Vec2D<T> { | ||
data: Vec<T>, | ||
rows: usize, | ||
cols: usize, | ||
} | ||
|
||
impl<T> Vec2D<T> { | ||
/// Initialize a grid of size (`rows`, `cols`) with the given data element. | ||
pub fn init(data: T, size: (usize, usize)) -> Vec2D<T> | ||
where | ||
T: Clone, | ||
{ | ||
let (rows, cols) = size; | ||
let len = rows | ||
.checked_mul(cols) | ||
.unwrap_or_else(|| panic!("{} rows by {} cols exceeds usize::MAX", rows, cols)); | ||
Vec2D { | ||
data: vec![data; len], | ||
rows, | ||
cols, | ||
} | ||
} | ||
|
||
/// Fills the grid with elements by cloning `value`. | ||
pub fn fill(&mut self, value: T) | ||
where | ||
T: Clone, | ||
{ | ||
self.data.fill(value) | ||
} | ||
} | ||
|
||
impl<T> Index<usize> for Vec2D<T> { | ||
type Output = [T]; | ||
|
||
fn index(&self, row: usize) -> &Self::Output { | ||
if row < self.rows { | ||
let start_row = row * self.cols; | ||
&self.data[start_row..start_row + self.cols] | ||
} else { | ||
panic!("row index {:?} out of bounds.", row); | ||
} | ||
} | ||
} | ||
|
||
impl<T> IndexMut<usize> for Vec2D<T> { | ||
fn index_mut(&mut self, row: usize) -> &mut Self::Output { | ||
if row < self.rows { | ||
let start_row = row * self.cols; | ||
&mut self.data[start_row..start_row + self.cols] | ||
} else { | ||
panic!("row index {:?} out of bounds.", row); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn vec2d_init() { | ||
let vec2d = Vec2D::init(1, (2, 3)); | ||
assert_eq!(vec2d[0], [1, 1, 1]); | ||
assert_eq!(vec2d[1], [1, 1, 1]); | ||
} | ||
|
||
#[test] | ||
fn vec2d_fill() { | ||
let mut vec2d = Vec2D::init(0, (2, 3)); | ||
vec2d.fill(7); | ||
assert_eq!(vec2d[0], [7, 7, 7]); | ||
assert_eq!(vec2d[1], [7, 7, 7]); | ||
} | ||
|
||
#[test] | ||
fn vec2d_index() { | ||
let vec2d = Vec2D { | ||
data: vec![0, 1, 2, 3, 4, 5, 6, 7, 8], | ||
rows: 3, | ||
cols: 3, | ||
}; | ||
assert_eq!(vec2d[0], [0, 1, 2]); | ||
assert_eq!(vec2d[1], [3, 4, 5]); | ||
assert_eq!(vec2d[2], [6, 7, 8]); | ||
} | ||
|
||
#[test] | ||
fn vec2d_index_mut() { | ||
let mut vec2d = Vec2D { | ||
data: vec![0, 1, 2, 3, 4, 5, 6, 7, 8], | ||
rows: 3, | ||
cols: 3, | ||
}; | ||
|
||
vec2d[1][1] = 9; | ||
assert_eq!(vec2d[0], [0, 1, 2]); | ||
assert_eq!(vec2d[1], [3, 9, 5]); | ||
assert_eq!(vec2d[2], [6, 7, 8]); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn vec2d_index_out_of_bounds() { | ||
let vec2d = Vec2D::init(1, (2, 3)); | ||
let _x = vec2d[2][4]; | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn vec2d_index_out_of_bounds_vec_edge() { | ||
let vec2d = Vec2D::init(1, (2, 3)); | ||
let _x = vec2d[1][3]; | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn vec2d_index_out_of_bounds_overflow() { | ||
let vec2d = Vec2D::init(1, (2, 3)); | ||
let _x = vec2d[0][3]; | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn vec2d_indexmut_out_of_bounds_vec_edge() { | ||
let mut vec2d = Vec2D::init(1, (2, 3)); | ||
vec2d[1][3] = 0; | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn vec2d_indexmut_out_of_bounds_overflow() { | ||
let mut vec2d = Vec2D::init(1, (2, 3)); | ||
vec2d[0][3] = 0; | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn vec2d_indexmut_out_of_bounds() { | ||
let mut vec2d = Vec2D::init(1, (2, 3)); | ||
vec2d[2][4] = 0; | ||
} | ||
} |