-
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
4 changed files
with
125 additions
and
10 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
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,74 @@ | ||
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>, | ||
cols: usize, | ||
rows: usize, | ||
} | ||
|
||
impl<T> Vec2D<T> { | ||
/// Initialize a grid of size `rows * cols` with the given data element. | ||
pub fn init(rows: usize, cols: usize, data: T) -> Vec2D<T> | ||
where | ||
T: Clone, | ||
{ | ||
Vec2D { | ||
data: vec![data; rows * cols], | ||
cols, | ||
rows, | ||
} | ||
} | ||
|
||
/// 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, idx: usize) -> &Self::Output { | ||
if idx < self.rows { | ||
let start_idx = idx * self.cols; | ||
&self.data[start_idx..start_idx + self.cols] | ||
} else { | ||
panic!("row index {:?} out of bounds.", idx); | ||
} | ||
} | ||
} | ||
|
||
impl<T> IndexMut<usize> for Vec2D<T> { | ||
fn index_mut(&mut self, idx: usize) -> &mut Self::Output { | ||
&mut self.data[(idx * self.cols)..] | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn vec2d_fill() { | ||
let mut vec2d = Vec2D::init(3, 3, 0); | ||
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], | ||
rows: 3, | ||
cols: 3, | ||
}; | ||
assert_eq!(vec2d[0], [0, 1, 2]); | ||
assert_eq!(vec2d[1], [3, 4, 5]); | ||
} | ||
} |