mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Avoid unnecessary re-reads/seeks in RandomAccessFile::ReadBlock
parent
653e5e8b63
commit
b84a164590
|
@ -144,7 +144,15 @@ uint32_t RandomAccessFile::ReadDword()
|
||||||
*/
|
*/
|
||||||
void RandomAccessFile::ReadBlock(void *ptr, size_t size)
|
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_t>(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<char *>(ptr) + to_copy;
|
||||||
|
}
|
||||||
|
|
||||||
this->pos += fread(ptr, 1, size, *this->file_handle);
|
this->pos += fread(ptr, 1, size, *this->file_handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue