Optimize file reading

This commit is contained in:
Maschell 2022-08-08 11:55:13 +02:00
parent 991ba41a20
commit ff5d23cae2

View File

@ -20,8 +20,13 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_
return -1; return -1;
} }
uint32_t filesize = lseek(iFd, 0, SEEK_END); struct stat file_stat;
lseek(iFd, 0, SEEK_SET); int rc = fstat(iFd, &file_stat);
if (rc < 0) {
close(iFd);
return -4;
}
uint32_t filesize = file_stat.st_size;
auto *buffer = (uint8_t *) memalign(0x40, ROUNDUP(filesize, 0x40)); auto *buffer = (uint8_t *) memalign(0x40, ROUNDUP(filesize, 0x40));
if (buffer == nullptr) { if (buffer == nullptr) {
@ -29,17 +34,18 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_
return -2; return -2;
} }
uint32_t blocksize = 0x20000; uint32_t blocksize = 0x80000;
uint32_t done = 0; uint32_t done = 0;
int32_t readBytes = 0; int32_t readBytes;
while (done < filesize) { while (done < filesize) {
if (done + blocksize > filesize) { if (done + blocksize > filesize) {
blocksize = filesize - done; blocksize = filesize - done;
} }
readBytes = read(iFd, buffer + done, blocksize); readBytes = read(iFd, buffer + done, blocksize);
if (readBytes <= 0) if (readBytes <= 0) {
break; break;
}
done += readBytes; done += readBytes;
} }