From 42405dff2edd11bc4e05058cc5befa817c74ebe9 Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 30 Dec 2018 20:10:50 +0100 Subject: [PATCH] pnmimagetypes: fix allocation efficiency in PNG reader Rather than making one allocation per row, it instead makes one allocation for all rows. --- panda/src/pnmimagetypes/pnmFileTypePNG.cxx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/panda/src/pnmimagetypes/pnmFileTypePNG.cxx b/panda/src/pnmimagetypes/pnmFileTypePNG.cxx index 799f4d1dce..7d64faed50 100644 --- a/panda/src/pnmimagetypes/pnmFileTypePNG.cxx +++ b/panda/src/pnmimagetypes/pnmFileTypePNG.cxx @@ -338,11 +338,12 @@ read_data(xel *array, xelval *alpha_data) { // format, mainly because we keep array and alpha data separately, and there // doesn't appear to be good support to get this stuff out row-at-a-time for // interlaced files. - png_bytep *rows = (png_bytep *)PANDA_MALLOC_ARRAY(num_rows * sizeof(png_bytep)); + png_bytep *rows = (png_bytep *)alloca(num_rows * sizeof(png_bytep)); int yi; + png_byte *alloc = (png_byte *)PANDA_MALLOC_ARRAY(row_byte_length * sizeof(png_byte) * num_rows); for (yi = 0; yi < num_rows; yi++) { - rows[yi] = (png_byte *)PANDA_MALLOC_ARRAY(row_byte_length * sizeof(png_byte)); + rows[yi] = alloc + (row_byte_length * sizeof(png_byte)) * yi; } png_read_image(_png, rows); @@ -402,12 +403,10 @@ read_data(xel *array, xelval *alpha_data) { } nassertr(source <= rows[yi] + row_byte_length, yi); - PANDA_FREE_ARRAY(rows[yi]); } - PANDA_FREE_ARRAY(rows); - png_read_end(_png, nullptr); + PANDA_FREE_ARRAY(alloc); return _y_size; }