From b84a1645900d913036845ed33e5fdfccca188e4d Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 17 Nov 2024 14:12:07 +0000 Subject: [PATCH] Codechange: Avoid unnecessary re-reads/seeks in RandomAccessFile::ReadBlock --- src/random_access_file.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/random_access_file.cpp b/src/random_access_file.cpp index dc80021271..397f729bd8 100644 --- a/src/random_access_file.cpp +++ b/src/random_access_file.cpp @@ -144,7 +144,15 @@ uint32_t RandomAccessFile::ReadDword() */ void RandomAccessFile::ReadBlock(void *ptr, size_t size) { - this->SeekTo(this->GetPos(), SEEK_SET); + if (this->buffer != this->buffer_end) { + size_t to_copy = std::min(size, this->buffer_end - this->buffer); + memcpy(ptr, this->buffer, to_copy); + this->buffer += to_copy; + size -= to_copy; + if (size == 0) return; + ptr = static_cast(ptr) + to_copy; + } + this->pos += fread(ptr, 1, size, *this->file_handle); }