-
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.
Reduce unnecessary allocations and indirections
* Changed literal_probs array from a Vec<Vec<u16>> to a Vec2D backed by a contiguous allocation * BitTrees in LenDecoder and DecoderState are now stored inline. The actual BitTree data still lives in a Vec but one level of indirection is reduced. * Don't bother with filling stack-allocated DecoderState arrays on reset, and just recreate the arrays dropping the existing ones.
- Loading branch information
Showing
5 changed files
with
216 additions
and
42 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,137 @@ | ||
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: Box<[T]>, | ||
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].into_boxed_slice(), | ||
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]; | ||
|
||
#[inline] | ||
fn index(&self, row: usize) -> &Self::Output { | ||
let start_row = row * self.cols; | ||
&self.data[start_row..start_row + self.cols] | ||
} | ||
} | ||
|
||
impl<T> IndexMut<usize> for Vec2D<T> { | ||
#[inline] | ||
fn index_mut(&mut self, row: usize) -> &mut Self::Output { | ||
let start_row = row * self.cols; | ||
&mut self.data[start_row..start_row + self.cols] | ||
} | ||
} | ||
|
||
#[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].into_boxed_slice(), | ||
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].into_boxed_slice(), | ||
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; | ||
} | ||
} |