Skip to content

Commit 344a04e

Browse files
committed
Optimize data copy in compute_image
Data was copied byte by byte in two nested loops. This changes the code to a single loop, using memcpy to copy one line of data at a time. I observed a 2% performance improvement on the "decode 512x512 image" benchmark on a 2018 Macbook Pro.
1 parent 330725d commit 344a04e

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/decoder.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,9 +788,11 @@ fn compute_image(components: &[Component],
788788
let line_stride = component.block_size.width as usize * component.dct_scale;
789789

790790
for y in 0 .. height {
791-
for x in 0 .. width {
792-
buffer[y * width + x] = data[0][y * line_stride + x];
793-
}
791+
let destination_idx = y * width;
792+
let source_idx = y * line_stride;
793+
let destination = &mut buffer[destination_idx..destination_idx + width];
794+
let source = &data[0][source_idx..source_idx + width];
795+
destination.copy_from_slice(source);
794796
}
795797

796798
Ok(buffer)

0 commit comments

Comments
 (0)