Skip to content

Commit e9a3fb4

Browse files
authored
Remove bounds checks from color transform hot loop (#133)
1 parent b5159db commit e9a3fb4

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

fuzz/Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lossless_transform.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,19 @@ pub(crate) fn apply_color_transform(
350350
let width = usize::from(width);
351351

352352
for (y, row) in image_data.chunks_exact_mut(width * 4).enumerate() {
353-
for (block_x, block) in row.chunks_mut(4 << size_bits).enumerate() {
354-
let block_index = (y >> size_bits) * block_xsize + block_x;
355-
let red_to_blue = transform_data[block_index * 4];
356-
let green_to_blue = transform_data[block_index * 4 + 1];
357-
let green_to_red = transform_data[block_index * 4 + 2];
353+
let row_transform_data_start = (y >> size_bits) * block_xsize * 4;
354+
// the length of block_tf_data should be `block_xsize * 4`, so we could slice it with [..block_xsize * 4]
355+
// but there is no point - `.zip()` runs until either of the iterators is consumed,
356+
// so the extra slicing operation would be doing more work for no reason
357+
let row_tf_data = &transform_data[row_transform_data_start..];
358+
359+
for (block, transform) in row
360+
.chunks_mut(4 << size_bits)
361+
.zip(row_tf_data.chunks_exact(4))
362+
{
363+
let red_to_blue = transform[0];
364+
let green_to_blue = transform[1];
365+
let green_to_red = transform[2];
358366

359367
for pixel in block.chunks_exact_mut(4) {
360368
let green = u32::from(pixel[1]);

0 commit comments

Comments
 (0)