Skip to content

Commit

Permalink
Simplify reallocs
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 5, 2025
1 parent 9ab5c4f commit 2801061
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/reader/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,21 +620,23 @@ impl StreamingDecoder {
}

if local_table {
let entries = PLTE_CHANNELS * (1 << (table_size + 1));
let mut pal = Vec::new();
pal.try_reserve_exact(entries).map_err(|_| io::ErrorKind::OutOfMemory)?;
frame.palette = Some(pal);
goto!(LocalPalette(entries))
let pal_len = PLTE_CHANNELS * (1 << (table_size + 1));
frame.palette.get_or_insert_with(Vec::new)
.try_reserve_exact(pal_len).map_err(|_| io::ErrorKind::OutOfMemory)?;
goto!(LocalPalette(pal_len))
} else {
goto!(LocalPalette(0))
}
}
}
}
GlobalPalette(left) => {
let n = cmp::min(left, buf.len());
// the global_color_table is guaranteed to have the exact capacity required
if left > 0 {
self.global_color_table.extend_from_slice(&buf[..n]);
let n = cmp::min(left, buf.len());
if n <= self.global_color_table.capacity() - self.global_color_table.len() {
self.global_color_table.extend_from_slice(&buf[..n]);
}
goto!(n, GlobalPalette(left - n))
} else {
goto!(BlockStart(b), emit Decoded::GlobalPalette(
Expand Down

0 comments on commit 2801061

Please sign in to comment.