Skip to content

Commit d11922e

Browse files
Shnatselkornelski
authored andcommitted
Optimize index table lookups by giving the compiler info about lengths and keeping the table as a vec of values rather than pointers to values
1 parent 93baf7d commit d11922e

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/lossless_transform.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,12 @@ pub(crate) fn apply_color_indexing_transform(
387387
table_data: &[u8],
388388
) {
389389
if table_size > 16 {
390-
let mut table = table_data.chunks_exact(4).collect::<Vec<_>>();
391-
table.resize(256, &[0; 4]);
390+
let mut table: Vec<[u8; 4]> = table_data.chunks_exact(4).map(|c| TryInto::<[u8; 4]>::try_into(c).unwrap()).collect();
391+
table.resize(256, [0; 4]);
392+
let table: &[[u8; 4]; 256] = table.as_slice().try_into().unwrap();
392393

393394
for pixel in image_data.chunks_exact_mut(4) {
394-
pixel.copy_from_slice(table[pixel[1] as usize]);
395+
pixel.copy_from_slice(&table[pixel[1] as usize]);
395396
}
396397
} else {
397398
let width_bits: u8 = if table_size <= 2 {

0 commit comments

Comments
 (0)