Skip to content

Commit 8974e9a

Browse files
committed
Reduce stack usage for CFA to prevent stack overflows
Because of rust-lang/rust#34283, in the get_decoder() function we ran out of stack space. Each CFA instance is ~19.000 bytes on the stack, and each decoder instance contains a camera member which contains a cfa member. This found by: cargo +nightly rustc --lib -- -Zprint-type-sizes 2>&1 | grep print-type > type-sizes.txt egrep "[[:digit:]]{5,9} bytes" type-sizes.txt
1 parent 513fbf9 commit 8974e9a

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

rawler/src/cfa.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ pub struct CFA {
5757
pub width: usize,
5858
/// Height of the repeating pattern
5959
pub height: usize,
60-
61-
pattern: [[usize; 48]; 48],
60+
// Actual pattern. We use u8 here because usize would blow
61+
// the stack usage and we don't need that much bits.
62+
pattern: [[u8; 48]; 48],
6263
}
6364

6465
impl Default for CFA {
@@ -99,7 +100,7 @@ impl CFA {
99100
144 => (12, 12),
100101
_ => panic!("Unknown CFA size \"{}\"", patname),
101102
};
102-
let mut pattern: [[usize; 48]; 48] = [[0; 48]; 48];
103+
let mut pattern: [[u8; 48]; 48] = [[0; 48]; 48];
103104

104105
if width > 0 {
105106
// copy the pattern into the top left
@@ -138,7 +139,7 @@ impl CFA {
138139
/// This is useful if you need to remap RGB to R G1 G2 B.
139140
pub fn map_colors<F>(&self, op: F) -> Self
140141
where
141-
F: Fn(usize, usize, usize) -> usize, // row, col, color -> new-color
142+
F: Fn(usize, usize, u8) -> u8, // row, col, color -> new-color
142143
{
143144
let mut copy = self.clone();
144145
for row in 0..48 {
@@ -152,12 +153,12 @@ impl CFA {
152153
/// Get the color index at the given position. Designed to be fast so it can be called
153154
/// from inner loops without performance issues.
154155
pub fn color_at(&self, row: usize, col: usize) -> usize {
155-
self.pattern[(row + 48) % 48][(col + 48) % 48]
156+
self.pattern[(row + 48) % 48][(col + 48) % 48] as usize
156157
}
157158

158159
/// from inner loops without performance issues.
159160
pub fn cfa_color_at(&self, row: usize, col: usize) -> CFAColor {
160-
self.pattern[(row + 48) % 48][(col + 48) % 48].try_into().unwrap()
161+
(self.pattern[(row + 48) % 48][(col + 48) % 48] as usize).try_into().unwrap()
161162
}
162163

163164
/// Get a flat pattern
@@ -218,10 +219,10 @@ impl CFA {
218219
/// assert_eq!(shifted.color_at(1,1), 0);
219220
/// ```
220221
pub fn shift(&self, x: usize, y: usize) -> CFA {
221-
let mut pattern: [[usize; 48]; 48] = [[0; 48]; 48];
222+
let mut pattern: [[u8; 48]; 48] = [[0; 48]; 48];
222223
for row in 0..48 {
223224
for col in 0..48 {
224-
pattern[row][col] = self.color_at(row + y, col + x);
225+
pattern[row][col] = self.color_at(row + y, col + x) as u8;
225226
}
226227
}
227228

rawler/src/decoders/iiq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl<'a> IiqDecoder<'a> {
490490
let cfa = self
491491
.camera
492492
.cfa
493-
.map_colors(|row, _col, color| if color == cfa::CFA_COLOR_G && (row & 1 == 1) { 3 } else { color });
493+
.map_colors(|row, _col, color| if (color as usize) == cfa::CFA_COLOR_G && (row & 1 == 1) { 3 } else { color });
494494

495495
for flat in senscorr.flats.iter() {
496496
let nc = match flat.typ {

0 commit comments

Comments
 (0)