Skip to content

Commit 42405df

Browse files
committed
pnmimagetypes: fix allocation efficiency in PNG reader
Rather than making one allocation per row, it instead makes one allocation for all rows.
1 parent 62781c1 commit 42405df

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

panda/src/pnmimagetypes/pnmFileTypePNG.cxx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,12 @@ read_data(xel *array, xelval *alpha_data) {
338338
// format, mainly because we keep array and alpha data separately, and there
339339
// doesn't appear to be good support to get this stuff out row-at-a-time for
340340
// interlaced files.
341-
png_bytep *rows = (png_bytep *)PANDA_MALLOC_ARRAY(num_rows * sizeof(png_bytep));
341+
png_bytep *rows = (png_bytep *)alloca(num_rows * sizeof(png_bytep));
342342
int yi;
343343

344+
png_byte *alloc = (png_byte *)PANDA_MALLOC_ARRAY(row_byte_length * sizeof(png_byte) * num_rows);
344345
for (yi = 0; yi < num_rows; yi++) {
345-
rows[yi] = (png_byte *)PANDA_MALLOC_ARRAY(row_byte_length * sizeof(png_byte));
346+
rows[yi] = alloc + (row_byte_length * sizeof(png_byte)) * yi;
346347
}
347348

348349
png_read_image(_png, rows);
@@ -402,12 +403,10 @@ read_data(xel *array, xelval *alpha_data) {
402403
}
403404

404405
nassertr(source <= rows[yi] + row_byte_length, yi);
405-
PANDA_FREE_ARRAY(rows[yi]);
406406
}
407407

408-
PANDA_FREE_ARRAY(rows);
409-
410408
png_read_end(_png, nullptr);
409+
PANDA_FREE_ARRAY(alloc);
411410

412411
return _y_size;
413412
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy