From bcc75fcfc161e06facaa57bd2dd6b8c07d37e234 Mon Sep 17 00:00:00 2001 From: Delian0 <40333853+Delian0@users.noreply.github.com> Date: Sat, 23 Nov 2024 23:08:52 +0100 Subject: [PATCH] Bugfix + corrected formatting There's a bug which makes loading files potentially 2x slower. After reading the whole file into the buffer, the loop unnecessarily repeats where the whole buffer is resized and reallocated. --- src/Engine/FileMap.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Engine/FileMap.cpp b/src/Engine/FileMap.cpp index b0d3f1bbc4..69808ea3ad 100644 --- a/src/Engine/FileMap.cpp +++ b/src/Engine/FileMap.cpp @@ -102,9 +102,11 @@ void *SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, int freesrc) return NULL; } + bool wholeFile = true; size = SDL_RWsize(src); if (size < 0) { size = FILE_CHUNK_SIZE; + wholeFile = false; } data = SDL_malloc((size_t)(size + 1)); @@ -119,17 +121,19 @@ void *SDL_LoadFile_RW(SDL_RWops *src, size_t *datasize, int freesrc) SDL_OutOfMemory(); goto done; } - data = newdata; - } - - size_read = SDL_RWread(src, (char *)data+size_total, 1, (size_t)(size-size_total)); - if (size_read == 0) { + data = newdata; + } + size_read = SDL_RWread(src, (char*)data + size_total, 1, (size_t)(size - size_total)); + if (size_read == 0) { + break; + } + if (size_read == -1) { break; } - if (size_read == -1) { - break; - } size_total += size_read; + if (wholeFile) { + break; + } } if (datasize) {