Skip to content

Commit 2801061

Browse files
committed
Simplify reallocs
1 parent 9ab5c4f commit 2801061

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/reader/decoder.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -620,21 +620,23 @@ impl StreamingDecoder {
620620
}
621621

622622
if local_table {
623-
let entries = PLTE_CHANNELS * (1 << (table_size + 1));
624-
let mut pal = Vec::new();
625-
pal.try_reserve_exact(entries).map_err(|_| io::ErrorKind::OutOfMemory)?;
626-
frame.palette = Some(pal);
627-
goto!(LocalPalette(entries))
623+
let pal_len = PLTE_CHANNELS * (1 << (table_size + 1));
624+
frame.palette.get_or_insert_with(Vec::new)
625+
.try_reserve_exact(pal_len).map_err(|_| io::ErrorKind::OutOfMemory)?;
626+
goto!(LocalPalette(pal_len))
628627
} else {
629628
goto!(LocalPalette(0))
630629
}
631630
}
632631
}
633632
}
634633
GlobalPalette(left) => {
635-
let n = cmp::min(left, buf.len());
634+
// the global_color_table is guaranteed to have the exact capacity required
636635
if left > 0 {
637-
self.global_color_table.extend_from_slice(&buf[..n]);
636+
let n = cmp::min(left, buf.len());
637+
if n <= self.global_color_table.capacity() - self.global_color_table.len() {
638+
self.global_color_table.extend_from_slice(&buf[..n]);
639+
}
638640
goto!(n, GlobalPalette(left - n))
639641
} else {
640642
goto!(BlockStart(b), emit Decoded::GlobalPalette(

0 commit comments

Comments
 (0)